blob: e77b1dfcd58a303306bcc7799e2a9ecb84ff04b2 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000041#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000044#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000045#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000048#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000050#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000053#include "llvm/Support/raw_ostream.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000054#include "llvm/Target/TargetOptions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000055using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000056using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000057
Evan Chengb1712452010-01-27 06:25:16 +000058STATISTIC(NumTailCalls, "Number of tail calls");
59
Evan Cheng10e86422008-04-25 19:11:04 +000060// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000061static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000062 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000063
David Greenea5f26012011-02-07 19:36:54 +000064static SDValue Insert128BitVector(SDValue Result,
65 SDValue Vec,
66 SDValue Idx,
67 SelectionDAG &DAG,
68 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000069
David Greenea5f26012011-02-07 19:36:54 +000070static SDValue Extract128BitVector(SDValue Vec,
71 SDValue Idx,
72 SelectionDAG &DAG,
73 DebugLoc dl);
74
75/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
76/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000077/// simple subregister reference. Idx is an index in the 128 bits we
78/// want. It need not be aligned to a 128-bit bounday. That makes
79/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000080static SDValue Extract128BitVector(SDValue Vec,
81 SDValue Idx,
82 SelectionDAG &DAG,
83 DebugLoc dl) {
84 EVT VT = Vec.getValueType();
85 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000086 EVT ElVT = VT.getVectorElementType();
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000087 int Factor = VT.getSizeInBits()/128;
88 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
89 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000090
91 // Extract from UNDEF is UNDEF.
92 if (Vec.getOpcode() == ISD::UNDEF)
93 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
94
95 if (isa<ConstantSDNode>(Idx)) {
96 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
97
98 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
99 // we can match to VEXTRACTF128.
100 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
101
102 // This is the index of the first element of the 128-bit chunk
103 // we want.
104 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
105 * ElemsPerChunk);
106
107 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000108 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
109 VecIdx);
110
111 return Result;
112 }
113
114 return SDValue();
115}
116
117/// Generate a DAG to put 128-bits into a vector > 128 bits. This
118/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000119/// simple superregister reference. Idx is an index in the 128 bits
120/// we want. It need not be aligned to a 128-bit bounday. That makes
121/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000122static SDValue Insert128BitVector(SDValue Result,
123 SDValue Vec,
124 SDValue Idx,
125 SelectionDAG &DAG,
126 DebugLoc dl) {
127 if (isa<ConstantSDNode>(Idx)) {
128 EVT VT = Vec.getValueType();
129 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
130
131 EVT ElVT = VT.getVectorElementType();
David Greenea5f26012011-02-07 19:36:54 +0000132 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
David Greenea5f26012011-02-07 19:36:54 +0000133 EVT ResultVT = Result.getValueType();
134
135 // Insert the relevant 128 bits.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000136 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000137
138 // This is the index of the first element of the 128-bit chunk
139 // we want.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000140 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
David Greenea5f26012011-02-07 19:36:54 +0000141 * ElemsPerChunk);
142
143 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000144 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
145 VecIdx);
146 return Result;
147 }
148
149 return SDValue();
150}
151
Chris Lattnerf0144122009-07-28 03:13:23 +0000152static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000153 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
154 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000155
Evan Cheng2bffee22011-02-01 01:14:13 +0000156 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000157 if (is64Bit)
158 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000159 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000160 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000161
Evan Cheng203576a2011-07-20 19:50:42 +0000162 if (Subtarget->isTargetELF())
163 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000164 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000165 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000166 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000167}
168
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000169X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000170 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000171 Subtarget = &TM.getSubtarget<X86Subtarget>();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000172 X86ScalarSSEf64 = Subtarget->hasXMMInt();
173 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000174 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000175
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000176 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000177 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000178
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000179 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000180 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000181
182 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000183 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000184 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
185 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000186
Eric Christopherde5e1012011-03-11 01:05:58 +0000187 // For 64-bit since we have so many registers use the ILP scheduler, for
188 // 32-bit code use the register pressure specific scheduling.
189 if (Subtarget->is64Bit())
190 setSchedulingPreference(Sched::ILP);
191 else
192 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000193 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000194
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000195 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000196 // Setup Windows compiler runtime calls.
197 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000198 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000199 setLibcallName(RTLIB::SREM_I64, "_allrem");
200 setLibcallName(RTLIB::UREM_I64, "_aullrem");
201 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000202 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000203 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000204 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000205 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000206 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
207 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
208 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000209 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
210 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000211 }
212
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000213 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000214 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000215 setUseUnderscoreSetJmp(false);
216 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000217 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000218 // MS runtime is weird: it exports _setjmp, but longjmp!
219 setUseUnderscoreSetJmp(true);
220 setUseUnderscoreLongJmp(false);
221 } else {
222 setUseUnderscoreSetJmp(true);
223 setUseUnderscoreLongJmp(true);
224 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000225
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000226 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000228 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000230 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000232
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000234
Scott Michelfdc40a02009-02-17 22:15:04 +0000235 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000237 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000239 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
241 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000242
243 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
245 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
246 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
247 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
248 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
249 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000250
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000251 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
252 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000253 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
254 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
255 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000256
Evan Cheng25ab6902006-09-08 06:48:29 +0000257 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
259 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000260 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000261 // We have an algorithm for SSE2->double, and we turn this into a
262 // 64-bit FILD followed by conditional FADD for other targets.
263 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000264 // We have an algorithm for SSE2, and we turn this into a 64-bit
265 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000266 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000267 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000268
269 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
270 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
272 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000273
Devang Patel6a784892009-06-05 18:48:29 +0000274 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000275 // SSE has no i16 to fp conversion, only i32
276 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000278 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000280 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
282 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000283 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000284 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
286 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000287 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000288
Dale Johannesen73328d12007-09-19 23:55:34 +0000289 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
290 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
292 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000293
Evan Cheng02568ff2006-01-30 22:13:22 +0000294 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
295 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
297 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000298
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000299 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000301 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000303 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
305 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000306 }
307
308 // Handle FP_TO_UINT by promoting the destination to a larger signed
309 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
311 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
312 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000313
Evan Cheng25ab6902006-09-08 06:48:29 +0000314 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
316 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000317 } else if (!UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000318 // Since AVX is a superset of SSE3, only check for SSE here.
319 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 // Expand FP_TO_UINT into a select.
321 // FIXME: We would like to use a Custom expander here eventually to do
322 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000325 // With SSE3 we can use fisttpll to convert to a signed i64; without
326 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000328 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000329
Chris Lattner399610a2006-12-05 18:22:22 +0000330 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000331 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000332 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
333 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000334 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000335 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000336 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000337 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000338 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000339 }
Chris Lattner21f66852005-12-23 05:15:23 +0000340
Dan Gohmanb00ee212008-02-18 19:34:53 +0000341 // Scalar integer divide and remainder are lowered to use operations that
342 // produce two results, to match the available instructions. This exposes
343 // the two-result form to trivial CSE, which is able to combine x/y and x%y
344 // into a single instruction.
345 //
346 // Scalar integer multiply-high is also lowered to use two-result
347 // operations, to match the available instructions. However, plain multiply
348 // (low) operations are left as Legal, as there are single-result
349 // instructions for this in x86. Using the two-result multiply instructions
350 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000351 for (unsigned i = 0, e = 4; i != e; ++i) {
352 MVT VT = IntVTs[i];
353 setOperationAction(ISD::MULHS, VT, Expand);
354 setOperationAction(ISD::MULHU, VT, Expand);
355 setOperationAction(ISD::SDIV, VT, Expand);
356 setOperationAction(ISD::UDIV, VT, Expand);
357 setOperationAction(ISD::SREM, VT, Expand);
358 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000359
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000360 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000361 setOperationAction(ISD::ADDC, VT, Custom);
362 setOperationAction(ISD::ADDE, VT, Custom);
363 setOperationAction(ISD::SUBC, VT, Custom);
364 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000365 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000366
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
368 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
369 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
370 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000371 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
375 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
376 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
377 setOperationAction(ISD::FREM , MVT::f32 , Expand);
378 setOperationAction(ISD::FREM , MVT::f64 , Expand);
379 setOperationAction(ISD::FREM , MVT::f80 , Expand);
380 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000381
Craig Topper909652f2011-10-14 03:21:46 +0000382 if (Subtarget->hasBMI()) {
383 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
384 } else {
385 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
386 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
387 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
388 if (Subtarget->is64Bit())
389 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
390 }
Craig Topper37f21672011-10-11 06:44:02 +0000391
392 if (Subtarget->hasLZCNT()) {
393 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
394 } else {
395 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
396 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
397 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
398 if (Subtarget->is64Bit())
399 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000400 }
401
Benjamin Kramer1292c222010-12-04 20:32:23 +0000402 if (Subtarget->hasPOPCNT()) {
403 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
404 } else {
405 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
406 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
407 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
408 if (Subtarget->is64Bit())
409 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
410 }
411
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
413 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000414
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000415 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000416 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000417 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000418 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000419 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
421 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
422 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
423 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
424 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000425 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
427 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
428 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
429 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000430 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000432 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000433 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000434 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000435
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000436 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
438 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
439 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
440 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000441 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000442 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
443 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000444 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000445 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000446 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
447 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
448 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
449 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000450 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000451 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000452 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
454 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
455 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000456 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000457 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
458 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
459 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000460 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000461
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000462 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000464
Eric Christopher9a9d2752010-07-22 02:48:34 +0000465 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000466 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000467
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000468 // On X86 and X86-64, atomic operations are lowered to locked instructions.
469 // Locked instructions, in turn, have implicit fence semantics (all memory
470 // operations are flushed before issuing the locked instruction, and they
471 // are not buffered), so we can fold away the common pattern of
472 // fence-atomic-fence.
473 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000474
Mon P Wang63307c32008-05-05 19:05:59 +0000475 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000476 for (unsigned i = 0, e = 4; i != e; ++i) {
477 MVT VT = IntVTs[i];
478 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
479 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000480 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000481 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000482
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000483 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000484 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
486 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
487 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
488 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
489 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
490 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
491 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000492 }
493
Eli Friedman43f51ae2011-08-26 21:21:21 +0000494 if (Subtarget->hasCmpxchg16b()) {
495 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
496 }
497
Evan Cheng3c992d22006-03-07 02:02:57 +0000498 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000499 if (!Subtarget->isTargetDarwin() &&
500 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000501 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000502 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000503 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000504
Owen Anderson825b72b2009-08-11 20:47:22 +0000505 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
506 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
507 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
508 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000509 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000510 setExceptionPointerRegister(X86::RAX);
511 setExceptionSelectorRegister(X86::RDX);
512 } else {
513 setExceptionPointerRegister(X86::EAX);
514 setExceptionSelectorRegister(X86::EDX);
515 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
517 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000518
Duncan Sands4a544a72011-09-06 13:37:06 +0000519 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
520 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000521
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000523
Nate Begemanacc398c2006-01-25 18:21:52 +0000524 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000525 setOperationAction(ISD::VASTART , MVT::Other, Custom);
526 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000527 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000528 setOperationAction(ISD::VAARG , MVT::Other, Custom);
529 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000530 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000531 setOperationAction(ISD::VAARG , MVT::Other, Expand);
532 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000533 }
Evan Chengae642192007-03-02 23:16:35 +0000534
Owen Anderson825b72b2009-08-11 20:47:22 +0000535 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
536 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000537
538 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
539 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
540 MVT::i64 : MVT::i32, Custom);
541 else if (EnableSegmentedStacks)
542 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
543 MVT::i64 : MVT::i32, Custom);
544 else
545 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
546 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000547
Evan Chengc7ce29b2009-02-13 22:36:38 +0000548 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000549 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000550 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
552 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000553
Evan Cheng223547a2006-01-31 22:28:30 +0000554 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000555 setOperationAction(ISD::FABS , MVT::f64, Custom);
556 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000557
558 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000559 setOperationAction(ISD::FNEG , MVT::f64, Custom);
560 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000561
Evan Cheng68c47cb2007-01-05 07:55:56 +0000562 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000563 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
564 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000565
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000566 // Lower this to FGETSIGNx86 plus an AND.
567 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
568 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
569
Evan Chengd25e9e82006-02-02 00:28:23 +0000570 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000571 setOperationAction(ISD::FSIN , MVT::f64, Expand);
572 setOperationAction(ISD::FCOS , MVT::f64, Expand);
573 setOperationAction(ISD::FSIN , MVT::f32, Expand);
574 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000575
Chris Lattnera54aa942006-01-29 06:26:08 +0000576 // Expand FP immediates into loads from the stack, except for the special
577 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000578 addLegalFPImmediate(APFloat(+0.0)); // xorpd
579 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000580 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000581 // Use SSE for f32, x87 for f64.
582 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000583 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
584 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000585
586 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000587 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000588
589 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000590 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000591
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000593
594 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000595 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
596 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000597
598 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000599 setOperationAction(ISD::FSIN , MVT::f32, Expand);
600 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000601
Nate Begemane1795842008-02-14 08:57:00 +0000602 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000603 addLegalFPImmediate(APFloat(+0.0f)); // xorps
604 addLegalFPImmediate(APFloat(+0.0)); // FLD0
605 addLegalFPImmediate(APFloat(+1.0)); // FLD1
606 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
607 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
608
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000609 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
611 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000612 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000613 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000614 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000615 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
617 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000618
Owen Anderson825b72b2009-08-11 20:47:22 +0000619 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
620 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
621 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
622 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000623
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000624 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000625 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
626 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000627 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000628 addLegalFPImmediate(APFloat(+0.0)); // FLD0
629 addLegalFPImmediate(APFloat(+1.0)); // FLD1
630 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
631 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000632 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
633 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
634 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
635 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000636 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000637
Cameron Zwarich33390842011-07-08 21:39:21 +0000638 // We don't support FMA.
639 setOperationAction(ISD::FMA, MVT::f64, Expand);
640 setOperationAction(ISD::FMA, MVT::f32, Expand);
641
Dale Johannesen59a58732007-08-05 18:49:15 +0000642 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000643 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
645 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
646 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000647 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000648 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000649 addLegalFPImmediate(TmpFlt); // FLD0
650 TmpFlt.changeSign();
651 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000652
653 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000654 APFloat TmpFlt2(+1.0);
655 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
656 &ignored);
657 addLegalFPImmediate(TmpFlt2); // FLD1
658 TmpFlt2.changeSign();
659 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
660 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000661
Evan Chengc7ce29b2009-02-13 22:36:38 +0000662 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
664 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000665 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000666
667 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000668 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000669
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000670 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000671 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
672 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
673 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000674
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 setOperationAction(ISD::FLOG, MVT::f80, Expand);
676 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
677 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
678 setOperationAction(ISD::FEXP, MVT::f80, Expand);
679 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000680
Mon P Wangf007a8b2008-11-06 05:31:54 +0000681 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000682 // (for widening) or expand (for scalarization). Then we will selectively
683 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
685 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
686 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000702 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
703 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000704 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000725 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000726 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000735 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000736 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000740 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000741 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
742 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
743 setTruncStoreAction((MVT::SimpleValueType)VT,
744 (MVT::SimpleValueType)InnerVT, Expand);
745 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
746 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
747 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000748 }
749
Evan Chengc7ce29b2009-02-13 22:36:38 +0000750 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
751 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000752 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000753 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000754 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000755 }
756
Dale Johannesen0488fb62010-09-30 23:57:10 +0000757 // MMX-sized vectors (other than x86mmx) are expected to be expanded
758 // into smaller operations.
759 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
760 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
761 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
762 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
763 setOperationAction(ISD::AND, MVT::v8i8, Expand);
764 setOperationAction(ISD::AND, MVT::v4i16, Expand);
765 setOperationAction(ISD::AND, MVT::v2i32, Expand);
766 setOperationAction(ISD::AND, MVT::v1i64, Expand);
767 setOperationAction(ISD::OR, MVT::v8i8, Expand);
768 setOperationAction(ISD::OR, MVT::v4i16, Expand);
769 setOperationAction(ISD::OR, MVT::v2i32, Expand);
770 setOperationAction(ISD::OR, MVT::v1i64, Expand);
771 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
772 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
773 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
774 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
775 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
776 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
777 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
778 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
779 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
780 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
781 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
782 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
783 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000784 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
785 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
786 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
787 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000788
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000789 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000791
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
793 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
794 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
795 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
796 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
797 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
798 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
799 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
800 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
801 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
802 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000803 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000804 }
805
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000806 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000808
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000809 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
810 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
812 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
813 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
814 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000815
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
817 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
818 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
819 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
820 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
821 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
822 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
823 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
824 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
825 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
826 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
827 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
828 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
829 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
830 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
831 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000832
Nadav Rotem354efd82011-09-18 14:57:03 +0000833 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000834 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
835 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
836 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000837
Owen Anderson825b72b2009-08-11 20:47:22 +0000838 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
839 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
840 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
841 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
842 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000843
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000844 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
845 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
846 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
847 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
848 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
849
Evan Cheng2c3ae372006-04-12 21:21:57 +0000850 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
852 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000853 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000854 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000855 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000856 // Do not attempt to custom lower non-128-bit vectors
857 if (!VT.is128BitVector())
858 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000859 setOperationAction(ISD::BUILD_VECTOR,
860 VT.getSimpleVT().SimpleTy, Custom);
861 setOperationAction(ISD::VECTOR_SHUFFLE,
862 VT.getSimpleVT().SimpleTy, Custom);
863 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
864 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000865 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000866
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
868 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
869 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
870 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
871 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
872 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000873
Nate Begemancdd1eec2008-02-12 22:51:28 +0000874 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
876 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000877 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000878
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000879 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
881 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000882 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000883
884 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000885 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000886 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000887
Owen Andersond6662ad2009-08-10 20:46:15 +0000888 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000890 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000892 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000894 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000896 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000898 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000899
Owen Anderson825b72b2009-08-11 20:47:22 +0000900 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000901
Evan Cheng2c3ae372006-04-12 21:21:57 +0000902 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
904 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
905 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
906 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000907
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
909 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000910 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000911
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000912 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000913 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
914 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
915 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
916 setOperationAction(ISD::FRINT, MVT::f32, Legal);
917 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
918 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
919 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
920 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
921 setOperationAction(ISD::FRINT, MVT::f64, Legal);
922 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
923
Nate Begeman14d12ca2008-02-11 04:19:36 +0000924 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000926
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000927 // Can turn SHL into an integer multiply.
928 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000929 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000930
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000931 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
932 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
933 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
934 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
935 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000936
Nate Begeman14d12ca2008-02-11 04:19:36 +0000937 // i8 and i16 vectors are custom , because the source register and source
938 // source memory operand types are not the same width. f32 vectors are
939 // custom since the immediate controlling the insert encodes additional
940 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
942 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
943 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
944 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000945
Owen Anderson825b72b2009-08-11 20:47:22 +0000946 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
947 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
948 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
949 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000950
951 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000952 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
953 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000954 }
955 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000956
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000957 if (Subtarget->hasXMMInt()) {
Nadav Rotem43012222011-05-11 08:12:09 +0000958 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
959 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
960 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000961 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000962
963 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
964 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
965 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
966
967 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
968 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000969 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000970 }
971
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000972 if (Subtarget->hasSSE42() || Subtarget->hasAVX())
Duncan Sands28b77e92011-09-06 19:07:46 +0000973 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000974
David Greene9b9838d2009-06-29 16:47:10 +0000975 if (!UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000976 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
977 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
978 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
979 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
980 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
981 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000982
Owen Anderson825b72b2009-08-11 20:47:22 +0000983 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000984 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
985 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000986
Owen Anderson825b72b2009-08-11 20:47:22 +0000987 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
988 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
989 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
990 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
991 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
992 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000993
Owen Anderson825b72b2009-08-11 20:47:22 +0000994 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
995 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
996 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
997 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
998 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
999 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001000
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001001 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1002 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001003 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001004
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00001005 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
1006 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
1007 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
1008 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
1009 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
1010 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
1011
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001012 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1013 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1014 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1015 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1016
1017 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1018 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1019 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1020 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1021
1022 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
1023 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
1024
Duncan Sands28b77e92011-09-06 19:07:46 +00001025 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1026 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1027 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1028 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001029
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001030 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1031 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1032 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1033
Craig Topperaaa643c2011-11-09 07:28:55 +00001034 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1035 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1036 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1037 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001038
Craig Topperaaa643c2011-11-09 07:28:55 +00001039 if (Subtarget->hasAVX2()) {
1040 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1041 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1042 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1043 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001044
Craig Topperaaa643c2011-11-09 07:28:55 +00001045 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1046 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1047 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1048 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001049
Craig Topperaaa643c2011-11-09 07:28:55 +00001050 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1051 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1052 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001053 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001054
1055 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001056 } else {
1057 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1058 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1059 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1060 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1061
1062 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1063 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1064 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1065 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1066
1067 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1068 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1069 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1070 // Don't lower v32i8 because there is no 128-bit byte mul
1071 }
Craig Topper13894fa2011-08-24 06:14:18 +00001072
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001073 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001074 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001075 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1076 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1077 EVT VT = SVT;
1078
1079 // Extract subvector is special because the value type
1080 // (result) is 128-bit but the source is 256-bit wide.
1081 if (VT.is128BitVector())
1082 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1083
1084 // Do not attempt to custom lower other non-256-bit vectors
1085 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001086 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001087
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001088 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1089 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1090 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1091 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001092 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001093 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001094 }
1095
David Greene54d8eba2011-01-27 22:38:56 +00001096 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001097 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1098 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1099 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001100
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001101 // Do not attempt to promote non-256-bit vectors
1102 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001103 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001104
1105 setOperationAction(ISD::AND, SVT, Promote);
1106 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1107 setOperationAction(ISD::OR, SVT, Promote);
1108 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1109 setOperationAction(ISD::XOR, SVT, Promote);
1110 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1111 setOperationAction(ISD::LOAD, SVT, Promote);
1112 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1113 setOperationAction(ISD::SELECT, SVT, Promote);
1114 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001115 }
David Greene9b9838d2009-06-29 16:47:10 +00001116 }
1117
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001118 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1119 // of this type with custom code.
1120 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1121 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1122 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1123 }
1124
Evan Cheng6be2c582006-04-05 23:38:46 +00001125 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001126 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001127
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001128
Eli Friedman962f5492010-06-02 19:35:46 +00001129 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1130 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001131 //
Eli Friedman962f5492010-06-02 19:35:46 +00001132 // FIXME: We really should do custom legalization for addition and
1133 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1134 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001135 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1136 // Add/Sub/Mul with overflow operations are custom lowered.
1137 MVT VT = IntVTs[i];
1138 setOperationAction(ISD::SADDO, VT, Custom);
1139 setOperationAction(ISD::UADDO, VT, Custom);
1140 setOperationAction(ISD::SSUBO, VT, Custom);
1141 setOperationAction(ISD::USUBO, VT, Custom);
1142 setOperationAction(ISD::SMULO, VT, Custom);
1143 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001144 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001145
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001146 // There are no 8-bit 3-address imul/mul instructions
1147 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1148 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001149
Evan Chengd54f2d52009-03-31 19:38:51 +00001150 if (!Subtarget->is64Bit()) {
1151 // These libcalls are not available in 32-bit.
1152 setLibcallName(RTLIB::SHL_I128, 0);
1153 setLibcallName(RTLIB::SRL_I128, 0);
1154 setLibcallName(RTLIB::SRA_I128, 0);
1155 }
1156
Evan Cheng206ee9d2006-07-07 08:33:52 +00001157 // We have target-specific dag combine patterns for the following nodes:
1158 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001159 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001160 setTargetDAGCombine(ISD::BUILD_VECTOR);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001161 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001162 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001163 setTargetDAGCombine(ISD::SHL);
1164 setTargetDAGCombine(ISD::SRA);
1165 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001166 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001167 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001168 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001169 setTargetDAGCombine(ISD::FADD);
1170 setTargetDAGCombine(ISD::FSUB);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001171 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001172 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001173 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001174 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001175 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001176 if (Subtarget->is64Bit())
1177 setTargetDAGCombine(ISD::MUL);
Craig Topperb4c94572011-10-21 06:55:01 +00001178 if (Subtarget->hasBMI())
1179 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001180
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001181 computeRegisterProperties();
1182
Evan Cheng05219282011-01-06 06:52:41 +00001183 // On Darwin, -Os means optimize for size without hurting performance,
1184 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001185 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001186 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001187 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001188 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1189 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1190 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001191 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001192 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001193
1194 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001195}
1196
Scott Michel5b8f82e2008-03-10 15:42:14 +00001197
Duncan Sands28b77e92011-09-06 19:07:46 +00001198EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1199 if (!VT.isVector()) return MVT::i8;
1200 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001201}
1202
1203
Evan Cheng29286502008-01-23 23:17:41 +00001204/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1205/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001206static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001207 if (MaxAlign == 16)
1208 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001209 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001210 if (VTy->getBitWidth() == 128)
1211 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001212 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001213 unsigned EltAlign = 0;
1214 getMaxByValAlign(ATy->getElementType(), EltAlign);
1215 if (EltAlign > MaxAlign)
1216 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001217 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001218 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1219 unsigned EltAlign = 0;
1220 getMaxByValAlign(STy->getElementType(i), EltAlign);
1221 if (EltAlign > MaxAlign)
1222 MaxAlign = EltAlign;
1223 if (MaxAlign == 16)
1224 break;
1225 }
1226 }
1227 return;
1228}
1229
1230/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1231/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001232/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1233/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001234unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001235 if (Subtarget->is64Bit()) {
1236 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001237 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001238 if (TyAlign > 8)
1239 return TyAlign;
1240 return 8;
1241 }
1242
Evan Cheng29286502008-01-23 23:17:41 +00001243 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001244 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001245 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001246 return Align;
1247}
Chris Lattner2b02a442007-02-25 08:29:00 +00001248
Evan Chengf0df0312008-05-15 08:39:06 +00001249/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001250/// and store operations as a result of memset, memcpy, and memmove
1251/// lowering. If DstAlign is zero that means it's safe to destination
1252/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1253/// means there isn't a need to check it against alignment requirement,
1254/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001255/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001256/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1257/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1258/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001259/// It returns EVT::Other if the type should be determined using generic
1260/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001261EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001262X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1263 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001264 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001265 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001266 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001267 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1268 // linux. This is because the stack realignment code can't handle certain
1269 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001270 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001271 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001272 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001273 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001274 (Subtarget->isUnalignedMemAccessFast() ||
1275 ((DstAlign == 0 || DstAlign >= 16) &&
1276 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001277 Subtarget->getStackAlignment() >= 16) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001278 if (Subtarget->hasAVX() &&
1279 Subtarget->getStackAlignment() >= 32)
1280 return MVT::v8f32;
1281 if (Subtarget->hasXMMInt())
Evan Cheng255f20f2010-04-01 06:04:33 +00001282 return MVT::v4i32;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001283 if (Subtarget->hasXMM())
Evan Cheng255f20f2010-04-01 06:04:33 +00001284 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001285 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001286 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001287 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001288 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001289 // Do not use f64 to lower memcpy if source is string constant. It's
1290 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001291 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001292 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001293 }
Evan Chengf0df0312008-05-15 08:39:06 +00001294 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001295 return MVT::i64;
1296 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001297}
1298
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001299/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1300/// current function. The returned value is a member of the
1301/// MachineJumpTableInfo::JTEntryKind enum.
1302unsigned X86TargetLowering::getJumpTableEncoding() const {
1303 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1304 // symbol.
1305 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1306 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001307 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001308
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001309 // Otherwise, use the normal jump table encoding heuristics.
1310 return TargetLowering::getJumpTableEncoding();
1311}
1312
Chris Lattnerc64daab2010-01-26 05:02:42 +00001313const MCExpr *
1314X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1315 const MachineBasicBlock *MBB,
1316 unsigned uid,MCContext &Ctx) const{
1317 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1318 Subtarget->isPICStyleGOT());
1319 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1320 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001321 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1322 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001323}
1324
Evan Chengcc415862007-11-09 01:32:10 +00001325/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1326/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001327SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001328 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001329 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001330 // This doesn't have DebugLoc associated with it, but is not really the
1331 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001332 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001333 return Table;
1334}
1335
Chris Lattner589c6f62010-01-26 06:28:43 +00001336/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1337/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1338/// MCExpr.
1339const MCExpr *X86TargetLowering::
1340getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1341 MCContext &Ctx) const {
1342 // X86-64 uses RIP relative addressing based on the jump table label.
1343 if (Subtarget->isPICStyleRIPRel())
1344 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1345
1346 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001347 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001348}
1349
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001350// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001351std::pair<const TargetRegisterClass*, uint8_t>
1352X86TargetLowering::findRepresentativeClass(EVT VT) const{
1353 const TargetRegisterClass *RRC = 0;
1354 uint8_t Cost = 1;
1355 switch (VT.getSimpleVT().SimpleTy) {
1356 default:
1357 return TargetLowering::findRepresentativeClass(VT);
1358 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1359 RRC = (Subtarget->is64Bit()
1360 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1361 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001362 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001363 RRC = X86::VR64RegisterClass;
1364 break;
1365 case MVT::f32: case MVT::f64:
1366 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1367 case MVT::v4f32: case MVT::v2f64:
1368 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1369 case MVT::v4f64:
1370 RRC = X86::VR128RegisterClass;
1371 break;
1372 }
1373 return std::make_pair(RRC, Cost);
1374}
1375
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001376bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1377 unsigned &Offset) const {
1378 if (!Subtarget->isTargetLinux())
1379 return false;
1380
1381 if (Subtarget->is64Bit()) {
1382 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1383 Offset = 0x28;
1384 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1385 AddressSpace = 256;
1386 else
1387 AddressSpace = 257;
1388 } else {
1389 // %gs:0x14 on i386
1390 Offset = 0x14;
1391 AddressSpace = 256;
1392 }
1393 return true;
1394}
1395
1396
Chris Lattner2b02a442007-02-25 08:29:00 +00001397//===----------------------------------------------------------------------===//
1398// Return Value Calling Convention Implementation
1399//===----------------------------------------------------------------------===//
1400
Chris Lattner59ed56b2007-02-28 04:55:35 +00001401#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001402
Michael J. Spencerec38de22010-10-10 22:04:20 +00001403bool
Eric Christopher471e4222011-06-08 23:55:35 +00001404X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1405 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001406 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001407 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001408 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001409 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001410 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001411 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001412}
1413
Dan Gohman98ca4f22009-08-05 01:29:28 +00001414SDValue
1415X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001416 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001417 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001418 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001419 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001420 MachineFunction &MF = DAG.getMachineFunction();
1421 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001422
Chris Lattner9774c912007-02-27 05:28:59 +00001423 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001424 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001425 RVLocs, *DAG.getContext());
1426 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001427
Evan Chengdcea1632010-02-04 02:40:39 +00001428 // Add the regs to the liveout set for the function.
1429 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1430 for (unsigned i = 0; i != RVLocs.size(); ++i)
1431 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1432 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001433
Dan Gohman475871a2008-07-27 21:46:04 +00001434 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001435
Dan Gohman475871a2008-07-27 21:46:04 +00001436 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001437 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1438 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001439 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1440 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001441
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001442 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001443 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1444 CCValAssign &VA = RVLocs[i];
1445 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001446 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001447 EVT ValVT = ValToCopy.getValueType();
1448
Dale Johannesenc4510512010-09-24 19:05:48 +00001449 // If this is x86-64, and we disabled SSE, we can't return FP values,
1450 // or SSE or MMX vectors.
1451 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1452 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001453 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001454 report_fatal_error("SSE register return with SSE disabled");
1455 }
1456 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1457 // llvm-gcc has never done it right and no one has noticed, so this
1458 // should be OK for now.
1459 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001460 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001461 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001462
Chris Lattner447ff682008-03-11 03:23:40 +00001463 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1464 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001465 if (VA.getLocReg() == X86::ST0 ||
1466 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001467 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1468 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001469 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001470 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001471 RetOps.push_back(ValToCopy);
1472 // Don't emit a copytoreg.
1473 continue;
1474 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001475
Evan Cheng242b38b2009-02-23 09:03:22 +00001476 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1477 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001478 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001479 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001480 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001481 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001482 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1483 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001484 // If we don't have SSE2 available, convert to v4f32 so the generated
1485 // register is legal.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001486 if (!Subtarget->hasXMMInt())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001487 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001488 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001489 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001490 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001491
Dale Johannesendd64c412009-02-04 00:33:20 +00001492 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001493 Flag = Chain.getValue(1);
1494 }
Dan Gohman61a92132008-04-21 23:59:07 +00001495
1496 // The x86-64 ABI for returning structs by value requires that we copy
1497 // the sret argument into %rax for the return. We saved the argument into
1498 // a virtual register in the entry block, so now we copy the value out
1499 // and into %rax.
1500 if (Subtarget->is64Bit() &&
1501 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1502 MachineFunction &MF = DAG.getMachineFunction();
1503 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1504 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001505 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001506 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001507 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001508
Dale Johannesendd64c412009-02-04 00:33:20 +00001509 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001510 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001511
1512 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001513 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001514 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001515
Chris Lattner447ff682008-03-11 03:23:40 +00001516 RetOps[0] = Chain; // Update chain.
1517
1518 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001519 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001520 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001521
1522 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001523 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001524}
1525
Evan Cheng3d2125c2010-11-30 23:55:39 +00001526bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1527 if (N->getNumValues() != 1)
1528 return false;
1529 if (!N->hasNUsesOfValue(1, 0))
1530 return false;
1531
1532 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001533 if (Copy->getOpcode() != ISD::CopyToReg &&
1534 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001535 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001536
1537 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001538 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001539 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001540 if (UI->getOpcode() != X86ISD::RET_FLAG)
1541 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001542 HasRet = true;
1543 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001544
Evan Cheng1bf891a2010-12-01 22:59:46 +00001545 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001546}
1547
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001548EVT
1549X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001550 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001551 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001552 // TODO: Is this also valid on 32-bit?
1553 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001554 ReturnMVT = MVT::i8;
1555 else
1556 ReturnMVT = MVT::i32;
1557
1558 EVT MinVT = getRegisterType(Context, ReturnMVT);
1559 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001560}
1561
Dan Gohman98ca4f22009-08-05 01:29:28 +00001562/// LowerCallResult - Lower the result values of a call into the
1563/// appropriate copies out of appropriate physical registers.
1564///
1565SDValue
1566X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001567 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001568 const SmallVectorImpl<ISD::InputArg> &Ins,
1569 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001570 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001571
Chris Lattnere32bbf62007-02-28 07:09:55 +00001572 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001573 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001574 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001575 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1576 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001577 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001578
Chris Lattner3085e152007-02-25 08:59:22 +00001579 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001580 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001581 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001582 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001583
Torok Edwin3f142c32009-02-01 18:15:56 +00001584 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001585 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001586 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001587 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001588 }
1589
Evan Cheng79fb3b42009-02-20 20:43:02 +00001590 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001591
1592 // If this is a call to a function that returns an fp value on the floating
1593 // point stack, we must guarantee the the value is popped from the stack, so
1594 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001595 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001596 // instead.
1597 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1598 // If we prefer to use the value in xmm registers, copy it out as f80 and
1599 // use a truncate to move it from fp stack reg to xmm reg.
1600 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001601 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001602 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1603 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001604 Val = Chain.getValue(0);
1605
1606 // Round the f80 to the right size, which also moves it to the appropriate
1607 // xmm register.
1608 if (CopyVT != VA.getValVT())
1609 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1610 // This truncation won't change the value.
1611 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001612 } else {
1613 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1614 CopyVT, InFlag).getValue(1);
1615 Val = Chain.getValue(0);
1616 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001617 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001618 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001619 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001620
Dan Gohman98ca4f22009-08-05 01:29:28 +00001621 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001622}
1623
1624
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001625//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001626// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001627//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001628// StdCall calling convention seems to be standard for many Windows' API
1629// routines and around. It differs from C calling convention just a little:
1630// callee should clean up the stack, not caller. Symbols should be also
1631// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001632// For info on fast calling convention see Fast Calling Convention (tail call)
1633// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001634
Dan Gohman98ca4f22009-08-05 01:29:28 +00001635/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001636/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001637static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1638 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001639 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001640
Dan Gohman98ca4f22009-08-05 01:29:28 +00001641 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001642}
1643
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001644/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001645/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001646static bool
1647ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1648 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001649 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001650
Dan Gohman98ca4f22009-08-05 01:29:28 +00001651 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001652}
1653
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001654/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1655/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001656/// the specific parameter attribute. The copy will be passed as a byval
1657/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001658static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001659CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001660 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1661 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001662 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001663
Dale Johannesendd64c412009-02-04 00:33:20 +00001664 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001665 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001666 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001667}
1668
Chris Lattner29689432010-03-11 00:22:57 +00001669/// IsTailCallConvention - Return true if the calling convention is one that
1670/// supports tail call optimization.
1671static bool IsTailCallConvention(CallingConv::ID CC) {
1672 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1673}
1674
Evan Cheng485fafc2011-03-21 01:19:09 +00001675bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1676 if (!CI->isTailCall())
1677 return false;
1678
1679 CallSite CS(CI);
1680 CallingConv::ID CalleeCC = CS.getCallingConv();
1681 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1682 return false;
1683
1684 return true;
1685}
1686
Evan Cheng0c439eb2010-01-27 00:07:07 +00001687/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1688/// a tailcall target by changing its ABI.
1689static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001690 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001691}
1692
Dan Gohman98ca4f22009-08-05 01:29:28 +00001693SDValue
1694X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001695 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001696 const SmallVectorImpl<ISD::InputArg> &Ins,
1697 DebugLoc dl, SelectionDAG &DAG,
1698 const CCValAssign &VA,
1699 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001700 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001701 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001702 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001703 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001704 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001705 EVT ValVT;
1706
1707 // If value is passed by pointer we have address passed instead of the value
1708 // itself.
1709 if (VA.getLocInfo() == CCValAssign::Indirect)
1710 ValVT = VA.getLocVT();
1711 else
1712 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001713
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001714 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001715 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001716 // In case of tail call optimization mark all arguments mutable. Since they
1717 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001718 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001719 unsigned Bytes = Flags.getByValSize();
1720 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1721 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001722 return DAG.getFrameIndex(FI, getPointerTy());
1723 } else {
1724 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001725 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001726 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1727 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001728 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001729 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001730 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001731}
1732
Dan Gohman475871a2008-07-27 21:46:04 +00001733SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001734X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001735 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001736 bool isVarArg,
1737 const SmallVectorImpl<ISD::InputArg> &Ins,
1738 DebugLoc dl,
1739 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001740 SmallVectorImpl<SDValue> &InVals)
1741 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001742 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001743 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001744
Gordon Henriksen86737662008-01-05 16:56:59 +00001745 const Function* Fn = MF.getFunction();
1746 if (Fn->hasExternalLinkage() &&
1747 Subtarget->isTargetCygMing() &&
1748 Fn->getName() == "main")
1749 FuncInfo->setForceFramePointer(true);
1750
Evan Cheng1bc78042006-04-26 01:20:17 +00001751 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001752 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001753 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001754
Chris Lattner29689432010-03-11 00:22:57 +00001755 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1756 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001757
Chris Lattner638402b2007-02-28 07:00:42 +00001758 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001759 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001760 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001761 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001762
1763 // Allocate shadow area for Win64
1764 if (IsWin64) {
1765 CCInfo.AllocateStack(32, 8);
1766 }
1767
Duncan Sands45907662010-10-31 13:21:44 +00001768 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001769
Chris Lattnerf39f7712007-02-28 05:46:49 +00001770 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001771 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001772 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1773 CCValAssign &VA = ArgLocs[i];
1774 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1775 // places.
1776 assert(VA.getValNo() != LastVal &&
1777 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001778 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001779 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001780
Chris Lattnerf39f7712007-02-28 05:46:49 +00001781 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001782 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001783 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001784 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001785 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001786 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001787 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001788 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001789 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001790 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001791 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001792 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1793 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001794 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001795 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001796 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001797 RC = X86::VR64RegisterClass;
1798 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001799 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001800
Devang Patel68e6bee2011-02-21 23:21:26 +00001801 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001802 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001803
Chris Lattnerf39f7712007-02-28 05:46:49 +00001804 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1805 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1806 // right size.
1807 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001808 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001809 DAG.getValueType(VA.getValVT()));
1810 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001811 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001812 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001813 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001814 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001815
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001816 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001817 // Handle MMX values passed in XMM regs.
1818 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001819 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1820 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001821 } else
1822 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001823 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001824 } else {
1825 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001826 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001827 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001828
1829 // If value is passed via pointer - do a load.
1830 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001831 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001832 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001833
Dan Gohman98ca4f22009-08-05 01:29:28 +00001834 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001835 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001836
Dan Gohman61a92132008-04-21 23:59:07 +00001837 // The x86-64 ABI for returning structs by value requires that we copy
1838 // the sret argument into %rax for the return. Save the argument into
1839 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001840 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001841 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1842 unsigned Reg = FuncInfo->getSRetReturnReg();
1843 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001844 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001845 FuncInfo->setSRetReturnReg(Reg);
1846 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001847 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001848 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001849 }
1850
Chris Lattnerf39f7712007-02-28 05:46:49 +00001851 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001852 // Align stack specially for tail calls.
1853 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001854 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001855
Evan Cheng1bc78042006-04-26 01:20:17 +00001856 // If the function takes variable number of arguments, make a frame index for
1857 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001858 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001859 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1860 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001861 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001862 }
1863 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001864 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1865
1866 // FIXME: We should really autogenerate these arrays
1867 static const unsigned GPR64ArgRegsWin64[] = {
1868 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001869 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001870 static const unsigned GPR64ArgRegs64Bit[] = {
1871 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1872 };
1873 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001874 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1875 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1876 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001877 const unsigned *GPR64ArgRegs;
1878 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001879
1880 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001881 // The XMM registers which might contain var arg parameters are shadowed
1882 // in their paired GPR. So we only need to save the GPR to their home
1883 // slots.
1884 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001885 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001886 } else {
1887 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1888 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001889
1890 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001891 }
1892 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1893 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001894
Devang Patel578efa92009-06-05 21:57:13 +00001895 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001896 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001897 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001898 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001899 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001900 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001901 // Kernel mode asks for SSE to be disabled, so don't push them
1902 // on the stack.
1903 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001904
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001905 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001906 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001907 // Get to the caller-allocated home save location. Add 8 to account
1908 // for the return address.
1909 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001910 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001911 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001912 // Fixup to set vararg frame on shadow area (4 x i64).
1913 if (NumIntRegs < 4)
1914 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001915 } else {
1916 // For X86-64, if there are vararg parameters that are passed via
1917 // registers, then we must store them to their spots on the stack so they
1918 // may be loaded by deferencing the result of va_next.
1919 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1920 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1921 FuncInfo->setRegSaveFrameIndex(
1922 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001923 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001924 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001925
Gordon Henriksen86737662008-01-05 16:56:59 +00001926 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001927 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001928 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1929 getPointerTy());
1930 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001931 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001932 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1933 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001934 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001935 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001936 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001937 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001938 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001939 MachinePointerInfo::getFixedStack(
1940 FuncInfo->getRegSaveFrameIndex(), Offset),
1941 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001942 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001943 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001944 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001945
Dan Gohmanface41a2009-08-16 21:24:25 +00001946 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1947 // Now store the XMM (fp + vector) parameter registers.
1948 SmallVector<SDValue, 11> SaveXMMOps;
1949 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001950
Devang Patel68e6bee2011-02-21 23:21:26 +00001951 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001952 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1953 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001954
Dan Gohman1e93df62010-04-17 14:41:14 +00001955 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1956 FuncInfo->getRegSaveFrameIndex()));
1957 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1958 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001959
Dan Gohmanface41a2009-08-16 21:24:25 +00001960 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001961 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001962 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001963 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1964 SaveXMMOps.push_back(Val);
1965 }
1966 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1967 MVT::Other,
1968 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001969 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001970
1971 if (!MemOps.empty())
1972 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1973 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001974 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001975 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001976
Gordon Henriksen86737662008-01-05 16:56:59 +00001977 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001978 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001979 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001980 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001981 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001982 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001983 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001984 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001985 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001986
Gordon Henriksen86737662008-01-05 16:56:59 +00001987 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001988 // RegSaveFrameIndex is X86-64 only.
1989 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001990 if (CallConv == CallingConv::X86_FastCall ||
1991 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001992 // fastcc functions can't have varargs.
1993 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001994 }
Evan Cheng25caf632006-05-23 21:06:34 +00001995
Rafael Espindola76927d752011-08-30 19:39:58 +00001996 FuncInfo->setArgumentStackSize(StackSize);
1997
Dan Gohman98ca4f22009-08-05 01:29:28 +00001998 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001999}
2000
Dan Gohman475871a2008-07-27 21:46:04 +00002001SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002002X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2003 SDValue StackPtr, SDValue Arg,
2004 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002005 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002006 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002007 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002008 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002009 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002010 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002011 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002012
2013 return DAG.getStore(Chain, dl, Arg, PtrOff,
2014 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002015 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002016}
2017
Bill Wendling64e87322009-01-16 19:25:27 +00002018/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002019/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002020SDValue
2021X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002022 SDValue &OutRetAddr, SDValue Chain,
2023 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002024 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002025 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002026 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002027 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002028
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002029 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002030 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002031 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002032 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002033}
2034
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002035/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002036/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002037static SDValue
2038EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002039 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002040 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002041 // Store the return address to the appropriate stack slot.
2042 if (!FPDiff) return Chain;
2043 // Calculate the new stack slot for the return address.
2044 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002045 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002046 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002047 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002048 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002049 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002050 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002051 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002052 return Chain;
2053}
2054
Dan Gohman98ca4f22009-08-05 01:29:28 +00002055SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002056X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002057 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002058 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002059 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002060 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002061 const SmallVectorImpl<ISD::InputArg> &Ins,
2062 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002063 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002064 MachineFunction &MF = DAG.getMachineFunction();
2065 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002066 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002067 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002068 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002069
Evan Cheng5f941932010-02-05 02:21:12 +00002070 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002071 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002072 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2073 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002074 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002075
2076 // Sibcalls are automatically detected tailcalls which do not require
2077 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002078 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002079 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002080
2081 if (isTailCall)
2082 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002083 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002084
Chris Lattner29689432010-03-11 00:22:57 +00002085 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2086 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002087
Chris Lattner638402b2007-02-28 07:00:42 +00002088 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002089 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002090 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002091 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002092
2093 // Allocate shadow area for Win64
2094 if (IsWin64) {
2095 CCInfo.AllocateStack(32, 8);
2096 }
2097
Duncan Sands45907662010-10-31 13:21:44 +00002098 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002099
Chris Lattner423c5f42007-02-28 05:31:48 +00002100 // Get a count of how many bytes are to be pushed on the stack.
2101 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002102 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002103 // This is a sibcall. The memory operands are available in caller's
2104 // own caller's stack.
2105 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002106 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002107 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002108
Gordon Henriksen86737662008-01-05 16:56:59 +00002109 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002110 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002111 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002112 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002113 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2114 FPDiff = NumBytesCallerPushed - NumBytes;
2115
2116 // Set the delta of movement of the returnaddr stackslot.
2117 // But only set if delta is greater than previous delta.
2118 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2119 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2120 }
2121
Evan Chengf22f9b32010-02-06 03:28:46 +00002122 if (!IsSibcall)
2123 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002124
Dan Gohman475871a2008-07-27 21:46:04 +00002125 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002126 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002127 if (isTailCall && FPDiff)
2128 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2129 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002130
Dan Gohman475871a2008-07-27 21:46:04 +00002131 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2132 SmallVector<SDValue, 8> MemOpChains;
2133 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002134
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002135 // Walk the register/memloc assignments, inserting copies/loads. In the case
2136 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002137 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2138 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002139 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002140 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002141 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002142 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002143
Chris Lattner423c5f42007-02-28 05:31:48 +00002144 // Promote the value if needed.
2145 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002146 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002147 case CCValAssign::Full: break;
2148 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002149 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002150 break;
2151 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002152 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002153 break;
2154 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002155 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2156 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002157 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002158 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2159 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002160 } else
2161 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2162 break;
2163 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002164 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002165 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002166 case CCValAssign::Indirect: {
2167 // Store the argument.
2168 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002169 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002170 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002171 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002172 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002173 Arg = SpillSlot;
2174 break;
2175 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002176 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002177
Chris Lattner423c5f42007-02-28 05:31:48 +00002178 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002179 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2180 if (isVarArg && IsWin64) {
2181 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2182 // shadow reg if callee is a varargs function.
2183 unsigned ShadowReg = 0;
2184 switch (VA.getLocReg()) {
2185 case X86::XMM0: ShadowReg = X86::RCX; break;
2186 case X86::XMM1: ShadowReg = X86::RDX; break;
2187 case X86::XMM2: ShadowReg = X86::R8; break;
2188 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002189 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002190 if (ShadowReg)
2191 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002192 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002193 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002194 assert(VA.isMemLoc());
2195 if (StackPtr.getNode() == 0)
2196 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2197 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2198 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002199 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002200 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002201
Evan Cheng32fe1032006-05-25 00:59:30 +00002202 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002203 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002204 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002205
Evan Cheng347d5f72006-04-28 21:29:37 +00002206 // Build a sequence of copy-to-reg nodes chained together with token chain
2207 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002208 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002209 // Tail call byval lowering might overwrite argument registers so in case of
2210 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002211 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002212 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002213 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002214 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002215 InFlag = Chain.getValue(1);
2216 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002217
Chris Lattner88e1fd52009-07-09 04:24:46 +00002218 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002219 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2220 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002221 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002222 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2223 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002224 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002225 InFlag);
2226 InFlag = Chain.getValue(1);
2227 } else {
2228 // If we are tail calling and generating PIC/GOT style code load the
2229 // address of the callee into ECX. The value in ecx is used as target of
2230 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2231 // for tail calls on PIC/GOT architectures. Normally we would just put the
2232 // address of GOT into ebx and then call target@PLT. But for tail calls
2233 // ebx would be restored (since ebx is callee saved) before jumping to the
2234 // target@PLT.
2235
2236 // Note: The actual moving to ECX is done further down.
2237 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2238 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2239 !G->getGlobal()->hasProtectedVisibility())
2240 Callee = LowerGlobalAddress(Callee, DAG);
2241 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002242 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002243 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002244 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002245
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002246 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002247 // From AMD64 ABI document:
2248 // For calls that may call functions that use varargs or stdargs
2249 // (prototype-less calls or calls to functions containing ellipsis (...) in
2250 // the declaration) %al is used as hidden argument to specify the number
2251 // of SSE registers used. The contents of %al do not need to match exactly
2252 // the number of registers, but must be an ubound on the number of SSE
2253 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002254
Gordon Henriksen86737662008-01-05 16:56:59 +00002255 // Count the number of XMM registers allocated.
2256 static const unsigned XMMArgRegs[] = {
2257 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2258 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2259 };
2260 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002261 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002262 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002263
Dale Johannesendd64c412009-02-04 00:33:20 +00002264 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002265 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002266 InFlag = Chain.getValue(1);
2267 }
2268
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002269
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002270 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002271 if (isTailCall) {
2272 // Force all the incoming stack arguments to be loaded from the stack
2273 // before any new outgoing arguments are stored to the stack, because the
2274 // outgoing stack slots may alias the incoming argument stack slots, and
2275 // the alias isn't otherwise explicit. This is slightly more conservative
2276 // than necessary, because it means that each store effectively depends
2277 // on every argument instead of just those arguments it would clobber.
2278 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2279
Dan Gohman475871a2008-07-27 21:46:04 +00002280 SmallVector<SDValue, 8> MemOpChains2;
2281 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002282 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002283 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002284 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002285 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002286 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2287 CCValAssign &VA = ArgLocs[i];
2288 if (VA.isRegLoc())
2289 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002290 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002291 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002292 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002293 // Create frame index.
2294 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002295 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002296 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002297 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002298
Duncan Sands276dcbd2008-03-21 09:14:45 +00002299 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002300 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002301 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002302 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002303 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002304 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002305 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002306
Dan Gohman98ca4f22009-08-05 01:29:28 +00002307 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2308 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002309 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002310 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002311 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002312 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002313 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002314 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002315 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002316 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002317 }
2318 }
2319
2320 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002321 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002322 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002323
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002324 // Copy arguments to their registers.
2325 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002326 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002327 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002328 InFlag = Chain.getValue(1);
2329 }
Dan Gohman475871a2008-07-27 21:46:04 +00002330 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002331
Gordon Henriksen86737662008-01-05 16:56:59 +00002332 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002333 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002334 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002335 }
2336
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002337 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2338 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2339 // In the 64-bit large code model, we have to make all calls
2340 // through a register, since the call instruction's 32-bit
2341 // pc-relative offset may not be large enough to hold the whole
2342 // address.
2343 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002344 // If the callee is a GlobalAddress node (quite common, every direct call
2345 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2346 // it.
2347
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002348 // We should use extra load for direct calls to dllimported functions in
2349 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002350 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002351 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002352 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002353 bool ExtraLoad = false;
2354 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002355
Chris Lattner48a7d022009-07-09 05:02:21 +00002356 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2357 // external symbols most go through the PLT in PIC mode. If the symbol
2358 // has hidden or protected visibility, or if it is static or local, then
2359 // we don't need to use the PLT - we can directly call it.
2360 if (Subtarget->isTargetELF() &&
2361 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002362 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002363 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002364 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002365 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002366 (!Subtarget->getTargetTriple().isMacOSX() ||
2367 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002368 // PC-relative references to external symbols should go through $stub,
2369 // unless we're building with the leopard linker or later, which
2370 // automatically synthesizes these stubs.
2371 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002372 } else if (Subtarget->isPICStyleRIPRel() &&
2373 isa<Function>(GV) &&
2374 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2375 // If the function is marked as non-lazy, generate an indirect call
2376 // which loads from the GOT directly. This avoids runtime overhead
2377 // at the cost of eager binding (and one extra byte of encoding).
2378 OpFlags = X86II::MO_GOTPCREL;
2379 WrapperKind = X86ISD::WrapperRIP;
2380 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002381 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002382
Devang Patel0d881da2010-07-06 22:08:15 +00002383 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002384 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002385
2386 // Add a wrapper if needed.
2387 if (WrapperKind != ISD::DELETED_NODE)
2388 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2389 // Add extra indirection if needed.
2390 if (ExtraLoad)
2391 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2392 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002393 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002394 }
Bill Wendling056292f2008-09-16 21:48:12 +00002395 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002396 unsigned char OpFlags = 0;
2397
Evan Cheng1bf891a2010-12-01 22:59:46 +00002398 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2399 // external symbols should go through the PLT.
2400 if (Subtarget->isTargetELF() &&
2401 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2402 OpFlags = X86II::MO_PLT;
2403 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002404 (!Subtarget->getTargetTriple().isMacOSX() ||
2405 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002406 // PC-relative references to external symbols should go through $stub,
2407 // unless we're building with the leopard linker or later, which
2408 // automatically synthesizes these stubs.
2409 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002410 }
Eric Christopherfd179292009-08-27 18:07:15 +00002411
Chris Lattner48a7d022009-07-09 05:02:21 +00002412 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2413 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002414 }
2415
Chris Lattnerd96d0722007-02-25 06:40:16 +00002416 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002417 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002418 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002419
Evan Chengf22f9b32010-02-06 03:28:46 +00002420 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002421 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2422 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002423 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002424 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002425
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002426 Ops.push_back(Chain);
2427 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002428
Dan Gohman98ca4f22009-08-05 01:29:28 +00002429 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002430 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002431
Gordon Henriksen86737662008-01-05 16:56:59 +00002432 // Add argument registers to the end of the list so that they are known live
2433 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002434 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2435 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2436 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002437
Evan Cheng586ccac2008-03-18 23:36:35 +00002438 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002439 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002440 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2441
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002442 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002443 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002444 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002445
Gabor Greifba36cb52008-08-28 21:40:38 +00002446 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002447 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002448
Dan Gohman98ca4f22009-08-05 01:29:28 +00002449 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002450 // We used to do:
2451 //// If this is the first return lowered for this function, add the regs
2452 //// to the liveout set for the function.
2453 // This isn't right, although it's probably harmless on x86; liveouts
2454 // should be computed from returns not tail calls. Consider a void
2455 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002456 return DAG.getNode(X86ISD::TC_RETURN, dl,
2457 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002458 }
2459
Dale Johannesenace16102009-02-03 19:33:06 +00002460 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002461 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002462
Chris Lattner2d297092006-05-23 18:50:38 +00002463 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002464 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002465 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002466 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002467 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002468 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002469 // pops the hidden struct pointer, so we have to push it back.
2470 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002471 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002472 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002473 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002474
Gordon Henriksenae636f82008-01-03 16:47:34 +00002475 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002476 if (!IsSibcall) {
2477 Chain = DAG.getCALLSEQ_END(Chain,
2478 DAG.getIntPtrConstant(NumBytes, true),
2479 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2480 true),
2481 InFlag);
2482 InFlag = Chain.getValue(1);
2483 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002484
Chris Lattner3085e152007-02-25 08:59:22 +00002485 // Handle result values, copying them out of physregs into vregs that we
2486 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002487 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2488 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002489}
2490
Evan Cheng25ab6902006-09-08 06:48:29 +00002491
2492//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002493// Fast Calling Convention (tail call) implementation
2494//===----------------------------------------------------------------------===//
2495
2496// Like std call, callee cleans arguments, convention except that ECX is
2497// reserved for storing the tail called function address. Only 2 registers are
2498// free for argument passing (inreg). Tail call optimization is performed
2499// provided:
2500// * tailcallopt is enabled
2501// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002502// On X86_64 architecture with GOT-style position independent code only local
2503// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002504// To keep the stack aligned according to platform abi the function
2505// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2506// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002507// If a tail called function callee has more arguments than the caller the
2508// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002509// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002510// original REtADDR, but before the saved framepointer or the spilled registers
2511// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2512// stack layout:
2513// arg1
2514// arg2
2515// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002516// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002517// move area ]
2518// (possible EBP)
2519// ESI
2520// EDI
2521// local1 ..
2522
2523/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2524/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002525unsigned
2526X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2527 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002528 MachineFunction &MF = DAG.getMachineFunction();
2529 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002530 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002531 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002532 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002533 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002534 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002535 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2536 // Number smaller than 12 so just add the difference.
2537 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2538 } else {
2539 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002540 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002541 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002542 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002543 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002544}
2545
Evan Cheng5f941932010-02-05 02:21:12 +00002546/// MatchingStackOffset - Return true if the given stack call argument is
2547/// already available in the same position (relatively) of the caller's
2548/// incoming argument stack.
2549static
2550bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2551 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2552 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002553 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2554 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002555 if (Arg.getOpcode() == ISD::CopyFromReg) {
2556 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002557 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002558 return false;
2559 MachineInstr *Def = MRI->getVRegDef(VR);
2560 if (!Def)
2561 return false;
2562 if (!Flags.isByVal()) {
2563 if (!TII->isLoadFromStackSlot(Def, FI))
2564 return false;
2565 } else {
2566 unsigned Opcode = Def->getOpcode();
2567 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2568 Def->getOperand(1).isFI()) {
2569 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002570 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002571 } else
2572 return false;
2573 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002574 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2575 if (Flags.isByVal())
2576 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002577 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002578 // define @foo(%struct.X* %A) {
2579 // tail call @bar(%struct.X* byval %A)
2580 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002581 return false;
2582 SDValue Ptr = Ld->getBasePtr();
2583 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2584 if (!FINode)
2585 return false;
2586 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002587 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002588 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002589 FI = FINode->getIndex();
2590 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002591 } else
2592 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002593
Evan Cheng4cae1332010-03-05 08:38:04 +00002594 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002595 if (!MFI->isFixedObjectIndex(FI))
2596 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002597 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002598}
2599
Dan Gohman98ca4f22009-08-05 01:29:28 +00002600/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2601/// for tail call optimization. Targets which want to do tail call
2602/// optimization should implement this function.
2603bool
2604X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002605 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002606 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002607 bool isCalleeStructRet,
2608 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002609 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002610 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002611 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002612 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002613 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002614 CalleeCC != CallingConv::C)
2615 return false;
2616
Evan Cheng7096ae42010-01-29 06:45:59 +00002617 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002618 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002619 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002620 CallingConv::ID CallerCC = CallerF->getCallingConv();
2621 bool CCMatch = CallerCC == CalleeCC;
2622
Dan Gohman1797ed52010-02-08 20:27:50 +00002623 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002624 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002625 return true;
2626 return false;
2627 }
2628
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002629 // Look for obvious safe cases to perform tail call optimization that do not
2630 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002631
Evan Cheng2c12cb42010-03-26 16:26:03 +00002632 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2633 // emit a special epilogue.
2634 if (RegInfo->needsStackRealignment(MF))
2635 return false;
2636
Evan Chenga375d472010-03-15 18:54:48 +00002637 // Also avoid sibcall optimization if either caller or callee uses struct
2638 // return semantics.
2639 if (isCalleeStructRet || isCallerStructRet)
2640 return false;
2641
Chad Rosier2416da32011-06-24 21:15:36 +00002642 // An stdcall caller is expected to clean up its arguments; the callee
2643 // isn't going to do that.
2644 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2645 return false;
2646
Chad Rosier871f6642011-05-18 19:59:50 +00002647 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002648 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002649 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002650
2651 // Optimizing for varargs on Win64 is unlikely to be safe without
2652 // additional testing.
2653 if (Subtarget->isTargetWin64())
2654 return false;
2655
Chad Rosier871f6642011-05-18 19:59:50 +00002656 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002657 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2658 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002659
Chad Rosier871f6642011-05-18 19:59:50 +00002660 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2661 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2662 if (!ArgLocs[i].isRegLoc())
2663 return false;
2664 }
2665
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002666 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2667 // Therefore if it's not used by the call it is not safe to optimize this into
2668 // a sibcall.
2669 bool Unused = false;
2670 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2671 if (!Ins[i].Used) {
2672 Unused = true;
2673 break;
2674 }
2675 }
2676 if (Unused) {
2677 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002678 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2679 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002680 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002681 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002682 CCValAssign &VA = RVLocs[i];
2683 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2684 return false;
2685 }
2686 }
2687
Evan Cheng13617962010-04-30 01:12:32 +00002688 // If the calling conventions do not match, then we'd better make sure the
2689 // results are returned in the same way as what the caller expects.
2690 if (!CCMatch) {
2691 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002692 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2693 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002694 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2695
2696 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002697 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2698 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002699 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2700
2701 if (RVLocs1.size() != RVLocs2.size())
2702 return false;
2703 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2704 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2705 return false;
2706 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2707 return false;
2708 if (RVLocs1[i].isRegLoc()) {
2709 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2710 return false;
2711 } else {
2712 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2713 return false;
2714 }
2715 }
2716 }
2717
Evan Chenga6bff982010-01-30 01:22:00 +00002718 // If the callee takes no arguments then go on to check the results of the
2719 // call.
2720 if (!Outs.empty()) {
2721 // Check if stack adjustment is needed. For now, do not do this if any
2722 // argument is passed on the stack.
2723 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002724 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2725 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002726
2727 // Allocate shadow area for Win64
2728 if (Subtarget->isTargetWin64()) {
2729 CCInfo.AllocateStack(32, 8);
2730 }
2731
Duncan Sands45907662010-10-31 13:21:44 +00002732 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002733 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002734 MachineFunction &MF = DAG.getMachineFunction();
2735 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2736 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002737
2738 // Check if the arguments are already laid out in the right way as
2739 // the caller's fixed stack objects.
2740 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002741 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2742 const X86InstrInfo *TII =
2743 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002744 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2745 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002746 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002747 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002748 if (VA.getLocInfo() == CCValAssign::Indirect)
2749 return false;
2750 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002751 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2752 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002753 return false;
2754 }
2755 }
2756 }
Evan Cheng9c044672010-05-29 01:35:22 +00002757
2758 // If the tailcall address may be in a register, then make sure it's
2759 // possible to register allocate for it. In 32-bit, the call address can
2760 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002761 // callee-saved registers are restored. These happen to be the same
2762 // registers used to pass 'inreg' arguments so watch out for those.
2763 if (!Subtarget->is64Bit() &&
2764 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002765 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002766 unsigned NumInRegs = 0;
2767 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2768 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002769 if (!VA.isRegLoc())
2770 continue;
2771 unsigned Reg = VA.getLocReg();
2772 switch (Reg) {
2773 default: break;
2774 case X86::EAX: case X86::EDX: case X86::ECX:
2775 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002776 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002777 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002778 }
2779 }
2780 }
Evan Chenga6bff982010-01-30 01:22:00 +00002781 }
Evan Chengb1712452010-01-27 06:25:16 +00002782
Evan Cheng86809cc2010-02-03 03:28:02 +00002783 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002784}
2785
Dan Gohman3df24e62008-09-03 23:12:08 +00002786FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002787X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2788 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002789}
2790
2791
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002792//===----------------------------------------------------------------------===//
2793// Other Lowering Hooks
2794//===----------------------------------------------------------------------===//
2795
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002796static bool MayFoldLoad(SDValue Op) {
2797 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2798}
2799
2800static bool MayFoldIntoStore(SDValue Op) {
2801 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2802}
2803
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002804static bool isTargetShuffle(unsigned Opcode) {
2805 switch(Opcode) {
2806 default: return false;
2807 case X86ISD::PSHUFD:
2808 case X86ISD::PSHUFHW:
2809 case X86ISD::PSHUFLW:
2810 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002811 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002812 case X86ISD::SHUFPS:
2813 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002814 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002815 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002816 case X86ISD::MOVLPS:
2817 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002818 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002819 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002820 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002821 case X86ISD::MOVSS:
2822 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002823 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002824 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002825 case X86ISD::VUNPCKLPSY:
2826 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002827 case X86ISD::PUNPCKLWD:
2828 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002829 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002830 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002831 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002832 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002833 case X86ISD::VUNPCKHPSY:
2834 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002835 case X86ISD::PUNPCKHWD:
2836 case X86ISD::PUNPCKHBW:
2837 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002838 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002839 case X86ISD::VPERMILPS:
2840 case X86ISD::VPERMILPSY:
2841 case X86ISD::VPERMILPD:
2842 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002843 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002844 return true;
2845 }
2846 return false;
2847}
2848
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002849static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002850 SDValue V1, SelectionDAG &DAG) {
2851 switch(Opc) {
2852 default: llvm_unreachable("Unknown x86 shuffle node");
2853 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002854 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002855 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002856 return DAG.getNode(Opc, dl, VT, V1);
2857 }
2858
2859 return SDValue();
2860}
2861
2862static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002863 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002864 switch(Opc) {
2865 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002866 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002867 case X86ISD::PSHUFHW:
2868 case X86ISD::PSHUFLW:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002869 case X86ISD::VPERMILPS:
2870 case X86ISD::VPERMILPSY:
2871 case X86ISD::VPERMILPD:
2872 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002873 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2874 }
2875
2876 return SDValue();
2877}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002878
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002879static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2880 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2881 switch(Opc) {
2882 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002883 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002884 case X86ISD::SHUFPD:
2885 case X86ISD::SHUFPS:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002886 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002887 return DAG.getNode(Opc, dl, VT, V1, V2,
2888 DAG.getConstant(TargetMask, MVT::i8));
2889 }
2890 return SDValue();
2891}
2892
2893static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2894 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2895 switch(Opc) {
2896 default: llvm_unreachable("Unknown x86 shuffle node");
2897 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002898 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002899 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002900 case X86ISD::MOVLPS:
2901 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002902 case X86ISD::MOVSS:
2903 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002904 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002905 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002906 case X86ISD::VUNPCKLPSY:
2907 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002908 case X86ISD::PUNPCKLWD:
2909 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002910 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002911 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002912 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002913 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002914 case X86ISD::VUNPCKHPSY:
2915 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002916 case X86ISD::PUNPCKHWD:
2917 case X86ISD::PUNPCKHBW:
2918 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002919 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002920 return DAG.getNode(Opc, dl, VT, V1, V2);
2921 }
2922 return SDValue();
2923}
2924
Dan Gohmand858e902010-04-17 15:26:15 +00002925SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002926 MachineFunction &MF = DAG.getMachineFunction();
2927 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2928 int ReturnAddrIndex = FuncInfo->getRAIndex();
2929
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002930 if (ReturnAddrIndex == 0) {
2931 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002932 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002933 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002934 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002935 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002936 }
2937
Evan Cheng25ab6902006-09-08 06:48:29 +00002938 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002939}
2940
2941
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002942bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2943 bool hasSymbolicDisplacement) {
2944 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002945 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002946 return false;
2947
2948 // If we don't have a symbolic displacement - we don't have any extra
2949 // restrictions.
2950 if (!hasSymbolicDisplacement)
2951 return true;
2952
2953 // FIXME: Some tweaks might be needed for medium code model.
2954 if (M != CodeModel::Small && M != CodeModel::Kernel)
2955 return false;
2956
2957 // For small code model we assume that latest object is 16MB before end of 31
2958 // bits boundary. We may also accept pretty large negative constants knowing
2959 // that all objects are in the positive half of address space.
2960 if (M == CodeModel::Small && Offset < 16*1024*1024)
2961 return true;
2962
2963 // For kernel code model we know that all object resist in the negative half
2964 // of 32bits address space. We may not accept negative offsets, since they may
2965 // be just off and we may accept pretty large positive ones.
2966 if (M == CodeModel::Kernel && Offset > 0)
2967 return true;
2968
2969 return false;
2970}
2971
Evan Chengef41ff62011-06-23 17:54:54 +00002972/// isCalleePop - Determines whether the callee is required to pop its
2973/// own arguments. Callee pop is necessary to support tail calls.
2974bool X86::isCalleePop(CallingConv::ID CallingConv,
2975 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2976 if (IsVarArg)
2977 return false;
2978
2979 switch (CallingConv) {
2980 default:
2981 return false;
2982 case CallingConv::X86_StdCall:
2983 return !is64Bit;
2984 case CallingConv::X86_FastCall:
2985 return !is64Bit;
2986 case CallingConv::X86_ThisCall:
2987 return !is64Bit;
2988 case CallingConv::Fast:
2989 return TailCallOpt;
2990 case CallingConv::GHC:
2991 return TailCallOpt;
2992 }
2993}
2994
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002995/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2996/// specific condition code, returning the condition code and the LHS/RHS of the
2997/// comparison to make.
2998static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2999 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003000 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003001 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3002 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3003 // X > -1 -> X == 0, jump !sign.
3004 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003005 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003006 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
3007 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003008 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00003009 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003010 // X < 1 -> X <= 0
3011 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003012 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003013 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003014 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003015
Evan Chengd9558e02006-01-06 00:43:03 +00003016 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003017 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003018 case ISD::SETEQ: return X86::COND_E;
3019 case ISD::SETGT: return X86::COND_G;
3020 case ISD::SETGE: return X86::COND_GE;
3021 case ISD::SETLT: return X86::COND_L;
3022 case ISD::SETLE: return X86::COND_LE;
3023 case ISD::SETNE: return X86::COND_NE;
3024 case ISD::SETULT: return X86::COND_B;
3025 case ISD::SETUGT: return X86::COND_A;
3026 case ISD::SETULE: return X86::COND_BE;
3027 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003028 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003029 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003030
Chris Lattner4c78e022008-12-23 23:42:27 +00003031 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003032
Chris Lattner4c78e022008-12-23 23:42:27 +00003033 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003034 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3035 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003036 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3037 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003038 }
3039
Chris Lattner4c78e022008-12-23 23:42:27 +00003040 switch (SetCCOpcode) {
3041 default: break;
3042 case ISD::SETOLT:
3043 case ISD::SETOLE:
3044 case ISD::SETUGT:
3045 case ISD::SETUGE:
3046 std::swap(LHS, RHS);
3047 break;
3048 }
3049
3050 // On a floating point condition, the flags are set as follows:
3051 // ZF PF CF op
3052 // 0 | 0 | 0 | X > Y
3053 // 0 | 0 | 1 | X < Y
3054 // 1 | 0 | 0 | X == Y
3055 // 1 | 1 | 1 | unordered
3056 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003057 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003058 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003059 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003060 case ISD::SETOLT: // flipped
3061 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003062 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003063 case ISD::SETOLE: // flipped
3064 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003065 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003066 case ISD::SETUGT: // flipped
3067 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003068 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003069 case ISD::SETUGE: // flipped
3070 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003071 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003072 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003073 case ISD::SETNE: return X86::COND_NE;
3074 case ISD::SETUO: return X86::COND_P;
3075 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003076 case ISD::SETOEQ:
3077 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003078 }
Evan Chengd9558e02006-01-06 00:43:03 +00003079}
3080
Evan Cheng4a460802006-01-11 00:33:36 +00003081/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3082/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003083/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003084static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003085 switch (X86CC) {
3086 default:
3087 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003088 case X86::COND_B:
3089 case X86::COND_BE:
3090 case X86::COND_E:
3091 case X86::COND_P:
3092 case X86::COND_A:
3093 case X86::COND_AE:
3094 case X86::COND_NE:
3095 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003096 return true;
3097 }
3098}
3099
Evan Chengeb2f9692009-10-27 19:56:55 +00003100/// isFPImmLegal - Returns true if the target can instruction select the
3101/// specified FP immediate natively. If false, the legalizer will
3102/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003103bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003104 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3105 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3106 return true;
3107 }
3108 return false;
3109}
3110
Nate Begeman9008ca62009-04-27 18:41:29 +00003111/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3112/// the specified range (L, H].
3113static bool isUndefOrInRange(int Val, int Low, int Hi) {
3114 return (Val < 0) || (Val >= Low && Val < Hi);
3115}
3116
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003117/// isUndefOrInRange - Return true if every element in Mask, begining
3118/// from position Pos and ending in Pos+Size, falls within the specified
3119/// range (L, L+Pos]. or is undef.
3120static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3121 int Pos, int Size, int Low, int Hi) {
3122 for (int i = Pos, e = Pos+Size; i != e; ++i)
3123 if (!isUndefOrInRange(Mask[i], Low, Hi))
3124 return false;
3125 return true;
3126}
3127
Nate Begeman9008ca62009-04-27 18:41:29 +00003128/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3129/// specified value.
3130static bool isUndefOrEqual(int Val, int CmpVal) {
3131 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003132 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003133 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003134}
3135
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003136/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3137/// from position Pos and ending in Pos+Size, falls within the specified
3138/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003139static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3140 int Pos, int Size, int Low) {
3141 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3142 if (!isUndefOrEqual(Mask[i], Low))
3143 return false;
3144 return true;
3145}
3146
Nate Begeman9008ca62009-04-27 18:41:29 +00003147/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3148/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3149/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003150static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003151 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003152 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003153 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003154 return (Mask[0] < 2 && Mask[1] < 2);
3155 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003156}
3157
Nate Begeman9008ca62009-04-27 18:41:29 +00003158bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003159 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003160 N->getMask(M);
3161 return ::isPSHUFDMask(M, N->getValueType(0));
3162}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003163
Nate Begeman9008ca62009-04-27 18:41:29 +00003164/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3165/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003166static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003167 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003168 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003169
Nate Begeman9008ca62009-04-27 18:41:29 +00003170 // Lower quadword copied in order or undef.
3171 for (int i = 0; i != 4; ++i)
3172 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003173 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003174
Evan Cheng506d3df2006-03-29 23:07:14 +00003175 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003176 for (int i = 4; i != 8; ++i)
3177 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003178 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003179
Evan Cheng506d3df2006-03-29 23:07:14 +00003180 return true;
3181}
3182
Nate Begeman9008ca62009-04-27 18:41:29 +00003183bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003184 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003185 N->getMask(M);
3186 return ::isPSHUFHWMask(M, N->getValueType(0));
3187}
Evan Cheng506d3df2006-03-29 23:07:14 +00003188
Nate Begeman9008ca62009-04-27 18:41:29 +00003189/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3190/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003191static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003192 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003193 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003194
Rafael Espindola15684b22009-04-24 12:40:33 +00003195 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003196 for (int i = 4; i != 8; ++i)
3197 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003198 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003199
Rafael Espindola15684b22009-04-24 12:40:33 +00003200 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003201 for (int i = 0; i != 4; ++i)
3202 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003203 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003204
Rafael Espindola15684b22009-04-24 12:40:33 +00003205 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003206}
3207
Nate Begeman9008ca62009-04-27 18:41:29 +00003208bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003209 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003210 N->getMask(M);
3211 return ::isPSHUFLWMask(M, N->getValueType(0));
3212}
3213
Nate Begemana09008b2009-10-19 02:17:23 +00003214/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3215/// is suitable for input to PALIGNR.
3216static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003217 bool hasSSSE3OrAVX) {
Nate Begemana09008b2009-10-19 02:17:23 +00003218 int i, e = VT.getVectorNumElements();
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003219 if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
3220 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003221
Nate Begemana09008b2009-10-19 02:17:23 +00003222 // Do not handle v2i64 / v2f64 shuffles with palignr.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003223 if (e < 4 || !hasSSSE3OrAVX)
Nate Begemana09008b2009-10-19 02:17:23 +00003224 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003225
Nate Begemana09008b2009-10-19 02:17:23 +00003226 for (i = 0; i != e; ++i)
3227 if (Mask[i] >= 0)
3228 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003229
Nate Begemana09008b2009-10-19 02:17:23 +00003230 // All undef, not a palignr.
3231 if (i == e)
3232 return false;
3233
Eli Friedman63f8dde2011-07-25 21:36:45 +00003234 // Make sure we're shifting in the right direction.
3235 if (Mask[i] <= i)
3236 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003237
3238 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003239
Nate Begemana09008b2009-10-19 02:17:23 +00003240 // Check the rest of the elements to see if they are consecutive.
3241 for (++i; i != e; ++i) {
3242 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003243 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003244 return false;
3245 }
3246 return true;
3247}
3248
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003249/// isVSHUFPSYMask - Return true if the specified VECTOR_SHUFFLE operand
3250/// specifies a shuffle of elements that is suitable for input to 256-bit
3251/// VSHUFPSY.
3252static bool isVSHUFPSYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3253 const X86Subtarget *Subtarget) {
3254 int NumElems = VT.getVectorNumElements();
3255
3256 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3257 return false;
3258
3259 if (NumElems != 8)
3260 return false;
3261
3262 // VSHUFPSY divides the resulting vector into 4 chunks.
3263 // The sources are also splitted into 4 chunks, and each destination
3264 // chunk must come from a different source chunk.
3265 //
3266 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3267 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3268 //
3269 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3270 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3271 //
3272 int QuarterSize = NumElems/4;
3273 int HalfSize = QuarterSize*2;
3274 for (int i = 0; i < QuarterSize; ++i)
3275 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3276 return false;
3277 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3278 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3279 return false;
3280
3281 // The mask of the second half must be the same as the first but with
3282 // the appropriate offsets. This works in the same way as VPERMILPS
3283 // works with masks.
3284 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3285 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3286 return false;
3287 int FstHalfIdx = i-HalfSize;
3288 if (Mask[FstHalfIdx] < 0)
3289 continue;
3290 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3291 return false;
3292 }
3293 for (int i = QuarterSize*3; i < NumElems; ++i) {
3294 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3295 return false;
3296 int FstHalfIdx = i-HalfSize;
3297 if (Mask[FstHalfIdx] < 0)
3298 continue;
3299 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3300 return false;
3301
3302 }
3303
3304 return true;
3305}
3306
3307/// getShuffleVSHUFPSYImmediate - Return the appropriate immediate to shuffle
3308/// the specified VECTOR_MASK mask with VSHUFPSY instruction.
3309static unsigned getShuffleVSHUFPSYImmediate(SDNode *N) {
3310 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3311 EVT VT = SVOp->getValueType(0);
3312 int NumElems = VT.getVectorNumElements();
3313
3314 assert(NumElems == 8 && VT.getSizeInBits() == 256 &&
3315 "Only supports v8i32 and v8f32 types");
3316
3317 int HalfSize = NumElems/2;
3318 unsigned Mask = 0;
3319 for (int i = 0; i != NumElems ; ++i) {
3320 if (SVOp->getMaskElt(i) < 0)
3321 continue;
3322 // The mask of the first half must be equal to the second one.
3323 unsigned Shamt = (i%HalfSize)*2;
3324 unsigned Elt = SVOp->getMaskElt(i) % HalfSize;
3325 Mask |= Elt << Shamt;
3326 }
3327
3328 return Mask;
3329}
3330
3331/// isVSHUFPDYMask - Return true if the specified VECTOR_SHUFFLE operand
3332/// specifies a shuffle of elements that is suitable for input to 256-bit
3333/// VSHUFPDY. This shuffle doesn't have the same restriction as the PS
3334/// version and the mask of the second half isn't binded with the first
3335/// one.
3336static bool isVSHUFPDYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3337 const X86Subtarget *Subtarget) {
3338 int NumElems = VT.getVectorNumElements();
3339
3340 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3341 return false;
3342
3343 if (NumElems != 4)
3344 return false;
3345
3346 // VSHUFPSY divides the resulting vector into 4 chunks.
3347 // The sources are also splitted into 4 chunks, and each destination
3348 // chunk must come from a different source chunk.
3349 //
3350 // SRC1 => X3 X2 X1 X0
3351 // SRC2 => Y3 Y2 Y1 Y0
3352 //
3353 // DST => Y2..Y3, X2..X3, Y1..Y0, X1..X0
3354 //
3355 int QuarterSize = NumElems/4;
3356 int HalfSize = QuarterSize*2;
3357 for (int i = 0; i < QuarterSize; ++i)
3358 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3359 return false;
3360 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3361 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3362 return false;
3363 for (int i = QuarterSize*2; i < QuarterSize*3; ++i)
3364 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3365 return false;
3366 for (int i = QuarterSize*3; i < NumElems; ++i)
3367 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3368 return false;
3369
3370 return true;
3371}
3372
3373/// getShuffleVSHUFPDYImmediate - Return the appropriate immediate to shuffle
3374/// the specified VECTOR_MASK mask with VSHUFPDY instruction.
3375static unsigned getShuffleVSHUFPDYImmediate(SDNode *N) {
3376 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3377 EVT VT = SVOp->getValueType(0);
3378 int NumElems = VT.getVectorNumElements();
3379
3380 assert(NumElems == 4 && VT.getSizeInBits() == 256 &&
3381 "Only supports v4i64 and v4f64 types");
3382
3383 int HalfSize = NumElems/2;
3384 unsigned Mask = 0;
3385 for (int i = 0; i != NumElems ; ++i) {
3386 if (SVOp->getMaskElt(i) < 0)
3387 continue;
3388 int Elt = SVOp->getMaskElt(i) % HalfSize;
3389 Mask |= Elt << i;
3390 }
3391
3392 return Mask;
3393}
3394
Evan Cheng14aed5e2006-03-24 01:18:28 +00003395/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003396/// specifies a shuffle of elements that is suitable for input to 128-bit
3397/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003398static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003399 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003400
3401 if (VT.getSizeInBits() != 128)
3402 return false;
3403
Nate Begeman9008ca62009-04-27 18:41:29 +00003404 if (NumElems != 2 && NumElems != 4)
3405 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003406
Nate Begeman9008ca62009-04-27 18:41:29 +00003407 int Half = NumElems / 2;
3408 for (int i = 0; i < Half; ++i)
3409 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003410 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003411 for (int i = Half; i < NumElems; ++i)
3412 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003413 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003414
Evan Cheng14aed5e2006-03-24 01:18:28 +00003415 return true;
3416}
3417
Nate Begeman9008ca62009-04-27 18:41:29 +00003418bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3419 SmallVector<int, 8> M;
3420 N->getMask(M);
3421 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003422}
3423
Evan Cheng213d2cf2007-05-17 18:45:50 +00003424/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003425/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3426/// half elements to come from vector 1 (which would equal the dest.) and
3427/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003428static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003429 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003430
3431 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003432 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003433
Nate Begeman9008ca62009-04-27 18:41:29 +00003434 int Half = NumElems / 2;
3435 for (int i = 0; i < Half; ++i)
3436 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003437 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003438 for (int i = Half; i < NumElems; ++i)
3439 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003440 return false;
3441 return true;
3442}
3443
Nate Begeman9008ca62009-04-27 18:41:29 +00003444static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3445 SmallVector<int, 8> M;
3446 N->getMask(M);
3447 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003448}
3449
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003450/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3451/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003452bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003453 EVT VT = N->getValueType(0);
3454 unsigned NumElems = VT.getVectorNumElements();
3455
3456 if (VT.getSizeInBits() != 128)
3457 return false;
3458
3459 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003460 return false;
3461
Evan Cheng2064a2b2006-03-28 06:50:32 +00003462 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003463 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3464 isUndefOrEqual(N->getMaskElt(1), 7) &&
3465 isUndefOrEqual(N->getMaskElt(2), 2) &&
3466 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003467}
3468
Nate Begeman0b10b912009-11-07 23:17:15 +00003469/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3470/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3471/// <2, 3, 2, 3>
3472bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003473 EVT VT = N->getValueType(0);
3474 unsigned NumElems = VT.getVectorNumElements();
3475
3476 if (VT.getSizeInBits() != 128)
3477 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003478
Nate Begeman0b10b912009-11-07 23:17:15 +00003479 if (NumElems != 4)
3480 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003481
Nate Begeman0b10b912009-11-07 23:17:15 +00003482 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003483 isUndefOrEqual(N->getMaskElt(1), 3) &&
3484 isUndefOrEqual(N->getMaskElt(2), 2) &&
3485 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003486}
3487
Evan Cheng5ced1d82006-04-06 23:23:56 +00003488/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3489/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003490bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3491 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003492
Evan Cheng5ced1d82006-04-06 23:23:56 +00003493 if (NumElems != 2 && NumElems != 4)
3494 return false;
3495
Evan Chengc5cdff22006-04-07 21:53:05 +00003496 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003497 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003498 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003499
Evan Chengc5cdff22006-04-07 21:53:05 +00003500 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003501 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003502 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003503
3504 return true;
3505}
3506
Nate Begeman0b10b912009-11-07 23:17:15 +00003507/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3508/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3509bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003510 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003511
David Greenea20244d2011-03-02 17:23:43 +00003512 if ((NumElems != 2 && NumElems != 4)
3513 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003514 return false;
3515
Evan Chengc5cdff22006-04-07 21:53:05 +00003516 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003517 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003518 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003519
Nate Begeman9008ca62009-04-27 18:41:29 +00003520 for (unsigned i = 0; i < NumElems/2; ++i)
3521 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003522 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003523
3524 return true;
3525}
3526
Evan Cheng0038e592006-03-28 00:39:58 +00003527/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3528/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003529static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003530 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003531 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003532
3533 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3534 "Unsupported vector type for unpckh");
3535
3536 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng0038e592006-03-28 00:39:58 +00003537 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003538
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003539 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3540 // independently on 128-bit lanes.
3541 unsigned NumLanes = VT.getSizeInBits()/128;
3542 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003543
3544 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003545 unsigned End = NumLaneElts;
3546 for (unsigned s = 0; s < NumLanes; ++s) {
3547 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003548 i != End;
3549 i += 2, ++j) {
3550 int BitI = Mask[i];
3551 int BitI1 = Mask[i+1];
3552 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003553 return false;
David Greenea20244d2011-03-02 17:23:43 +00003554 if (V2IsSplat) {
3555 if (!isUndefOrEqual(BitI1, NumElts))
3556 return false;
3557 } else {
3558 if (!isUndefOrEqual(BitI1, j + NumElts))
3559 return false;
3560 }
Evan Cheng39623da2006-04-20 08:58:49 +00003561 }
David Greenea20244d2011-03-02 17:23:43 +00003562 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003563 Start += NumLaneElts;
3564 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003565 }
David Greenea20244d2011-03-02 17:23:43 +00003566
Evan Cheng0038e592006-03-28 00:39:58 +00003567 return true;
3568}
3569
Nate Begeman9008ca62009-04-27 18:41:29 +00003570bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3571 SmallVector<int, 8> M;
3572 N->getMask(M);
3573 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003574}
3575
Evan Cheng4fcb9222006-03-28 02:43:26 +00003576/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3577/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003578static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003579 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003580 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003581
3582 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3583 "Unsupported vector type for unpckh");
3584
3585 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003586 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003587
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003588 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3589 // independently on 128-bit lanes.
3590 unsigned NumLanes = VT.getSizeInBits()/128;
3591 unsigned NumLaneElts = NumElts/NumLanes;
3592
3593 unsigned Start = 0;
3594 unsigned End = NumLaneElts;
3595 for (unsigned l = 0; l != NumLanes; ++l) {
3596 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3597 i != End; i += 2, ++j) {
3598 int BitI = Mask[i];
3599 int BitI1 = Mask[i+1];
3600 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003601 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003602 if (V2IsSplat) {
3603 if (isUndefOrEqual(BitI1, NumElts))
3604 return false;
3605 } else {
3606 if (!isUndefOrEqual(BitI1, j+NumElts))
3607 return false;
3608 }
Evan Cheng39623da2006-04-20 08:58:49 +00003609 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003610 // Process the next 128 bits.
3611 Start += NumLaneElts;
3612 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003613 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003614 return true;
3615}
3616
Nate Begeman9008ca62009-04-27 18:41:29 +00003617bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3618 SmallVector<int, 8> M;
3619 N->getMask(M);
3620 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003621}
3622
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003623/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3624/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3625/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003626static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003627 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003628 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003629 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003630
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003631 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3632 // FIXME: Need a better way to get rid of this, there's no latency difference
3633 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3634 // the former later. We should also remove the "_undef" special mask.
3635 if (NumElems == 4 && VT.getSizeInBits() == 256)
3636 return false;
3637
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003638 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3639 // independently on 128-bit lanes.
3640 unsigned NumLanes = VT.getSizeInBits() / 128;
3641 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003642
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003643 for (unsigned s = 0; s < NumLanes; ++s) {
3644 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3645 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003646 i += 2, ++j) {
3647 int BitI = Mask[i];
3648 int BitI1 = Mask[i+1];
3649
3650 if (!isUndefOrEqual(BitI, j))
3651 return false;
3652 if (!isUndefOrEqual(BitI1, j))
3653 return false;
3654 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003655 }
David Greenea20244d2011-03-02 17:23:43 +00003656
Rafael Espindola15684b22009-04-24 12:40:33 +00003657 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003658}
3659
Nate Begeman9008ca62009-04-27 18:41:29 +00003660bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3661 SmallVector<int, 8> M;
3662 N->getMask(M);
3663 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3664}
3665
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003666/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3667/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3668/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003669static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003670 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003671 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3672 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003673
Nate Begeman9008ca62009-04-27 18:41:29 +00003674 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3675 int BitI = Mask[i];
3676 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003677 if (!isUndefOrEqual(BitI, j))
3678 return false;
3679 if (!isUndefOrEqual(BitI1, j))
3680 return false;
3681 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003682 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003683}
3684
Nate Begeman9008ca62009-04-27 18:41:29 +00003685bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3686 SmallVector<int, 8> M;
3687 N->getMask(M);
3688 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3689}
3690
Evan Cheng017dcc62006-04-21 01:05:10 +00003691/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3692/// specifies a shuffle of elements that is suitable for input to MOVSS,
3693/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003694static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003695 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003696 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003697
3698 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003699
Nate Begeman9008ca62009-04-27 18:41:29 +00003700 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003701 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003702
Nate Begeman9008ca62009-04-27 18:41:29 +00003703 for (int i = 1; i < NumElts; ++i)
3704 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003705 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003706
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003707 return true;
3708}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003709
Nate Begeman9008ca62009-04-27 18:41:29 +00003710bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3711 SmallVector<int, 8> M;
3712 N->getMask(M);
3713 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003714}
3715
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003716/// isVPERM2F128Mask - Match 256-bit shuffles where the elements are considered
3717/// as permutations between 128-bit chunks or halves. As an example: this
3718/// shuffle bellow:
3719/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3720/// The first half comes from the second half of V1 and the second half from the
3721/// the second half of V2.
3722static bool isVPERM2F128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3723 const X86Subtarget *Subtarget) {
3724 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3725 return false;
3726
3727 // The shuffle result is divided into half A and half B. In total the two
3728 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3729 // B must come from C, D, E or F.
3730 int HalfSize = VT.getVectorNumElements()/2;
3731 bool MatchA = false, MatchB = false;
3732
3733 // Check if A comes from one of C, D, E, F.
3734 for (int Half = 0; Half < 4; ++Half) {
3735 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3736 MatchA = true;
3737 break;
3738 }
3739 }
3740
3741 // Check if B comes from one of C, D, E, F.
3742 for (int Half = 0; Half < 4; ++Half) {
3743 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3744 MatchB = true;
3745 break;
3746 }
3747 }
3748
3749 return MatchA && MatchB;
3750}
3751
3752/// getShuffleVPERM2F128Immediate - Return the appropriate immediate to shuffle
3753/// the specified VECTOR_MASK mask with VPERM2F128 instructions.
3754static unsigned getShuffleVPERM2F128Immediate(SDNode *N) {
3755 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3756 EVT VT = SVOp->getValueType(0);
3757
3758 int HalfSize = VT.getVectorNumElements()/2;
3759
3760 int FstHalf = 0, SndHalf = 0;
3761 for (int i = 0; i < HalfSize; ++i) {
3762 if (SVOp->getMaskElt(i) > 0) {
3763 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3764 break;
3765 }
3766 }
3767 for (int i = HalfSize; i < HalfSize*2; ++i) {
3768 if (SVOp->getMaskElt(i) > 0) {
3769 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3770 break;
3771 }
3772 }
3773
3774 return (FstHalf | (SndHalf << 4));
3775}
3776
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003777/// isVPERMILPDMask - Return true if the specified VECTOR_SHUFFLE operand
3778/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3779/// Note that VPERMIL mask matching is different depending whether theunderlying
3780/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3781/// to the same elements of the low, but to the higher half of the source.
3782/// In VPERMILPD the two lanes could be shuffled independently of each other
3783/// with the same restriction that lanes can't be crossed.
3784static bool isVPERMILPDMask(const SmallVectorImpl<int> &Mask, EVT VT,
3785 const X86Subtarget *Subtarget) {
3786 int NumElts = VT.getVectorNumElements();
3787 int NumLanes = VT.getSizeInBits()/128;
3788
3789 if (!Subtarget->hasAVX())
3790 return false;
3791
Eli Friedmandca62d52011-10-10 22:28:47 +00003792 // Only match 256-bit with 64-bit types
3793 if (VT.getSizeInBits() != 256 || NumElts != 4)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003794 return false;
3795
3796 // The mask on the high lane is independent of the low. Both can match
3797 // any element in inside its own lane, but can't cross.
3798 int LaneSize = NumElts/NumLanes;
3799 for (int l = 0; l < NumLanes; ++l)
3800 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3801 int LaneStart = l*LaneSize;
3802 if (!isUndefOrInRange(Mask[i], LaneStart, LaneStart+LaneSize))
3803 return false;
3804 }
3805
3806 return true;
3807}
3808
3809/// isVPERMILPSMask - Return true if the specified VECTOR_SHUFFLE operand
3810/// specifies a shuffle of elements that is suitable for input to VPERMILPS*.
3811/// Note that VPERMIL mask matching is different depending whether theunderlying
3812/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3813/// to the same elements of the low, but to the higher half of the source.
3814/// In VPERMILPD the two lanes could be shuffled independently of each other
3815/// with the same restriction that lanes can't be crossed.
3816static bool isVPERMILPSMask(const SmallVectorImpl<int> &Mask, EVT VT,
3817 const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003818 unsigned NumElts = VT.getVectorNumElements();
3819 unsigned NumLanes = VT.getSizeInBits()/128;
3820
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003821 if (!Subtarget->hasAVX())
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003822 return false;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003823
Eli Friedmandca62d52011-10-10 22:28:47 +00003824 // Only match 256-bit with 32-bit types
3825 if (VT.getSizeInBits() != 256 || NumElts != 8)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003826 return false;
3827
3828 // The mask on the high lane should be the same as the low. Actually,
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003829 // they can differ if any of the corresponding index in a lane is undef
3830 // and the other stays in range.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003831 int LaneSize = NumElts/NumLanes;
3832 for (int i = 0; i < LaneSize; ++i) {
3833 int HighElt = i+LaneSize;
Bruno Cardoso Lopes155a92a2011-08-10 01:54:17 +00003834 bool HighValid = isUndefOrInRange(Mask[HighElt], LaneSize, NumElts);
3835 bool LowValid = isUndefOrInRange(Mask[i], 0, LaneSize);
3836
3837 if (!HighValid || !LowValid)
3838 return false;
3839 if (Mask[i] < 0 || Mask[HighElt] < 0)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003840 continue;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003841 if (Mask[HighElt]-Mask[i] != LaneSize)
3842 return false;
3843 }
3844
3845 return true;
3846}
3847
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003848/// getShuffleVPERMILPSImmediate - Return the appropriate immediate to shuffle
3849/// the specified VECTOR_MASK mask with VPERMILPS* instructions.
3850static unsigned getShuffleVPERMILPSImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003851 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3852 EVT VT = SVOp->getValueType(0);
3853
3854 int NumElts = VT.getVectorNumElements();
3855 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003856 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003857
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003858 // Although the mask is equal for both lanes do it twice to get the cases
3859 // where a mask will match because the same mask element is undef on the
3860 // first half but valid on the second. This would get pathological cases
3861 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003862 unsigned Mask = 0;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003863 for (int l = 0; l < NumLanes; ++l) {
3864 for (int i = 0; i < LaneSize; ++i) {
3865 int MaskElt = SVOp->getMaskElt(i+(l*LaneSize));
3866 if (MaskElt < 0)
3867 continue;
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003868 if (MaskElt >= LaneSize)
3869 MaskElt -= LaneSize;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003870 Mask |= MaskElt << (i*2);
3871 }
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003872 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003873
3874 return Mask;
3875}
3876
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003877/// getShuffleVPERMILPDImmediate - Return the appropriate immediate to shuffle
3878/// the specified VECTOR_MASK mask with VPERMILPD* instructions.
3879static unsigned getShuffleVPERMILPDImmediate(SDNode *N) {
3880 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3881 EVT VT = SVOp->getValueType(0);
3882
3883 int NumElts = VT.getVectorNumElements();
3884 int NumLanes = VT.getSizeInBits()/128;
3885
3886 unsigned Mask = 0;
3887 int LaneSize = NumElts/NumLanes;
3888 for (int l = 0; l < NumLanes; ++l)
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003889 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3890 int MaskElt = SVOp->getMaskElt(i);
3891 if (MaskElt < 0)
3892 continue;
3893 Mask |= (MaskElt-l*LaneSize) << i;
3894 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003895
3896 return Mask;
3897}
3898
Evan Cheng017dcc62006-04-21 01:05:10 +00003899/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3900/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003901/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003902static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003903 bool V2IsSplat = false, bool V2IsUndef = false) {
3904 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003905 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003906 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003907
Nate Begeman9008ca62009-04-27 18:41:29 +00003908 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003909 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003910
Nate Begeman9008ca62009-04-27 18:41:29 +00003911 for (int i = 1; i < NumOps; ++i)
3912 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3913 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3914 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003915 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003916
Evan Cheng39623da2006-04-20 08:58:49 +00003917 return true;
3918}
3919
Nate Begeman9008ca62009-04-27 18:41:29 +00003920static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003921 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003922 SmallVector<int, 8> M;
3923 N->getMask(M);
3924 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003925}
3926
Evan Chengd9539472006-04-14 21:59:03 +00003927/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3928/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003929/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3930bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3931 const X86Subtarget *Subtarget) {
3932 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003933 return false;
3934
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003935 // The second vector must be undef
3936 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3937 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003938
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003939 EVT VT = N->getValueType(0);
3940 unsigned NumElems = VT.getVectorNumElements();
3941
3942 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3943 (VT.getSizeInBits() == 256 && NumElems != 8))
3944 return false;
3945
3946 // "i+1" is the value the indexed mask element must have
3947 for (unsigned i = 0; i < NumElems; i += 2)
3948 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3949 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003950 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003951
3952 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003953}
3954
3955/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3956/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003957/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3958bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3959 const X86Subtarget *Subtarget) {
3960 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003961 return false;
3962
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003963 // The second vector must be undef
3964 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3965 return false;
3966
3967 EVT VT = N->getValueType(0);
3968 unsigned NumElems = VT.getVectorNumElements();
3969
3970 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3971 (VT.getSizeInBits() == 256 && NumElems != 8))
3972 return false;
3973
3974 // "i" is the value the indexed mask element must have
3975 for (unsigned i = 0; i < NumElems; i += 2)
3976 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3977 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003978 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003979
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003980 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003981}
3982
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003983/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3984/// specifies a shuffle of elements that is suitable for input to 256-bit
3985/// version of MOVDDUP.
3986static bool isMOVDDUPYMask(ShuffleVectorSDNode *N,
3987 const X86Subtarget *Subtarget) {
3988 EVT VT = N->getValueType(0);
3989 int NumElts = VT.getVectorNumElements();
3990 bool V2IsUndef = N->getOperand(1).getOpcode() == ISD::UNDEF;
3991
3992 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256 ||
3993 !V2IsUndef || NumElts != 4)
3994 return false;
3995
3996 for (int i = 0; i != NumElts/2; ++i)
3997 if (!isUndefOrEqual(N->getMaskElt(i), 0))
3998 return false;
3999 for (int i = NumElts/2; i != NumElts; ++i)
4000 if (!isUndefOrEqual(N->getMaskElt(i), NumElts/2))
4001 return false;
4002 return true;
4003}
4004
Evan Cheng0b457f02008-09-25 20:50:48 +00004005/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004006/// specifies a shuffle of elements that is suitable for input to 128-bit
4007/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00004008bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004009 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004010
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004011 if (VT.getSizeInBits() != 128)
4012 return false;
4013
4014 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004015 for (int i = 0; i < e; ++i)
4016 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004017 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004018 for (int i = 0; i < e; ++i)
4019 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004020 return false;
4021 return true;
4022}
4023
David Greenec38a03e2011-02-03 15:50:00 +00004024/// isVEXTRACTF128Index - Return true if the specified
4025/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4026/// suitable for input to VEXTRACTF128.
4027bool X86::isVEXTRACTF128Index(SDNode *N) {
4028 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4029 return false;
4030
4031 // The index should be aligned on a 128-bit boundary.
4032 uint64_t Index =
4033 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4034
4035 unsigned VL = N->getValueType(0).getVectorNumElements();
4036 unsigned VBits = N->getValueType(0).getSizeInBits();
4037 unsigned ElSize = VBits / VL;
4038 bool Result = (Index * ElSize) % 128 == 0;
4039
4040 return Result;
4041}
4042
David Greeneccacdc12011-02-04 16:08:29 +00004043/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4044/// operand specifies a subvector insert that is suitable for input to
4045/// VINSERTF128.
4046bool X86::isVINSERTF128Index(SDNode *N) {
4047 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4048 return false;
4049
4050 // The index should be aligned on a 128-bit boundary.
4051 uint64_t Index =
4052 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4053
4054 unsigned VL = N->getValueType(0).getVectorNumElements();
4055 unsigned VBits = N->getValueType(0).getSizeInBits();
4056 unsigned ElSize = VBits / VL;
4057 bool Result = (Index * ElSize) % 128 == 0;
4058
4059 return Result;
4060}
4061
Evan Cheng63d33002006-03-22 08:01:21 +00004062/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004063/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00004064unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004065 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4066 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4067
Evan Chengb9df0ca2006-03-22 02:53:00 +00004068 unsigned Shift = (NumOperands == 4) ? 2 : 1;
4069 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00004070 for (int i = 0; i < NumOperands; ++i) {
4071 int Val = SVOp->getMaskElt(NumOperands-i-1);
4072 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00004073 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00004074 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00004075 if (i != NumOperands - 1)
4076 Mask <<= Shift;
4077 }
Evan Cheng63d33002006-03-22 08:01:21 +00004078 return Mask;
4079}
4080
Evan Cheng506d3df2006-03-29 23:07:14 +00004081/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004082/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004083unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004084 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004085 unsigned Mask = 0;
4086 // 8 nodes, but we only care about the last 4.
4087 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004088 int Val = SVOp->getMaskElt(i);
4089 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004090 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00004091 if (i != 4)
4092 Mask <<= 2;
4093 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004094 return Mask;
4095}
4096
4097/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004098/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004099unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004100 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004101 unsigned Mask = 0;
4102 // 8 nodes, but we only care about the first 4.
4103 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004104 int Val = SVOp->getMaskElt(i);
4105 if (Val >= 0)
4106 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004107 if (i != 0)
4108 Mask <<= 2;
4109 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004110 return Mask;
4111}
4112
Nate Begemana09008b2009-10-19 02:17:23 +00004113/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4114/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4115unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4116 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4117 EVT VVT = N->getValueType(0);
4118 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4119 int Val = 0;
4120
4121 unsigned i, e;
4122 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4123 Val = SVOp->getMaskElt(i);
4124 if (Val >= 0)
4125 break;
4126 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004127 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004128 return (Val - i) * EltSize;
4129}
4130
David Greenec38a03e2011-02-03 15:50:00 +00004131/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4132/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4133/// instructions.
4134unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4135 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4136 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4137
4138 uint64_t Index =
4139 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4140
4141 EVT VecVT = N->getOperand(0).getValueType();
4142 EVT ElVT = VecVT.getVectorElementType();
4143
4144 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004145 return Index / NumElemsPerChunk;
4146}
4147
David Greeneccacdc12011-02-04 16:08:29 +00004148/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4149/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4150/// instructions.
4151unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4152 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4153 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4154
4155 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004156 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004157
4158 EVT VecVT = N->getValueType(0);
4159 EVT ElVT = VecVT.getVectorElementType();
4160
4161 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004162 return Index / NumElemsPerChunk;
4163}
4164
Evan Cheng37b73872009-07-30 08:33:02 +00004165/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4166/// constant +0.0.
4167bool X86::isZeroNode(SDValue Elt) {
4168 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004169 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004170 (isa<ConstantFPSDNode>(Elt) &&
4171 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4172}
4173
Nate Begeman9008ca62009-04-27 18:41:29 +00004174/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4175/// their permute mask.
4176static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4177 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004178 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004179 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004180 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004181
Nate Begeman5a5ca152009-04-29 05:20:52 +00004182 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004183 int idx = SVOp->getMaskElt(i);
4184 if (idx < 0)
4185 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004186 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004187 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004188 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004189 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004190 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004191 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4192 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004193}
4194
Evan Cheng779ccea2007-12-07 21:30:01 +00004195/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
4196/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00004197static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004198 unsigned NumElems = VT.getVectorNumElements();
4199 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004200 int idx = Mask[i];
4201 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004202 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004203 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004204 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004205 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004206 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004207 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004208}
4209
Evan Cheng533a0aa2006-04-19 20:35:22 +00004210/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4211/// match movhlps. The lower half elements should come from upper half of
4212/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004213/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004214static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004215 EVT VT = Op->getValueType(0);
4216 if (VT.getSizeInBits() != 128)
4217 return false;
4218 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004219 return false;
4220 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004221 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004222 return false;
4223 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004224 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004225 return false;
4226 return true;
4227}
4228
Evan Cheng5ced1d82006-04-06 23:23:56 +00004229/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004230/// is promoted to a vector. It also returns the LoadSDNode by reference if
4231/// required.
4232static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004233 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4234 return false;
4235 N = N->getOperand(0).getNode();
4236 if (!ISD::isNON_EXTLoad(N))
4237 return false;
4238 if (LD)
4239 *LD = cast<LoadSDNode>(N);
4240 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004241}
4242
Dan Gohman65fd6562011-11-03 21:49:52 +00004243// Test whether the given value is a vector value which will be legalized
4244// into a load.
4245static bool WillBeConstantPoolLoad(SDNode *N) {
4246 if (N->getOpcode() != ISD::BUILD_VECTOR)
4247 return false;
4248
4249 // Check for any non-constant elements.
4250 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4251 switch (N->getOperand(i).getNode()->getOpcode()) {
4252 case ISD::UNDEF:
4253 case ISD::ConstantFP:
4254 case ISD::Constant:
4255 break;
4256 default:
4257 return false;
4258 }
4259
4260 // Vectors of all-zeros and all-ones are materialized with special
4261 // instructions rather than being loaded.
4262 return !ISD::isBuildVectorAllZeros(N) &&
4263 !ISD::isBuildVectorAllOnes(N);
4264}
4265
Evan Cheng533a0aa2006-04-19 20:35:22 +00004266/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4267/// match movlp{s|d}. The lower half elements should come from lower half of
4268/// V1 (and in order), and the upper half elements should come from the upper
4269/// half of V2 (and in order). And since V1 will become the source of the
4270/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004271static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4272 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004273 EVT VT = Op->getValueType(0);
4274 if (VT.getSizeInBits() != 128)
4275 return false;
4276
Evan Cheng466685d2006-10-09 20:57:25 +00004277 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004278 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004279 // Is V2 is a vector load, don't do this transformation. We will try to use
4280 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004281 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004282 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004283
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004284 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004285
Evan Cheng533a0aa2006-04-19 20:35:22 +00004286 if (NumElems != 2 && NumElems != 4)
4287 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004288 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004289 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004290 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004291 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004292 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004293 return false;
4294 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004295}
4296
Evan Cheng39623da2006-04-20 08:58:49 +00004297/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4298/// all the same.
4299static bool isSplatVector(SDNode *N) {
4300 if (N->getOpcode() != ISD::BUILD_VECTOR)
4301 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004302
Dan Gohman475871a2008-07-27 21:46:04 +00004303 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004304 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4305 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004306 return false;
4307 return true;
4308}
4309
Evan Cheng213d2cf2007-05-17 18:45:50 +00004310/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004311/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004312/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004313static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004314 SDValue V1 = N->getOperand(0);
4315 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004316 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4317 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004318 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004319 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004320 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004321 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4322 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004323 if (Opc != ISD::BUILD_VECTOR ||
4324 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004325 return false;
4326 } else if (Idx >= 0) {
4327 unsigned Opc = V1.getOpcode();
4328 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4329 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004330 if (Opc != ISD::BUILD_VECTOR ||
4331 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004332 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004333 }
4334 }
4335 return true;
4336}
4337
4338/// getZeroVector - Returns a vector of specified type with all zero elements.
4339///
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004340static SDValue getZeroVector(EVT VT, bool HasXMMInt, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004341 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004342 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004343
Dale Johannesen0488fb62010-09-30 23:57:10 +00004344 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004345 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004346 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004347 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004348 if (HasXMMInt) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004349 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4350 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4351 } else { // SSE1
4352 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4353 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4354 }
4355 } else if (VT.getSizeInBits() == 256) { // AVX
4356 // 256-bit logic and arithmetic instructions in AVX are
4357 // all floating-point, no support for integer ops. Default
4358 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004359 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004360 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4361 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004362 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004363 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004364}
4365
Chris Lattner8a594482007-11-25 00:24:49 +00004366/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004367/// Always build ones vectors as <4 x i32>. For 256-bit types, use two
4368/// <4 x i32> inserted in a <8 x i32> appropriately. Then bitcast to their
4369/// original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00004370static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004371 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004372 assert((VT.is128BitVector() || VT.is256BitVector())
4373 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004374
Owen Anderson825b72b2009-08-11 20:47:22 +00004375 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004376 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4377 Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004378
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004379 if (VT.is256BitVector()) {
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004380 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4381 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4382 Vec = Insert128BitVector(InsV, Vec,
4383 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4384 }
4385
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004386 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004387}
4388
Evan Cheng39623da2006-04-20 08:58:49 +00004389/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4390/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004391static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004392 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004393 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004394
Evan Cheng39623da2006-04-20 08:58:49 +00004395 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004396 SmallVector<int, 8> MaskVec;
4397 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004398
Nate Begeman5a5ca152009-04-29 05:20:52 +00004399 for (unsigned i = 0; i != NumElems; ++i) {
4400 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004401 MaskVec[i] = NumElems;
4402 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004403 }
Evan Cheng39623da2006-04-20 08:58:49 +00004404 }
Evan Cheng39623da2006-04-20 08:58:49 +00004405 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004406 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4407 SVOp->getOperand(1), &MaskVec[0]);
4408 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004409}
4410
Evan Cheng017dcc62006-04-21 01:05:10 +00004411/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4412/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004413static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004414 SDValue V2) {
4415 unsigned NumElems = VT.getVectorNumElements();
4416 SmallVector<int, 8> Mask;
4417 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004418 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004419 Mask.push_back(i);
4420 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004421}
4422
Nate Begeman9008ca62009-04-27 18:41:29 +00004423/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004424static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004425 SDValue V2) {
4426 unsigned NumElems = VT.getVectorNumElements();
4427 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004428 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004429 Mask.push_back(i);
4430 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004431 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004432 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004433}
4434
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004435/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004436static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004437 SDValue V2) {
4438 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004439 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004440 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004441 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004442 Mask.push_back(i + Half);
4443 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004444 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004445 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004446}
4447
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004448// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004449// a generic shuffle instruction because the target has no such instructions.
4450// Generate shuffles which repeat i16 and i8 several times until they can be
4451// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004452static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004453 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004454 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004455 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004456
Nate Begeman9008ca62009-04-27 18:41:29 +00004457 while (NumElems > 4) {
4458 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004459 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004460 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004461 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004462 EltNo -= NumElems/2;
4463 }
4464 NumElems >>= 1;
4465 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004466 return V;
4467}
Eric Christopherfd179292009-08-27 18:07:15 +00004468
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004469/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4470static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4471 EVT VT = V.getValueType();
4472 DebugLoc dl = V.getDebugLoc();
4473 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4474 && "Vector size not supported");
4475
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004476 if (VT.getSizeInBits() == 128) {
4477 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004478 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004479 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4480 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004481 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004482 // To use VPERMILPS to splat scalars, the second half of indicies must
4483 // refer to the higher part, which is a duplication of the lower one,
4484 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004485 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4486 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004487
4488 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4489 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4490 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004491 }
4492
4493 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4494}
4495
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004496/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004497static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4498 EVT SrcVT = SV->getValueType(0);
4499 SDValue V1 = SV->getOperand(0);
4500 DebugLoc dl = SV->getDebugLoc();
4501
4502 int EltNo = SV->getSplatIndex();
4503 int NumElems = SrcVT.getVectorNumElements();
4504 unsigned Size = SrcVT.getSizeInBits();
4505
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004506 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4507 "Unknown how to promote splat for type");
4508
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004509 // Extract the 128-bit part containing the splat element and update
4510 // the splat element index when it refers to the higher register.
4511 if (Size == 256) {
4512 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4513 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4514 if (Idx > 0)
4515 EltNo -= NumElems/2;
4516 }
4517
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004518 // All i16 and i8 vector types can't be used directly by a generic shuffle
4519 // instruction because the target has no such instruction. Generate shuffles
4520 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004521 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004522 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004523 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004524 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004525
4526 // Recreate the 256-bit vector and place the same 128-bit vector
4527 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004528 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004529 if (Size == 256) {
4530 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4531 DAG.getConstant(0, MVT::i32), DAG, dl);
4532 V1 = Insert128BitVector(InsV, V1,
4533 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4534 }
4535
4536 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004537}
4538
Evan Chengba05f722006-04-21 23:03:30 +00004539/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004540/// vector of zero or undef vector. This produces a shuffle where the low
4541/// element of V2 is swizzled into the zero/undef vector, landing at element
4542/// 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 +00004543static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004544 bool isZero, bool HasXMMInt,
4545 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004546 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004547 SDValue V1 = isZero
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004548 ? getZeroVector(VT, HasXMMInt, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004549 unsigned NumElems = VT.getVectorNumElements();
4550 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004551 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004552 // If this is the insertion idx, put the low elt of V2 here.
4553 MaskVec.push_back(i == Idx ? NumElems : i);
4554 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004555}
4556
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004557/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4558/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004559static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4560 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004561 if (Depth == 6)
4562 return SDValue(); // Limit search depth.
4563
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004564 SDValue V = SDValue(N, 0);
4565 EVT VT = V.getValueType();
4566 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004567
4568 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4569 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4570 Index = SV->getMaskElt(Index);
4571
4572 if (Index < 0)
4573 return DAG.getUNDEF(VT.getVectorElementType());
4574
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004575 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004576 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004577 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004578 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004579
4580 // Recurse into target specific vector shuffles to find scalars.
4581 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004582 int NumElems = VT.getVectorNumElements();
4583 SmallVector<unsigned, 16> ShuffleMask;
4584 SDValue ImmN;
4585
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004586 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004587 case X86ISD::SHUFPS:
4588 case X86ISD::SHUFPD:
4589 ImmN = N->getOperand(N->getNumOperands()-1);
4590 DecodeSHUFPSMask(NumElems,
4591 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4592 ShuffleMask);
4593 break;
4594 case X86ISD::PUNPCKHBW:
4595 case X86ISD::PUNPCKHWD:
4596 case X86ISD::PUNPCKHDQ:
4597 case X86ISD::PUNPCKHQDQ:
4598 DecodePUNPCKHMask(NumElems, ShuffleMask);
4599 break;
4600 case X86ISD::UNPCKHPS:
4601 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00004602 case X86ISD::VUNPCKHPSY:
4603 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004604 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4605 break;
4606 case X86ISD::PUNPCKLBW:
4607 case X86ISD::PUNPCKLWD:
4608 case X86ISD::PUNPCKLDQ:
4609 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004610 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004611 break;
4612 case X86ISD::UNPCKLPS:
4613 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004614 case X86ISD::VUNPCKLPSY:
4615 case X86ISD::VUNPCKLPDY:
4616 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004617 break;
4618 case X86ISD::MOVHLPS:
4619 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4620 break;
4621 case X86ISD::MOVLHPS:
4622 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4623 break;
4624 case X86ISD::PSHUFD:
4625 ImmN = N->getOperand(N->getNumOperands()-1);
4626 DecodePSHUFMask(NumElems,
4627 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4628 ShuffleMask);
4629 break;
4630 case X86ISD::PSHUFHW:
4631 ImmN = N->getOperand(N->getNumOperands()-1);
4632 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4633 ShuffleMask);
4634 break;
4635 case X86ISD::PSHUFLW:
4636 ImmN = N->getOperand(N->getNumOperands()-1);
4637 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4638 ShuffleMask);
4639 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004640 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004641 case X86ISD::MOVSD: {
4642 // The index 0 always comes from the first element of the second source,
4643 // this is why MOVSS and MOVSD are used in the first place. The other
4644 // elements come from the other positions of the first source vector.
4645 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004646 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4647 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004648 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004649 case X86ISD::VPERMILPS:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004650 ImmN = N->getOperand(N->getNumOperands()-1);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004651 DecodeVPERMILPSMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004652 ShuffleMask);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004653 break;
4654 case X86ISD::VPERMILPSY:
4655 ImmN = N->getOperand(N->getNumOperands()-1);
4656 DecodeVPERMILPSMask(8, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4657 ShuffleMask);
4658 break;
4659 case X86ISD::VPERMILPD:
4660 ImmN = N->getOperand(N->getNumOperands()-1);
4661 DecodeVPERMILPDMask(2, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4662 ShuffleMask);
4663 break;
4664 case X86ISD::VPERMILPDY:
4665 ImmN = N->getOperand(N->getNumOperands()-1);
4666 DecodeVPERMILPDMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4667 ShuffleMask);
4668 break;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004669 case X86ISD::VPERM2F128:
4670 ImmN = N->getOperand(N->getNumOperands()-1);
4671 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4672 ShuffleMask);
4673 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004674 case X86ISD::MOVDDUP:
4675 case X86ISD::MOVLHPD:
4676 case X86ISD::MOVLPD:
4677 case X86ISD::MOVLPS:
4678 case X86ISD::MOVSHDUP:
4679 case X86ISD::MOVSLDUP:
4680 case X86ISD::PALIGN:
4681 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004682 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004683 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004684 return SDValue();
4685 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004686
4687 Index = ShuffleMask[Index];
4688 if (Index < 0)
4689 return DAG.getUNDEF(VT.getVectorElementType());
4690
4691 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4692 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4693 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004694 }
4695
4696 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004697 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004698 V = V.getOperand(0);
4699 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004700 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004701
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004702 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004703 return SDValue();
4704 }
4705
4706 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4707 return (Index == 0) ? V.getOperand(0)
4708 : DAG.getUNDEF(VT.getVectorElementType());
4709
4710 if (V.getOpcode() == ISD::BUILD_VECTOR)
4711 return V.getOperand(Index);
4712
4713 return SDValue();
4714}
4715
4716/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4717/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004718/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004719static
4720unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4721 bool ZerosFromLeft, SelectionDAG &DAG) {
4722 int i = 0;
4723
4724 while (i < NumElems) {
4725 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004726 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004727 if (!(Elt.getNode() &&
4728 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4729 break;
4730 ++i;
4731 }
4732
4733 return i;
4734}
4735
4736/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4737/// MaskE correspond consecutively to elements from one of the vector operands,
4738/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4739static
4740bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4741 int OpIdx, int NumElems, unsigned &OpNum) {
4742 bool SeenV1 = false;
4743 bool SeenV2 = false;
4744
4745 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4746 int Idx = SVOp->getMaskElt(i);
4747 // Ignore undef indicies
4748 if (Idx < 0)
4749 continue;
4750
4751 if (Idx < NumElems)
4752 SeenV1 = true;
4753 else
4754 SeenV2 = true;
4755
4756 // Only accept consecutive elements from the same vector
4757 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4758 return false;
4759 }
4760
4761 OpNum = SeenV1 ? 0 : 1;
4762 return true;
4763}
4764
4765/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4766/// logical left shift of a vector.
4767static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4768 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4769 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4770 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4771 false /* check zeros from right */, DAG);
4772 unsigned OpSrc;
4773
4774 if (!NumZeros)
4775 return false;
4776
4777 // Considering the elements in the mask that are not consecutive zeros,
4778 // check if they consecutively come from only one of the source vectors.
4779 //
4780 // V1 = {X, A, B, C} 0
4781 // \ \ \ /
4782 // vector_shuffle V1, V2 <1, 2, 3, X>
4783 //
4784 if (!isShuffleMaskConsecutive(SVOp,
4785 0, // Mask Start Index
4786 NumElems-NumZeros-1, // Mask End Index
4787 NumZeros, // Where to start looking in the src vector
4788 NumElems, // Number of elements in vector
4789 OpSrc)) // Which source operand ?
4790 return false;
4791
4792 isLeft = false;
4793 ShAmt = NumZeros;
4794 ShVal = SVOp->getOperand(OpSrc);
4795 return true;
4796}
4797
4798/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4799/// logical left shift of a vector.
4800static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4801 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4802 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4803 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4804 true /* check zeros from left */, DAG);
4805 unsigned OpSrc;
4806
4807 if (!NumZeros)
4808 return false;
4809
4810 // Considering the elements in the mask that are not consecutive zeros,
4811 // check if they consecutively come from only one of the source vectors.
4812 //
4813 // 0 { A, B, X, X } = V2
4814 // / \ / /
4815 // vector_shuffle V1, V2 <X, X, 4, 5>
4816 //
4817 if (!isShuffleMaskConsecutive(SVOp,
4818 NumZeros, // Mask Start Index
4819 NumElems-1, // Mask End Index
4820 0, // Where to start looking in the src vector
4821 NumElems, // Number of elements in vector
4822 OpSrc)) // Which source operand ?
4823 return false;
4824
4825 isLeft = true;
4826 ShAmt = NumZeros;
4827 ShVal = SVOp->getOperand(OpSrc);
4828 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004829}
4830
4831/// isVectorShift - Returns true if the shuffle can be implemented as a
4832/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004833static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004834 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004835 // Although the logic below support any bitwidth size, there are no
4836 // shift instructions which handle more than 128-bit vectors.
4837 if (SVOp->getValueType(0).getSizeInBits() > 128)
4838 return false;
4839
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004840 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4841 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4842 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004843
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004844 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004845}
4846
Evan Chengc78d3b42006-04-24 18:01:45 +00004847/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4848///
Dan Gohman475871a2008-07-27 21:46:04 +00004849static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004850 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004851 SelectionDAG &DAG,
4852 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004853 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004854 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004855
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004856 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004857 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004858 bool First = true;
4859 for (unsigned i = 0; i < 16; ++i) {
4860 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4861 if (ThisIsNonZero && First) {
4862 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004863 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004864 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004865 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004866 First = false;
4867 }
4868
4869 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004870 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004871 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4872 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004873 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004874 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004875 }
4876 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004877 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4878 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4879 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004880 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004881 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004882 } else
4883 ThisElt = LastElt;
4884
Gabor Greifba36cb52008-08-28 21:40:38 +00004885 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004886 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004887 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004888 }
4889 }
4890
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004891 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004892}
4893
Bill Wendlinga348c562007-03-22 18:42:45 +00004894/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004895///
Dan Gohman475871a2008-07-27 21:46:04 +00004896static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004897 unsigned NumNonZero, unsigned NumZero,
4898 SelectionDAG &DAG,
4899 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004900 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004901 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004902
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004903 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004904 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004905 bool First = true;
4906 for (unsigned i = 0; i < 8; ++i) {
4907 bool isNonZero = (NonZeros & (1 << i)) != 0;
4908 if (isNonZero) {
4909 if (First) {
4910 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004911 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004912 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004913 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004914 First = false;
4915 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004916 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004917 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004918 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004919 }
4920 }
4921
4922 return V;
4923}
4924
Evan Chengf26ffe92008-05-29 08:22:04 +00004925/// getVShift - Return a vector logical shift node.
4926///
Owen Andersone50ed302009-08-10 22:56:29 +00004927static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004928 unsigned NumBits, SelectionDAG &DAG,
4929 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004930 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004931 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004932 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004933 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4934 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004935 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004936 DAG.getConstant(NumBits,
4937 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004938}
4939
Dan Gohman475871a2008-07-27 21:46:04 +00004940SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004941X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004942 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004943
Evan Chengc3630942009-12-09 21:00:30 +00004944 // Check if the scalar load can be widened into a vector load. And if
4945 // the address is "base + cst" see if the cst can be "absorbed" into
4946 // the shuffle mask.
4947 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4948 SDValue Ptr = LD->getBasePtr();
4949 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4950 return SDValue();
4951 EVT PVT = LD->getValueType(0);
4952 if (PVT != MVT::i32 && PVT != MVT::f32)
4953 return SDValue();
4954
4955 int FI = -1;
4956 int64_t Offset = 0;
4957 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4958 FI = FINode->getIndex();
4959 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004960 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004961 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4962 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4963 Offset = Ptr.getConstantOperandVal(1);
4964 Ptr = Ptr.getOperand(0);
4965 } else {
4966 return SDValue();
4967 }
4968
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004969 // FIXME: 256-bit vector instructions don't require a strict alignment,
4970 // improve this code to support it better.
4971 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004972 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004973 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004974 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004975 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004976 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004977 // Can't change the alignment. FIXME: It's possible to compute
4978 // the exact stack offset and reference FI + adjust offset instead.
4979 // If someone *really* cares about this. That's the way to implement it.
4980 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004981 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004982 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004983 }
4984 }
4985
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004986 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004987 // Ptr + (Offset & ~15).
4988 if (Offset < 0)
4989 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004990 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004991 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004992 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004993 if (StartOffset)
4994 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4995 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4996
4997 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004998 int NumElems = VT.getVectorNumElements();
4999
5000 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
5001 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
5002 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00005003 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005004 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005005
5006 // Canonicalize it to a v4i32 or v8i32 shuffle.
5007 SmallVector<int, 8> Mask;
5008 for (int i = 0; i < NumElems; ++i)
5009 Mask.push_back(EltNo);
5010
5011 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
5012 return DAG.getNode(ISD::BITCAST, dl, NVT,
5013 DAG.getVectorShuffle(CanonVT, dl, V1,
5014 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00005015 }
5016
5017 return SDValue();
5018}
5019
Michael J. Spencerec38de22010-10-10 22:04:20 +00005020/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5021/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005022/// load which has the same value as a build_vector whose operands are 'elts'.
5023///
5024/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005025///
Nate Begeman1449f292010-03-24 22:19:06 +00005026/// FIXME: we'd also like to handle the case where the last elements are zero
5027/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5028/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005029static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005030 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005031 EVT EltVT = VT.getVectorElementType();
5032 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005033
Nate Begemanfdea31a2010-03-24 20:49:50 +00005034 LoadSDNode *LDBase = NULL;
5035 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005036
Nate Begeman1449f292010-03-24 22:19:06 +00005037 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005038 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005039 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005040 for (unsigned i = 0; i < NumElems; ++i) {
5041 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005042
Nate Begemanfdea31a2010-03-24 20:49:50 +00005043 if (!Elt.getNode() ||
5044 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5045 return SDValue();
5046 if (!LDBase) {
5047 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5048 return SDValue();
5049 LDBase = cast<LoadSDNode>(Elt.getNode());
5050 LastLoadedElt = i;
5051 continue;
5052 }
5053 if (Elt.getOpcode() == ISD::UNDEF)
5054 continue;
5055
5056 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5057 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5058 return SDValue();
5059 LastLoadedElt = i;
5060 }
Nate Begeman1449f292010-03-24 22:19:06 +00005061
5062 // If we have found an entire vector of loads and undefs, then return a large
5063 // load of the entire vector width starting at the base pointer. If we found
5064 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005065 if (LastLoadedElt == NumElems - 1) {
5066 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005067 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005068 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005069 LDBase->isVolatile(), LDBase->isNonTemporal(),
5070 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005071 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005072 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005073 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005074 LDBase->isInvariant(), LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00005075 } else if (NumElems == 4 && LastLoadedElt == 1 &&
5076 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005077 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5078 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005079 SDValue ResNode =
5080 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5081 LDBase->getPointerInfo(),
5082 LDBase->getAlignment(),
5083 false/*isVolatile*/, true/*ReadMem*/,
5084 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005085 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005086 }
5087 return SDValue();
5088}
5089
Evan Chengc3630942009-12-09 21:00:30 +00005090SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005091X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005092 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005093
David Greenef125a292011-02-08 19:04:41 +00005094 EVT VT = Op.getValueType();
5095 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005096 unsigned NumElems = Op.getNumOperands();
5097
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005098 // Vectors containing all zeros can be matched by pxor and xorps later
5099 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5100 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5101 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005102 if (Op.getValueType() == MVT::v4i32 ||
5103 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005104 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005105
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005106 return getZeroVector(Op.getValueType(), Subtarget->hasXMMInt(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005107 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005108
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005109 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
5110 // vectors or broken into v4i32 operations on 256-bit vectors.
5111 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
5112 if (Op.getValueType() == MVT::v4i32)
5113 return Op;
5114
5115 return getOnesVector(Op.getValueType(), DAG, dl);
5116 }
5117
Owen Andersone50ed302009-08-10 22:56:29 +00005118 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005119
Evan Cheng0db9fe62006-04-25 20:13:52 +00005120 unsigned NumZero = 0;
5121 unsigned NumNonZero = 0;
5122 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005123 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005124 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005125 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005126 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005127 if (Elt.getOpcode() == ISD::UNDEF)
5128 continue;
5129 Values.insert(Elt);
5130 if (Elt.getOpcode() != ISD::Constant &&
5131 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005132 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005133 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005134 NumZero++;
5135 else {
5136 NonZeros |= (1 << i);
5137 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005138 }
5139 }
5140
Chris Lattner97a2a562010-08-26 05:24:29 +00005141 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5142 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005143 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005144
Chris Lattner67f453a2008-03-09 05:42:06 +00005145 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005146 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005147 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005148 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005149
Chris Lattner62098042008-03-09 01:05:04 +00005150 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5151 // the value are obviously zero, truncate the value to i32 and do the
5152 // insertion that way. Only do this if the value is non-constant or if the
5153 // value is a constant being inserted into element 0. It is cheaper to do
5154 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005155 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005156 (!IsAllConstants || Idx == 0)) {
5157 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005158 // Handle SSE only.
5159 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5160 EVT VecVT = MVT::v4i32;
5161 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005162
Chris Lattner62098042008-03-09 01:05:04 +00005163 // Truncate the value (which may itself be a constant) to i32, and
5164 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005165 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005166 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005167 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005168 Subtarget->hasXMMInt(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005169
Chris Lattner62098042008-03-09 01:05:04 +00005170 // Now we have our 32-bit value zero extended in the low element of
5171 // a vector. If Idx != 0, swizzle it into place.
5172 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005173 SmallVector<int, 4> Mask;
5174 Mask.push_back(Idx);
5175 for (unsigned i = 1; i != VecElts; ++i)
5176 Mask.push_back(i);
5177 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005178 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005179 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005180 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005181 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005182 }
5183 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005184
Chris Lattner19f79692008-03-08 22:59:52 +00005185 // If we have a constant or non-constant insertion into the low element of
5186 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5187 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005188 // depending on what the source datatype is.
5189 if (Idx == 0) {
5190 if (NumZero == 0) {
5191 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005192 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5193 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005194 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5195 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005196 return getShuffleVectorZeroOrUndef(Item, 0, true,Subtarget->hasXMMInt(),
Eli Friedman10415532009-06-06 06:05:10 +00005197 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005198 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5199 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005200 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5201 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005202 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5203 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005204 Subtarget->hasXMMInt(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005205 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005206 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005207 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005208
5209 // Is it a vector logical left shift?
5210 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005211 X86::isZeroNode(Op.getOperand(0)) &&
5212 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005213 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005214 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005215 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005216 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005217 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005218 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005219
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005220 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005221 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005222
Chris Lattner19f79692008-03-08 22:59:52 +00005223 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5224 // is a non-constant being inserted into an element other than the low one,
5225 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5226 // movd/movss) to move this into the low element, then shuffle it into
5227 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005228 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005229 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005230
Evan Cheng0db9fe62006-04-25 20:13:52 +00005231 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005232 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005233 Subtarget->hasXMMInt(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005234 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005235 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005236 MaskVec.push_back(i == Idx ? 0 : 1);
5237 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005238 }
5239 }
5240
Chris Lattner67f453a2008-03-09 05:42:06 +00005241 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005242 if (Values.size() == 1) {
5243 if (EVTBits == 32) {
5244 // Instead of a shuffle like this:
5245 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5246 // Check if it's possible to issue this instead.
5247 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5248 unsigned Idx = CountTrailingZeros_32(NonZeros);
5249 SDValue Item = Op.getOperand(Idx);
5250 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5251 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5252 }
Dan Gohman475871a2008-07-27 21:46:04 +00005253 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005254 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005255
Dan Gohmana3941172007-07-24 22:55:08 +00005256 // A vector full of immediates; various special cases are already
5257 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005258 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005259 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005260
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005261 // For AVX-length vectors, build the individual 128-bit pieces and use
5262 // shuffles to put them in place.
5263 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5264 SmallVector<SDValue, 32> V;
5265 for (unsigned i = 0; i < NumElems; ++i)
5266 V.push_back(Op.getOperand(i));
5267
5268 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5269
5270 // Build both the lower and upper subvector.
5271 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5272 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5273 NumElems/2);
5274
5275 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005276 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5277 DAG.getConstant(0, MVT::i32), DAG, dl);
5278 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005279 DAG, dl);
5280 }
5281
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005282 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005283 if (EVTBits == 64) {
5284 if (NumNonZero == 1) {
5285 // One half is zero or undef.
5286 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005287 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005288 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005289 return getShuffleVectorZeroOrUndef(V2, Idx, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005290 Subtarget->hasXMMInt(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005291 }
Dan Gohman475871a2008-07-27 21:46:04 +00005292 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005293 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005294
5295 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005296 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005297 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005298 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005299 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005300 }
5301
Bill Wendling826f36f2007-03-28 00:57:11 +00005302 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005303 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005304 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005305 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005306 }
5307
5308 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005309 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005310 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005311 if (NumElems == 4 && NumZero > 0) {
5312 for (unsigned i = 0; i < 4; ++i) {
5313 bool isZero = !(NonZeros & (1 << i));
5314 if (isZero)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005315 V[i] = getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005316 else
Dale Johannesenace16102009-02-03 19:33:06 +00005317 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005318 }
5319
5320 for (unsigned i = 0; i < 2; ++i) {
5321 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5322 default: break;
5323 case 0:
5324 V[i] = V[i*2]; // Must be a zero vector.
5325 break;
5326 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005327 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005328 break;
5329 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005330 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005331 break;
5332 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005333 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005334 break;
5335 }
5336 }
5337
Nate Begeman9008ca62009-04-27 18:41:29 +00005338 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005339 bool Reverse = (NonZeros & 0x3) == 2;
5340 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005341 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005342 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5343 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005344 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5345 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005346 }
5347
Nate Begemanfdea31a2010-03-24 20:49:50 +00005348 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5349 // Check for a build vector of consecutive loads.
5350 for (unsigned i = 0; i < NumElems; ++i)
5351 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005352
Nate Begemanfdea31a2010-03-24 20:49:50 +00005353 // Check for elements which are consecutive loads.
5354 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5355 if (LD.getNode())
5356 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005357
5358 // For SSE 4.1, use insertps to put the high elements into the low element.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005359 if (getSubtarget()->hasSSE41() || getSubtarget()->hasAVX()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005360 SDValue Result;
5361 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5362 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5363 else
5364 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005365
Chris Lattner24faf612010-08-28 17:59:08 +00005366 for (unsigned i = 1; i < NumElems; ++i) {
5367 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5368 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005369 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005370 }
5371 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005372 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005373
Chris Lattner6e80e442010-08-28 17:15:43 +00005374 // Otherwise, expand into a number of unpckl*, start by extending each of
5375 // our (non-undef) elements to the full vector width with the element in the
5376 // bottom slot of the vector (which generates no code for SSE).
5377 for (unsigned i = 0; i < NumElems; ++i) {
5378 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5379 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5380 else
5381 V[i] = DAG.getUNDEF(VT);
5382 }
5383
5384 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005385 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5386 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5387 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005388 unsigned EltStride = NumElems >> 1;
5389 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005390 for (unsigned i = 0; i < EltStride; ++i) {
5391 // If V[i+EltStride] is undef and this is the first round of mixing,
5392 // then it is safe to just drop this shuffle: V[i] is already in the
5393 // right place, the one element (since it's the first round) being
5394 // inserted as undef can be dropped. This isn't safe for successive
5395 // rounds because they will permute elements within both vectors.
5396 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5397 EltStride == NumElems/2)
5398 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005399
Chris Lattner6e80e442010-08-28 17:15:43 +00005400 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005401 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005402 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005403 }
5404 return V[0];
5405 }
Dan Gohman475871a2008-07-27 21:46:04 +00005406 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005407}
5408
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005409// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5410// them in a MMX register. This is better than doing a stack convert.
5411static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005412 DebugLoc dl = Op.getDebugLoc();
5413 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005414
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005415 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5416 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5417 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005418 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005419 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5420 InVec = Op.getOperand(1);
5421 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5422 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005423 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005424 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5425 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5426 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005427 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005428 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5429 Mask[0] = 0; Mask[1] = 2;
5430 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5431 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005432 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005433}
5434
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005435// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5436// to create 256-bit vectors from two other 128-bit ones.
5437static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5438 DebugLoc dl = Op.getDebugLoc();
5439 EVT ResVT = Op.getValueType();
5440
5441 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5442
5443 SDValue V1 = Op.getOperand(0);
5444 SDValue V2 = Op.getOperand(1);
5445 unsigned NumElems = ResVT.getVectorNumElements();
5446
5447 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5448 DAG.getConstant(0, MVT::i32), DAG, dl);
5449 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5450 DAG, dl);
5451}
5452
5453SDValue
5454X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005455 EVT ResVT = Op.getValueType();
5456
5457 assert(Op.getNumOperands() == 2);
5458 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5459 "Unsupported CONCAT_VECTORS for value type");
5460
5461 // We support concatenate two MMX registers and place them in a MMX register.
5462 // This is better than doing a stack convert.
5463 if (ResVT.is128BitVector())
5464 return LowerMMXCONCAT_VECTORS(Op, DAG);
5465
5466 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5467 // from two other 128-bit ones.
5468 return LowerAVXCONCAT_VECTORS(Op, DAG);
5469}
5470
Nate Begemanb9a47b82009-02-23 08:49:38 +00005471// v8i16 shuffles - Prefer shuffles in the following order:
5472// 1. [all] pshuflw, pshufhw, optional move
5473// 2. [ssse3] 1 x pshufb
5474// 3. [ssse3] 2 x pshufb + 1 x por
5475// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005476SDValue
5477X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5478 SelectionDAG &DAG) const {
5479 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005480 SDValue V1 = SVOp->getOperand(0);
5481 SDValue V2 = SVOp->getOperand(1);
5482 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005483 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005484
Nate Begemanb9a47b82009-02-23 08:49:38 +00005485 // Determine if more than 1 of the words in each of the low and high quadwords
5486 // of the result come from the same quadword of one of the two inputs. Undef
5487 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005488 unsigned LoQuad[] = { 0, 0, 0, 0 };
5489 unsigned HiQuad[] = { 0, 0, 0, 0 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005490 BitVector InputQuads(4);
5491 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005492 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005493 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005494 MaskVals.push_back(EltIdx);
5495 if (EltIdx < 0) {
5496 ++Quad[0];
5497 ++Quad[1];
5498 ++Quad[2];
5499 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005500 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005501 }
5502 ++Quad[EltIdx / 4];
5503 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005504 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005505
Nate Begemanb9a47b82009-02-23 08:49:38 +00005506 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005507 unsigned MaxQuad = 1;
5508 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005509 if (LoQuad[i] > MaxQuad) {
5510 BestLoQuad = i;
5511 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005512 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005513 }
5514
Nate Begemanb9a47b82009-02-23 08:49:38 +00005515 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005516 MaxQuad = 1;
5517 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005518 if (HiQuad[i] > MaxQuad) {
5519 BestHiQuad = i;
5520 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005521 }
5522 }
5523
Nate Begemanb9a47b82009-02-23 08:49:38 +00005524 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005525 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005526 // single pshufb instruction is necessary. If There are more than 2 input
5527 // quads, disable the next transformation since it does not help SSSE3.
5528 bool V1Used = InputQuads[0] || InputQuads[1];
5529 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005530 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005531 if (InputQuads.count() == 2 && V1Used && V2Used) {
5532 BestLoQuad = InputQuads.find_first();
5533 BestHiQuad = InputQuads.find_next(BestLoQuad);
5534 }
5535 if (InputQuads.count() > 2) {
5536 BestLoQuad = -1;
5537 BestHiQuad = -1;
5538 }
5539 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005540
Nate Begemanb9a47b82009-02-23 08:49:38 +00005541 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5542 // the shuffle mask. If a quad is scored as -1, that means that it contains
5543 // words from all 4 input quadwords.
5544 SDValue NewV;
5545 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005546 SmallVector<int, 8> MaskV;
5547 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5548 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005549 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005550 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5551 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5552 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005553
Nate Begemanb9a47b82009-02-23 08:49:38 +00005554 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5555 // source words for the shuffle, to aid later transformations.
5556 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005557 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005558 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005559 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005560 if (idx != (int)i)
5561 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005562 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005563 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005564 AllWordsInNewV = false;
5565 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005566 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005567
Nate Begemanb9a47b82009-02-23 08:49:38 +00005568 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5569 if (AllWordsInNewV) {
5570 for (int i = 0; i != 8; ++i) {
5571 int idx = MaskVals[i];
5572 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005573 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005574 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005575 if ((idx != i) && idx < 4)
5576 pshufhw = false;
5577 if ((idx != i) && idx > 3)
5578 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005579 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005580 V1 = NewV;
5581 V2Used = false;
5582 BestLoQuad = 0;
5583 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005584 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005585
Nate Begemanb9a47b82009-02-23 08:49:38 +00005586 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5587 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005588 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005589 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5590 unsigned TargetMask = 0;
5591 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005592 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005593 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5594 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5595 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005596 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005597 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005598 }
Eric Christopherfd179292009-08-27 18:07:15 +00005599
Nate Begemanb9a47b82009-02-23 08:49:38 +00005600 // If we have SSSE3, and all words of the result are from 1 input vector,
5601 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5602 // is present, fall back to case 4.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005603 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005604 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005605
Nate Begemanb9a47b82009-02-23 08:49:38 +00005606 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005607 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005608 // mask, and elements that come from V1 in the V2 mask, so that the two
5609 // results can be OR'd together.
5610 bool TwoInputs = V1Used && V2Used;
5611 for (unsigned i = 0; i != 8; ++i) {
5612 int EltIdx = MaskVals[i] * 2;
5613 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005614 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5615 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005616 continue;
5617 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005618 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5619 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005620 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005621 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005622 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005623 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005624 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005625 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005626 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005627
Nate Begemanb9a47b82009-02-23 08:49:38 +00005628 // Calculate the shuffle mask for the second input, shuffle it, and
5629 // OR it with the first shuffled input.
5630 pshufbMask.clear();
5631 for (unsigned i = 0; i != 8; ++i) {
5632 int EltIdx = MaskVals[i] * 2;
5633 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005634 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5635 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005636 continue;
5637 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005638 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5639 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005640 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005641 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005642 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005643 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005644 MVT::v16i8, &pshufbMask[0], 16));
5645 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005646 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005647 }
5648
5649 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5650 // and update MaskVals with new element order.
5651 BitVector InOrder(8);
5652 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005653 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005654 for (int i = 0; i != 4; ++i) {
5655 int idx = MaskVals[i];
5656 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005657 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005658 InOrder.set(i);
5659 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005660 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005661 InOrder.set(i);
5662 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005663 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005664 }
5665 }
5666 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005667 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005668 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005669 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005670
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005671 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5672 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005673 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5674 NewV.getOperand(0),
5675 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5676 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005677 }
Eric Christopherfd179292009-08-27 18:07:15 +00005678
Nate Begemanb9a47b82009-02-23 08:49:38 +00005679 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5680 // and update MaskVals with the new element order.
5681 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005682 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005683 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005684 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005685 for (unsigned i = 4; i != 8; ++i) {
5686 int idx = MaskVals[i];
5687 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005688 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005689 InOrder.set(i);
5690 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005691 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005692 InOrder.set(i);
5693 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005694 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005695 }
5696 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005697 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005698 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005699
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005700 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5701 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005702 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5703 NewV.getOperand(0),
5704 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5705 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005706 }
Eric Christopherfd179292009-08-27 18:07:15 +00005707
Nate Begemanb9a47b82009-02-23 08:49:38 +00005708 // In case BestHi & BestLo were both -1, which means each quadword has a word
5709 // from each of the four input quadwords, calculate the InOrder bitvector now
5710 // before falling through to the insert/extract cleanup.
5711 if (BestLoQuad == -1 && BestHiQuad == -1) {
5712 NewV = V1;
5713 for (int i = 0; i != 8; ++i)
5714 if (MaskVals[i] < 0 || MaskVals[i] == i)
5715 InOrder.set(i);
5716 }
Eric Christopherfd179292009-08-27 18:07:15 +00005717
Nate Begemanb9a47b82009-02-23 08:49:38 +00005718 // The other elements are put in the right place using pextrw and pinsrw.
5719 for (unsigned i = 0; i != 8; ++i) {
5720 if (InOrder[i])
5721 continue;
5722 int EltIdx = MaskVals[i];
5723 if (EltIdx < 0)
5724 continue;
5725 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005726 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005727 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005728 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005729 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005730 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005731 DAG.getIntPtrConstant(i));
5732 }
5733 return NewV;
5734}
5735
5736// v16i8 shuffles - Prefer shuffles in the following order:
5737// 1. [ssse3] 1 x pshufb
5738// 2. [ssse3] 2 x pshufb + 1 x por
5739// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5740static
Nate Begeman9008ca62009-04-27 18:41:29 +00005741SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005742 SelectionDAG &DAG,
5743 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005744 SDValue V1 = SVOp->getOperand(0);
5745 SDValue V2 = SVOp->getOperand(1);
5746 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005747 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005748 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005749
Nate Begemanb9a47b82009-02-23 08:49:38 +00005750 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005751 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005752 // present, fall back to case 3.
5753 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5754 bool V1Only = true;
5755 bool V2Only = true;
5756 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005757 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005758 if (EltIdx < 0)
5759 continue;
5760 if (EltIdx < 16)
5761 V2Only = false;
5762 else
5763 V1Only = false;
5764 }
Eric Christopherfd179292009-08-27 18:07:15 +00005765
Nate Begemanb9a47b82009-02-23 08:49:38 +00005766 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005767 if (TLI.getSubtarget()->hasSSSE3() || TLI.getSubtarget()->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005768 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005769
Nate Begemanb9a47b82009-02-23 08:49:38 +00005770 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005771 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005772 //
5773 // Otherwise, we have elements from both input vectors, and must zero out
5774 // elements that come from V2 in the first mask, and V1 in the second mask
5775 // so that we can OR them together.
5776 bool TwoInputs = !(V1Only || V2Only);
5777 for (unsigned i = 0; i != 16; ++i) {
5778 int EltIdx = MaskVals[i];
5779 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005780 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005781 continue;
5782 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005783 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005784 }
5785 // If all the elements are from V2, assign it to V1 and return after
5786 // building the first pshufb.
5787 if (V2Only)
5788 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005789 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005790 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005791 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005792 if (!TwoInputs)
5793 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005794
Nate Begemanb9a47b82009-02-23 08:49:38 +00005795 // Calculate the shuffle mask for the second input, shuffle it, and
5796 // OR it with the first shuffled input.
5797 pshufbMask.clear();
5798 for (unsigned i = 0; i != 16; ++i) {
5799 int EltIdx = MaskVals[i];
5800 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005801 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005802 continue;
5803 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005804 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005805 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005806 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005807 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005808 MVT::v16i8, &pshufbMask[0], 16));
5809 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005810 }
Eric Christopherfd179292009-08-27 18:07:15 +00005811
Nate Begemanb9a47b82009-02-23 08:49:38 +00005812 // No SSSE3 - Calculate in place words and then fix all out of place words
5813 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5814 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005815 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5816 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005817 SDValue NewV = V2Only ? V2 : V1;
5818 for (int i = 0; i != 8; ++i) {
5819 int Elt0 = MaskVals[i*2];
5820 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005821
Nate Begemanb9a47b82009-02-23 08:49:38 +00005822 // This word of the result is all undef, skip it.
5823 if (Elt0 < 0 && Elt1 < 0)
5824 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005825
Nate Begemanb9a47b82009-02-23 08:49:38 +00005826 // This word of the result is already in the correct place, skip it.
5827 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5828 continue;
5829 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5830 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005831
Nate Begemanb9a47b82009-02-23 08:49:38 +00005832 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5833 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5834 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005835
5836 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5837 // using a single extract together, load it and store it.
5838 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005839 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005840 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005841 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005842 DAG.getIntPtrConstant(i));
5843 continue;
5844 }
5845
Nate Begemanb9a47b82009-02-23 08:49:38 +00005846 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005847 // source byte is not also odd, shift the extracted word left 8 bits
5848 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005849 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005850 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005851 DAG.getIntPtrConstant(Elt1 / 2));
5852 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005853 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005854 DAG.getConstant(8,
5855 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005856 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005857 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5858 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005859 }
5860 // If Elt0 is defined, extract it from the appropriate source. If the
5861 // source byte is not also even, shift the extracted word right 8 bits. If
5862 // Elt1 was also defined, OR the extracted values together before
5863 // inserting them in the result.
5864 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005865 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005866 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5867 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005868 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005869 DAG.getConstant(8,
5870 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005871 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005872 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5873 DAG.getConstant(0x00FF, MVT::i16));
5874 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005875 : InsElt0;
5876 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005877 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005878 DAG.getIntPtrConstant(i));
5879 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005880 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005881}
5882
Evan Cheng7a831ce2007-12-15 03:00:47 +00005883/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005884/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005885/// done when every pair / quad of shuffle mask elements point to elements in
5886/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005887/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005888static
Nate Begeman9008ca62009-04-27 18:41:29 +00005889SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005890 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005891 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005892 SDValue V1 = SVOp->getOperand(0);
5893 SDValue V2 = SVOp->getOperand(1);
5894 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005895 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005896 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005897 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005898 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005899 case MVT::v4f32: NewVT = MVT::v2f64; break;
5900 case MVT::v4i32: NewVT = MVT::v2i64; break;
5901 case MVT::v8i16: NewVT = MVT::v4i32; break;
5902 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005903 }
5904
Nate Begeman9008ca62009-04-27 18:41:29 +00005905 int Scale = NumElems / NewWidth;
5906 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005907 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005908 int StartIdx = -1;
5909 for (int j = 0; j < Scale; ++j) {
5910 int EltIdx = SVOp->getMaskElt(i+j);
5911 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005912 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005913 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005914 StartIdx = EltIdx - (EltIdx % Scale);
5915 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005916 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005917 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005918 if (StartIdx == -1)
5919 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005920 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005921 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005922 }
5923
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005924 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5925 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005926 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005927}
5928
Evan Chengd880b972008-05-09 21:53:03 +00005929/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005930///
Owen Andersone50ed302009-08-10 22:56:29 +00005931static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005932 SDValue SrcOp, SelectionDAG &DAG,
5933 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005934 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005935 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005936 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005937 LD = dyn_cast<LoadSDNode>(SrcOp);
5938 if (!LD) {
5939 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5940 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005941 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005942 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005943 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005944 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005945 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005946 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005947 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005948 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005949 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5950 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5951 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005952 SrcOp.getOperand(0)
5953 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005954 }
5955 }
5956 }
5957
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005958 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005959 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005960 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005961 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005962}
5963
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005964/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5965/// shuffle node referes to only one lane in the sources.
5966static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5967 EVT VT = SVOp->getValueType(0);
5968 int NumElems = VT.getVectorNumElements();
5969 int HalfSize = NumElems/2;
5970 SmallVector<int, 16> M;
5971 SVOp->getMask(M);
5972 bool MatchA = false, MatchB = false;
5973
5974 for (int l = 0; l < NumElems*2; l += HalfSize) {
5975 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5976 MatchA = true;
5977 break;
5978 }
5979 }
5980
5981 for (int l = 0; l < NumElems*2; l += HalfSize) {
5982 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
5983 MatchB = true;
5984 break;
5985 }
5986 }
5987
5988 return MatchA && MatchB;
5989}
5990
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005991/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5992/// which could not be matched by any known target speficic shuffle
5993static SDValue
5994LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005995 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
5996 // If each half of a vector shuffle node referes to only one lane in the
5997 // source vectors, extract each used 128-bit lane and shuffle them using
5998 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
5999 // the work to the legalizer.
6000 DebugLoc dl = SVOp->getDebugLoc();
6001 EVT VT = SVOp->getValueType(0);
6002 int NumElems = VT.getVectorNumElements();
6003 int HalfSize = NumElems/2;
6004
6005 // Extract the reference for each half
6006 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
6007 int FstVecOpNum = 0, SndVecOpNum = 0;
6008 for (int i = 0; i < HalfSize; ++i) {
6009 int Elt = SVOp->getMaskElt(i);
6010 if (SVOp->getMaskElt(i) < 0)
6011 continue;
6012 FstVecOpNum = Elt/NumElems;
6013 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
6014 break;
6015 }
6016 for (int i = HalfSize; i < NumElems; ++i) {
6017 int Elt = SVOp->getMaskElt(i);
6018 if (SVOp->getMaskElt(i) < 0)
6019 continue;
6020 SndVecOpNum = Elt/NumElems;
6021 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
6022 break;
6023 }
6024
6025 // Extract the subvectors
6026 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
6027 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
6028 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
6029 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
6030
6031 // Generate 128-bit shuffles
6032 SmallVector<int, 16> MaskV1, MaskV2;
6033 for (int i = 0; i < HalfSize; ++i) {
6034 int Elt = SVOp->getMaskElt(i);
6035 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
6036 }
6037 for (int i = HalfSize; i < NumElems; ++i) {
6038 int Elt = SVOp->getMaskElt(i);
6039 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
6040 }
6041
6042 EVT NVT = V1.getValueType();
6043 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
6044 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
6045
6046 // Concatenate the result back
6047 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
6048 DAG.getConstant(0, MVT::i32), DAG, dl);
6049 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
6050 DAG, dl);
6051 }
6052
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006053 return SDValue();
6054}
6055
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006056/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6057/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006058static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006059LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006060 SDValue V1 = SVOp->getOperand(0);
6061 SDValue V2 = SVOp->getOperand(1);
6062 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006063 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006064
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006065 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
6066
Evan Chengace3c172008-07-22 21:13:36 +00006067 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00006068 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006069 SmallVector<int, 8> Mask1(4U, -1);
6070 SmallVector<int, 8> PermMask;
6071 SVOp->getMask(PermMask);
6072
Evan Chengace3c172008-07-22 21:13:36 +00006073 unsigned NumHi = 0;
6074 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006075 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006076 int Idx = PermMask[i];
6077 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006078 Locs[i] = std::make_pair(-1, -1);
6079 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006080 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6081 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006082 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006083 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006084 NumLo++;
6085 } else {
6086 Locs[i] = std::make_pair(1, NumHi);
6087 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006088 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006089 NumHi++;
6090 }
6091 }
6092 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006093
Evan Chengace3c172008-07-22 21:13:36 +00006094 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006095 // If no more than two elements come from either vector. This can be
6096 // implemented with two shuffles. First shuffle gather the elements.
6097 // The second shuffle, which takes the first shuffle as both of its
6098 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006099 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006100
Nate Begeman9008ca62009-04-27 18:41:29 +00006101 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006102
Evan Chengace3c172008-07-22 21:13:36 +00006103 for (unsigned i = 0; i != 4; ++i) {
6104 if (Locs[i].first == -1)
6105 continue;
6106 else {
6107 unsigned Idx = (i < 2) ? 0 : 4;
6108 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006109 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006110 }
6111 }
6112
Nate Begeman9008ca62009-04-27 18:41:29 +00006113 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006114 } else if (NumLo == 3 || NumHi == 3) {
6115 // Otherwise, we must have three elements from one vector, call it X, and
6116 // one element from the other, call it Y. First, use a shufps to build an
6117 // intermediate vector with the one element from Y and the element from X
6118 // that will be in the same half in the final destination (the indexes don't
6119 // matter). Then, use a shufps to build the final vector, taking the half
6120 // containing the element from Y from the intermediate, and the other half
6121 // from X.
6122 if (NumHi == 3) {
6123 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00006124 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006125 std::swap(V1, V2);
6126 }
6127
6128 // Find the element from V2.
6129 unsigned HiIndex;
6130 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006131 int Val = PermMask[HiIndex];
6132 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006133 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006134 if (Val >= 4)
6135 break;
6136 }
6137
Nate Begeman9008ca62009-04-27 18:41:29 +00006138 Mask1[0] = PermMask[HiIndex];
6139 Mask1[1] = -1;
6140 Mask1[2] = PermMask[HiIndex^1];
6141 Mask1[3] = -1;
6142 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006143
6144 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006145 Mask1[0] = PermMask[0];
6146 Mask1[1] = PermMask[1];
6147 Mask1[2] = HiIndex & 1 ? 6 : 4;
6148 Mask1[3] = HiIndex & 1 ? 4 : 6;
6149 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006150 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006151 Mask1[0] = HiIndex & 1 ? 2 : 0;
6152 Mask1[1] = HiIndex & 1 ? 0 : 2;
6153 Mask1[2] = PermMask[2];
6154 Mask1[3] = PermMask[3];
6155 if (Mask1[2] >= 0)
6156 Mask1[2] += 4;
6157 if (Mask1[3] >= 0)
6158 Mask1[3] += 4;
6159 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006160 }
Evan Chengace3c172008-07-22 21:13:36 +00006161 }
6162
6163 // Break it into (shuffle shuffle_hi, shuffle_lo).
6164 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006165 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006166 SmallVector<int,8> LoMask(4U, -1);
6167 SmallVector<int,8> HiMask(4U, -1);
6168
6169 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006170 unsigned MaskIdx = 0;
6171 unsigned LoIdx = 0;
6172 unsigned HiIdx = 2;
6173 for (unsigned i = 0; i != 4; ++i) {
6174 if (i == 2) {
6175 MaskPtr = &HiMask;
6176 MaskIdx = 1;
6177 LoIdx = 0;
6178 HiIdx = 2;
6179 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006180 int Idx = PermMask[i];
6181 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006182 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006183 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006184 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006185 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006186 LoIdx++;
6187 } else {
6188 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006189 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006190 HiIdx++;
6191 }
6192 }
6193
Nate Begeman9008ca62009-04-27 18:41:29 +00006194 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6195 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6196 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006197 for (unsigned i = 0; i != 4; ++i) {
6198 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006199 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006200 } else {
6201 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006202 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006203 }
6204 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006205 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006206}
6207
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006208static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006209 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006210 V = V.getOperand(0);
6211 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6212 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006213 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6214 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6215 // BUILD_VECTOR (load), undef
6216 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006217 if (MayFoldLoad(V))
6218 return true;
6219 return false;
6220}
6221
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006222// FIXME: the version above should always be used. Since there's
6223// a bug where several vector shuffles can't be folded because the
6224// DAG is not updated during lowering and a node claims to have two
6225// uses while it only has one, use this version, and let isel match
6226// another instruction if the load really happens to have more than
6227// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006228// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006229static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006230 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006231 V = V.getOperand(0);
6232 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6233 V = V.getOperand(0);
6234 if (ISD::isNormalLoad(V.getNode()))
6235 return true;
6236 return false;
6237}
6238
6239/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6240/// a vector extract, and if both can be later optimized into a single load.
6241/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6242/// here because otherwise a target specific shuffle node is going to be
6243/// emitted for this shuffle, and the optimization not done.
6244/// FIXME: This is probably not the best approach, but fix the problem
6245/// until the right path is decided.
6246static
6247bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6248 const TargetLowering &TLI) {
6249 EVT VT = V.getValueType();
6250 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6251
6252 // Be sure that the vector shuffle is present in a pattern like this:
6253 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6254 if (!V.hasOneUse())
6255 return false;
6256
6257 SDNode *N = *V.getNode()->use_begin();
6258 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6259 return false;
6260
6261 SDValue EltNo = N->getOperand(1);
6262 if (!isa<ConstantSDNode>(EltNo))
6263 return false;
6264
6265 // If the bit convert changed the number of elements, it is unsafe
6266 // to examine the mask.
6267 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006268 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006269 EVT SrcVT = V.getOperand(0).getValueType();
6270 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6271 return false;
6272 V = V.getOperand(0);
6273 HasShuffleIntoBitcast = true;
6274 }
6275
6276 // Select the input vector, guarding against out of range extract vector.
6277 unsigned NumElems = VT.getVectorNumElements();
6278 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6279 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6280 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6281
6282 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006283 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006284 V = V.getOperand(0);
6285
6286 if (ISD::isNormalLoad(V.getNode())) {
6287 // Is the original load suitable?
6288 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6289
6290 // FIXME: avoid the multi-use bug that is preventing lots of
6291 // of foldings to be detected, this is still wrong of course, but
6292 // give the temporary desired behavior, and if it happens that
6293 // the load has real more uses, during isel it will not fold, and
6294 // will generate poor code.
6295 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6296 return false;
6297
6298 if (!HasShuffleIntoBitcast)
6299 return true;
6300
6301 // If there's a bitcast before the shuffle, check if the load type and
6302 // alignment is valid.
6303 unsigned Align = LN0->getAlignment();
6304 unsigned NewAlign =
6305 TLI.getTargetData()->getABITypeAlignment(
6306 VT.getTypeForEVT(*DAG.getContext()));
6307
6308 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6309 return false;
6310 }
6311
6312 return true;
6313}
6314
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006315static
Evan Cheng835580f2010-10-07 20:50:20 +00006316SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6317 EVT VT = Op.getValueType();
6318
6319 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006320 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6321 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006322 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6323 V1, DAG));
6324}
6325
6326static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006327SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006328 bool HasXMMInt) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006329 SDValue V1 = Op.getOperand(0);
6330 SDValue V2 = Op.getOperand(1);
6331 EVT VT = Op.getValueType();
6332
6333 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6334
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006335 if (HasXMMInt && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006336 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6337
Evan Cheng0899f5c2011-08-31 02:05:24 +00006338 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6339 return DAG.getNode(ISD::BITCAST, dl, VT,
6340 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6341 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6342 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006343}
6344
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006345static
6346SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6347 SDValue V1 = Op.getOperand(0);
6348 SDValue V2 = Op.getOperand(1);
6349 EVT VT = Op.getValueType();
6350
6351 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6352 "unsupported shuffle type");
6353
6354 if (V2.getOpcode() == ISD::UNDEF)
6355 V2 = V1;
6356
6357 // v4i32 or v4f32
6358 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6359}
6360
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006361static inline unsigned getSHUFPOpcode(EVT VT) {
6362 switch(VT.getSimpleVT().SimpleTy) {
6363 case MVT::v8i32: // Use fp unit for int unpack.
6364 case MVT::v8f32:
6365 case MVT::v4i32: // Use fp unit for int unpack.
6366 case MVT::v4f32: return X86ISD::SHUFPS;
6367 case MVT::v4i64: // Use fp unit for int unpack.
6368 case MVT::v4f64:
6369 case MVT::v2i64: // Use fp unit for int unpack.
6370 case MVT::v2f64: return X86ISD::SHUFPD;
6371 default:
6372 llvm_unreachable("Unknown type for shufp*");
6373 }
6374 return 0;
6375}
6376
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006377static
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006378SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasXMMInt) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006379 SDValue V1 = Op.getOperand(0);
6380 SDValue V2 = Op.getOperand(1);
6381 EVT VT = Op.getValueType();
6382 unsigned NumElems = VT.getVectorNumElements();
6383
6384 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6385 // operand of these instructions is only memory, so check if there's a
6386 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6387 // same masks.
6388 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006389
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006390 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006391 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006392 CanFoldLoad = true;
6393
6394 // When V1 is a load, it can be folded later into a store in isel, example:
6395 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6396 // turns into:
6397 // (MOVLPSmr addr:$src1, VR128:$src2)
6398 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006399 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006400 CanFoldLoad = true;
6401
Dan Gohman65fd6562011-11-03 21:49:52 +00006402 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006403 if (CanFoldLoad) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006404 if (HasXMMInt && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006405 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6406
6407 if (NumElems == 4)
Dan Gohman65fd6562011-11-03 21:49:52 +00006408 // If we don't care about the second element, procede to use movss.
6409 if (SVOp->getMaskElt(1) != -1)
6410 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006411 }
6412
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006413 // movl and movlp will both match v2i64, but v2i64 is never matched by
6414 // movl earlier because we make it strict to avoid messing with the movlp load
6415 // folding logic (see the code above getMOVLP call). Match it here then,
6416 // this is horrible, but will stay like this until we move all shuffle
6417 // matching to x86 specific nodes. Note that for the 1st condition all
6418 // types are matched with movsd.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006419 if (HasXMMInt) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006420 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6421 // as to remove this logic from here, as much as possible
6422 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006423 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006424 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006425 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006426
6427 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6428
6429 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006430 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006431 X86::getShuffleSHUFImmediate(SVOp), DAG);
6432}
6433
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006434static inline unsigned getUNPCKLOpcode(EVT VT) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006435 switch(VT.getSimpleVT().SimpleTy) {
6436 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
6437 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006438 case MVT::v4f32: return X86ISD::UNPCKLPS;
6439 case MVT::v2f64: return X86ISD::UNPCKLPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006440 case MVT::v8i32: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006441 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006442 case MVT::v4i64: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006443 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006444 case MVT::v16i8: return X86ISD::PUNPCKLBW;
6445 case MVT::v8i16: return X86ISD::PUNPCKLWD;
6446 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006447 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006448 }
6449 return 0;
6450}
6451
6452static inline unsigned getUNPCKHOpcode(EVT VT) {
6453 switch(VT.getSimpleVT().SimpleTy) {
6454 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
6455 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
6456 case MVT::v4f32: return X86ISD::UNPCKHPS;
6457 case MVT::v2f64: return X86ISD::UNPCKHPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006458 case MVT::v8i32: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006459 case MVT::v8f32: return X86ISD::VUNPCKHPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006460 case MVT::v4i64: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006461 case MVT::v4f64: return X86ISD::VUNPCKHPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006462 case MVT::v16i8: return X86ISD::PUNPCKHBW;
6463 case MVT::v8i16: return X86ISD::PUNPCKHWD;
6464 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006465 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006466 }
6467 return 0;
6468}
6469
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006470static inline unsigned getVPERMILOpcode(EVT VT) {
6471 switch(VT.getSimpleVT().SimpleTy) {
6472 case MVT::v4i32:
6473 case MVT::v4f32: return X86ISD::VPERMILPS;
6474 case MVT::v2i64:
6475 case MVT::v2f64: return X86ISD::VPERMILPD;
6476 case MVT::v8i32:
6477 case MVT::v8f32: return X86ISD::VPERMILPSY;
6478 case MVT::v4i64:
6479 case MVT::v4f64: return X86ISD::VPERMILPDY;
6480 default:
6481 llvm_unreachable("Unknown type for vpermil");
6482 }
6483 return 0;
6484}
6485
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006486/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
6487/// a vbroadcast node. The nodes are suitable whenever we can fold a load coming
6488/// from a 32 or 64 bit scalar. Update Op to the desired load to be folded.
6489static bool isVectorBroadcast(SDValue &Op) {
6490 EVT VT = Op.getValueType();
6491 bool Is256 = VT.getSizeInBits() == 256;
6492
6493 assert((VT.getSizeInBits() == 128 || Is256) &&
6494 "Unsupported type for vbroadcast node");
6495
6496 SDValue V = Op;
6497 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
6498 V = V.getOperand(0);
6499
6500 if (Is256 && !(V.hasOneUse() &&
6501 V.getOpcode() == ISD::INSERT_SUBVECTOR &&
6502 V.getOperand(0).getOpcode() == ISD::UNDEF))
6503 return false;
6504
6505 if (Is256)
6506 V = V.getOperand(1);
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006507
6508 if (!V.hasOneUse())
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006509 return false;
6510
6511 // Check the source scalar_to_vector type. 256-bit broadcasts are
6512 // supported for 32/64-bit sizes, while 128-bit ones are only supported
6513 // for 32-bit scalars.
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006514 if (V.getOpcode() != ISD::SCALAR_TO_VECTOR)
6515 return false;
6516
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006517 unsigned ScalarSize = V.getOperand(0).getValueType().getSizeInBits();
6518 if (ScalarSize != 32 && ScalarSize != 64)
6519 return false;
6520 if (!Is256 && ScalarSize == 64)
6521 return false;
6522
6523 V = V.getOperand(0);
6524 if (!MayFoldLoad(V))
6525 return false;
6526
6527 // Return the load node
6528 Op = V;
6529 return true;
6530}
6531
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006532static
6533SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006534 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006535 const X86Subtarget *Subtarget) {
6536 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6537 EVT VT = Op.getValueType();
6538 DebugLoc dl = Op.getDebugLoc();
6539 SDValue V1 = Op.getOperand(0);
6540 SDValue V2 = Op.getOperand(1);
6541
6542 if (isZeroShuffle(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006543 return getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006544
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006545 // Handle splat operations
6546 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006547 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006548 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006549 // Special case, this is the only place now where it's allowed to return
6550 // a vector_shuffle operation without using a target specific node, because
6551 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6552 // this be moved to DAGCombine instead?
6553 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006554 return Op;
6555
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006556 // Use vbroadcast whenever the splat comes from a foldable load
6557 if (Subtarget->hasAVX() && isVectorBroadcast(V1))
6558 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, V1);
6559
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006560 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006561 if ((Size == 128 && NumElem <= 4) ||
6562 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006563 return SDValue();
6564
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006565 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006566 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006567 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006568
6569 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6570 // do it!
6571 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6572 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6573 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006574 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006575 } else if ((VT == MVT::v4i32 ||
6576 (VT == MVT::v4f32 && Subtarget->hasXMMInt()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006577 // FIXME: Figure out a cleaner way to do this.
6578 // Try to make use of movq to zero out the top part.
6579 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6580 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6581 if (NewOp.getNode()) {
6582 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6583 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6584 DAG, Subtarget, dl);
6585 }
6586 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6587 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6588 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6589 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6590 DAG, Subtarget, dl);
6591 }
6592 }
6593 return SDValue();
6594}
6595
Dan Gohman475871a2008-07-27 21:46:04 +00006596SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006597X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006598 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006599 SDValue V1 = Op.getOperand(0);
6600 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006601 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006602 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006603 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006604 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006605 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
6606 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006607 bool V1IsSplat = false;
6608 bool V2IsSplat = false;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006609 bool HasXMMInt = Subtarget->hasXMMInt();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006610 MachineFunction &MF = DAG.getMachineFunction();
6611 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006612
Dale Johannesen0488fb62010-09-30 23:57:10 +00006613 // Shuffle operations on MMX not supported.
6614 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006615 return Op;
6616
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006617 // Vector shuffle lowering takes 3 steps:
6618 //
6619 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6620 // narrowing and commutation of operands should be handled.
6621 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6622 // shuffle nodes.
6623 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6624 // so the shuffle can be broken into other shuffles and the legalizer can
6625 // try the lowering again.
6626 //
6627 // The general ideia is that no vector_shuffle operation should be left to
6628 // be matched during isel, all of them must be converted to a target specific
6629 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006630
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006631 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6632 // narrowing and commutation of operands should be handled. The actual code
6633 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006634 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006635 if (NewOp.getNode())
6636 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006637
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006638 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6639 // unpckh_undef). Only use pshufd if speed is more important than size.
6640 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006641 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006642 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006643 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006644
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006645 if (X86::isMOVDDUPMask(SVOp) &&
6646 (Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
6647 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006648 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006649
Dale Johannesen0488fb62010-09-30 23:57:10 +00006650 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006651 return getMOVHighToLow(Op, dl, DAG);
6652
6653 // Use to match splats
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006654 if (HasXMMInt && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006655 (VT == MVT::v2f64 || VT == MVT::v2i64))
6656 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
6657
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006658 if (X86::isPSHUFDMask(SVOp)) {
6659 // The actual implementation will match the mask in the if above and then
6660 // during isel it can match several different instructions, not only pshufd
6661 // as its name says, sad but true, emulate the behavior for now...
6662 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6663 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6664
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006665 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6666
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006667 if (HasXMMInt && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006668 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6669
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006670 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6671 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006672 }
Eric Christopherfd179292009-08-27 18:07:15 +00006673
Evan Chengf26ffe92008-05-29 08:22:04 +00006674 // Check if this can be converted into a logical shift.
6675 bool isLeft = false;
6676 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006677 SDValue ShVal;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006678 bool isShift = getSubtarget()->hasXMMInt() &&
6679 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006680 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006681 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006682 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006683 EVT EltVT = VT.getVectorElementType();
6684 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006685 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006686 }
Eric Christopherfd179292009-08-27 18:07:15 +00006687
Nate Begeman9008ca62009-04-27 18:41:29 +00006688 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006689 if (V1IsUndef)
6690 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00006691 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006692 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006693 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006694 if (HasXMMInt && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006695 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6696
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006697 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006698 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6699 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006700 }
Eric Christopherfd179292009-08-27 18:07:15 +00006701
Nate Begeman9008ca62009-04-27 18:41:29 +00006702 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00006703 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006704 return getMOVLowToHigh(Op, dl, DAG, HasXMMInt);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006705
Dale Johannesen0488fb62010-09-30 23:57:10 +00006706 if (X86::isMOVHLPSMask(SVOp))
6707 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006708
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006709 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006710 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006711
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006712 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006713 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006714
Dale Johannesen0488fb62010-09-30 23:57:10 +00006715 if (X86::isMOVLPMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006716 return getMOVLP(Op, dl, DAG, HasXMMInt);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006717
Nate Begeman9008ca62009-04-27 18:41:29 +00006718 if (ShouldXformToMOVHLPS(SVOp) ||
6719 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6720 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006721
Evan Chengf26ffe92008-05-29 08:22:04 +00006722 if (isShift) {
6723 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006724 EVT EltVT = VT.getVectorElementType();
6725 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006726 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006727 }
Eric Christopherfd179292009-08-27 18:07:15 +00006728
Evan Cheng9eca5e82006-10-25 21:49:50 +00006729 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006730 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6731 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006732 V1IsSplat = isSplatVector(V1.getNode());
6733 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006734
Chris Lattner8a594482007-11-25 00:24:49 +00006735 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00006736 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006737 Op = CommuteVectorShuffle(SVOp, DAG);
6738 SVOp = cast<ShuffleVectorSDNode>(Op);
6739 V1 = SVOp->getOperand(0);
6740 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006741 std::swap(V1IsSplat, V2IsSplat);
6742 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006743 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006744 }
6745
Nate Begeman9008ca62009-04-27 18:41:29 +00006746 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6747 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006748 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006749 return V1;
6750 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6751 // the instruction selector will not match, so get a canonical MOVL with
6752 // swapped operands to undo the commute.
6753 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006754 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006755
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006756 if (X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006757 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006758
6759 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006760 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006761
Evan Cheng9bbbb982006-10-25 20:48:19 +00006762 if (V2IsSplat) {
6763 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006764 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006765 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006766 SDValue NewMask = NormalizeMask(SVOp, DAG);
6767 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6768 if (NSVOp != SVOp) {
6769 if (X86::isUNPCKLMask(NSVOp, true)) {
6770 return NewMask;
6771 } else if (X86::isUNPCKHMask(NSVOp, true)) {
6772 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006773 }
6774 }
6775 }
6776
Evan Cheng9eca5e82006-10-25 21:49:50 +00006777 if (Commuted) {
6778 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006779 // FIXME: this seems wrong.
6780 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6781 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006782
6783 if (X86::isUNPCKLMask(NewSVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006784 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006785
6786 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006787 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006788 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006789
Nate Begeman9008ca62009-04-27 18:41:29 +00006790 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006791 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006792 return CommuteVectorShuffle(SVOp, DAG);
6793
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006794 // The checks below are all present in isShuffleMaskLegal, but they are
6795 // inlined here right now to enable us to directly emit target specific
6796 // nodes, and remove one by one until they don't return Op anymore.
6797 SmallVector<int, 16> M;
6798 SVOp->getMask(M);
6799
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006800 if (isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006801 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6802 X86::getShufflePALIGNRImmediate(SVOp),
6803 DAG);
6804
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006805 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6806 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006807 if (VT == MVT::v2f64)
6808 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006809 if (VT == MVT::v2i64)
6810 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6811 }
6812
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006813 if (isPSHUFHWMask(M, VT))
6814 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6815 X86::getShufflePSHUFHWImmediate(SVOp),
6816 DAG);
6817
6818 if (isPSHUFLWMask(M, VT))
6819 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6820 X86::getShufflePSHUFLWImmediate(SVOp),
6821 DAG);
6822
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006823 if (isSHUFPMask(M, VT))
6824 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6825 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006826
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006827 if (X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006828 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006829 if (X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006830 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006831
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006832 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006833 // Generate target specific nodes for 128 or 256-bit shuffles only
6834 // supported in the AVX instruction set.
6835 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006836
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006837 // Handle VMOVDDUPY permutations
6838 if (isMOVDDUPYMask(SVOp, Subtarget))
6839 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6840
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006841 // Handle VPERMILPS* permutations
6842 if (isVPERMILPSMask(M, VT, Subtarget))
6843 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6844 getShuffleVPERMILPSImmediate(SVOp), DAG);
6845
6846 // Handle VPERMILPD* permutations
6847 if (isVPERMILPDMask(M, VT, Subtarget))
6848 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6849 getShuffleVPERMILPDImmediate(SVOp), DAG);
6850
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00006851 // Handle VPERM2F128 permutations
6852 if (isVPERM2F128Mask(M, VT, Subtarget))
6853 return getTargetShuffleNode(X86ISD::VPERM2F128, dl, VT, V1, V2,
6854 getShuffleVPERM2F128Immediate(SVOp), DAG);
6855
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006856 // Handle VSHUFPSY permutations
6857 if (isVSHUFPSYMask(M, VT, Subtarget))
6858 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6859 getShuffleVSHUFPSYImmediate(SVOp), DAG);
6860
6861 // Handle VSHUFPDY permutations
6862 if (isVSHUFPDYMask(M, VT, Subtarget))
6863 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6864 getShuffleVSHUFPDYImmediate(SVOp), DAG);
6865
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006866 //===--------------------------------------------------------------------===//
6867 // Since no target specific shuffle was selected for this generic one,
6868 // lower it into other known shuffles. FIXME: this isn't true yet, but
6869 // this is the plan.
6870 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006871
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006872 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6873 if (VT == MVT::v8i16) {
6874 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6875 if (NewOp.getNode())
6876 return NewOp;
6877 }
6878
6879 if (VT == MVT::v16i8) {
6880 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6881 if (NewOp.getNode())
6882 return NewOp;
6883 }
6884
6885 // Handle all 128-bit wide vectors with 4 elements, and match them with
6886 // several different shuffle types.
6887 if (NumElems == 4 && VT.getSizeInBits() == 128)
6888 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6889
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006890 // Handle general 256-bit shuffles
6891 if (VT.is256BitVector())
6892 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6893
Dan Gohman475871a2008-07-27 21:46:04 +00006894 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006895}
6896
Dan Gohman475871a2008-07-27 21:46:04 +00006897SDValue
6898X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006899 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006900 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006901 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006902
6903 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6904 return SDValue();
6905
Duncan Sands83ec4b62008-06-06 12:08:01 +00006906 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006907 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006908 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006909 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006910 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006911 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006912 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006913 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6914 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6915 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006916 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6917 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006918 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006919 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006920 Op.getOperand(0)),
6921 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006922 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006923 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006924 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006925 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006926 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006927 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006928 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6929 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006930 // result has a single use which is a store or a bitcast to i32. And in
6931 // the case of a store, it's not worth it if the index is a constant 0,
6932 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006933 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006934 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006935 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006936 if ((User->getOpcode() != ISD::STORE ||
6937 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6938 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006939 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006940 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006941 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006942 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006943 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006944 Op.getOperand(0)),
6945 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006946 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006947 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006948 // ExtractPS works with constant index.
6949 if (isa<ConstantSDNode>(Op.getOperand(1)))
6950 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006951 }
Dan Gohman475871a2008-07-27 21:46:04 +00006952 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006953}
6954
6955
Dan Gohman475871a2008-07-27 21:46:04 +00006956SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006957X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6958 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006959 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006960 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006961
David Greene74a579d2011-02-10 16:57:36 +00006962 SDValue Vec = Op.getOperand(0);
6963 EVT VecVT = Vec.getValueType();
6964
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006965 // If this is a 256-bit vector result, first extract the 128-bit vector and
6966 // then extract the element from the 128-bit vector.
6967 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006968 DebugLoc dl = Op.getNode()->getDebugLoc();
6969 unsigned NumElems = VecVT.getVectorNumElements();
6970 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006971 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6972
6973 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006974 bool Upper = IdxVal >= NumElems/2;
6975 Vec = Extract128BitVector(Vec,
6976 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006977
David Greene74a579d2011-02-10 16:57:36 +00006978 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006979 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006980 }
6981
6982 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6983
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006984 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006985 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006986 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006987 return Res;
6988 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006989
Owen Andersone50ed302009-08-10 22:56:29 +00006990 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006991 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006992 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006993 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006994 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006995 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006996 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006997 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6998 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006999 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007000 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007001 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007002 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007003 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007004 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007005 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007006 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007007 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007008 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007009 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007010 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007011 if (Idx == 0)
7012 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007013
Evan Cheng0db9fe62006-04-25 20:13:52 +00007014 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007015 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007016 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007017 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007018 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007019 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007020 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00007021 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007022 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7023 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7024 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007025 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007026 if (Idx == 0)
7027 return Op;
7028
7029 // UNPCKHPD the element to the lowest double word, then movsd.
7030 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7031 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007032 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007033 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007034 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007035 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007036 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007037 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007038 }
7039
Dan Gohman475871a2008-07-27 21:46:04 +00007040 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007041}
7042
Dan Gohman475871a2008-07-27 21:46:04 +00007043SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007044X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7045 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007046 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007047 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007048 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007049
Dan Gohman475871a2008-07-27 21:46:04 +00007050 SDValue N0 = Op.getOperand(0);
7051 SDValue N1 = Op.getOperand(1);
7052 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007053
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007054 if (VT.getSizeInBits() == 256)
7055 return SDValue();
7056
Dan Gohman8a55ce42009-09-23 21:02:20 +00007057 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007058 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007059 unsigned Opc;
7060 if (VT == MVT::v8i16)
7061 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007062 else if (VT == MVT::v16i8)
7063 Opc = X86ISD::PINSRB;
7064 else
7065 Opc = X86ISD::PINSRB;
7066
Nate Begeman14d12ca2008-02-11 04:19:36 +00007067 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7068 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007069 if (N1.getValueType() != MVT::i32)
7070 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7071 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007072 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007073 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007074 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007075 // Bits [7:6] of the constant are the source select. This will always be
7076 // zero here. The DAG Combiner may combine an extract_elt index into these
7077 // bits. For example (insert (extract, 3), 2) could be matched by putting
7078 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007079 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007080 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007081 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007082 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007083 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007084 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007085 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007086 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007087 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007088 // PINSR* works with constant index.
7089 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007090 }
Dan Gohman475871a2008-07-27 21:46:04 +00007091 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007092}
7093
Dan Gohman475871a2008-07-27 21:46:04 +00007094SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007095X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007096 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007097 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007098
David Greene6b381262011-02-09 15:32:06 +00007099 DebugLoc dl = Op.getDebugLoc();
7100 SDValue N0 = Op.getOperand(0);
7101 SDValue N1 = Op.getOperand(1);
7102 SDValue N2 = Op.getOperand(2);
7103
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007104 // If this is a 256-bit vector result, first extract the 128-bit vector,
7105 // insert the element into the extracted half and then place it back.
7106 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007107 if (!isa<ConstantSDNode>(N2))
7108 return SDValue();
7109
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007110 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007111 unsigned NumElems = VT.getVectorNumElements();
7112 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007113 bool Upper = IdxVal >= NumElems/2;
7114 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
7115 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007116
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007117 // Insert the element into the desired half.
7118 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
7119 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00007120
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007121 // Insert the changed part back to the 256-bit vector
7122 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007123 }
7124
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007125 if (Subtarget->hasSSE41() || Subtarget->hasAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007126 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7127
Dan Gohman8a55ce42009-09-23 21:02:20 +00007128 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007129 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007130
Dan Gohman8a55ce42009-09-23 21:02:20 +00007131 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007132 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7133 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007134 if (N1.getValueType() != MVT::i32)
7135 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7136 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007137 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007138 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007139 }
Dan Gohman475871a2008-07-27 21:46:04 +00007140 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007141}
7142
Dan Gohman475871a2008-07-27 21:46:04 +00007143SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007144X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007145 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007146 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007147 EVT OpVT = Op.getValueType();
7148
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007149 // If this is a 256-bit vector result, first insert into a 128-bit
7150 // vector and then insert into the 256-bit vector.
7151 if (OpVT.getSizeInBits() > 128) {
7152 // Insert into a 128-bit vector.
7153 EVT VT128 = EVT::getVectorVT(*Context,
7154 OpVT.getVectorElementType(),
7155 OpVT.getVectorNumElements() / 2);
7156
7157 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7158
7159 // Insert the 128-bit vector.
7160 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7161 DAG.getConstant(0, MVT::i32),
7162 DAG, dl);
7163 }
7164
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007165 if (Op.getValueType() == MVT::v1i64 &&
7166 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007167 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007168
Owen Anderson825b72b2009-08-11 20:47:22 +00007169 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007170 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7171 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007172 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007173 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007174}
7175
David Greene91585092011-01-26 15:38:49 +00007176// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7177// a simple subregister reference or explicit instructions to grab
7178// upper bits of a vector.
7179SDValue
7180X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7181 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007182 DebugLoc dl = Op.getNode()->getDebugLoc();
7183 SDValue Vec = Op.getNode()->getOperand(0);
7184 SDValue Idx = Op.getNode()->getOperand(1);
7185
7186 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7187 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7188 return Extract128BitVector(Vec, Idx, DAG, dl);
7189 }
David Greene91585092011-01-26 15:38:49 +00007190 }
7191 return SDValue();
7192}
7193
David Greenecfe33c42011-01-26 19:13:22 +00007194// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7195// simple superregister reference or explicit instructions to insert
7196// the upper bits of a vector.
7197SDValue
7198X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7199 if (Subtarget->hasAVX()) {
7200 DebugLoc dl = Op.getNode()->getDebugLoc();
7201 SDValue Vec = Op.getNode()->getOperand(0);
7202 SDValue SubVec = Op.getNode()->getOperand(1);
7203 SDValue Idx = Op.getNode()->getOperand(2);
7204
7205 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7206 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007207 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007208 }
7209 }
7210 return SDValue();
7211}
7212
Bill Wendling056292f2008-09-16 21:48:12 +00007213// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7214// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7215// one of the above mentioned nodes. It has to be wrapped because otherwise
7216// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7217// be used to form addressing mode. These wrapped nodes will be selected
7218// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007219SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007220X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007221 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007222
Chris Lattner41621a22009-06-26 19:22:52 +00007223 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7224 // global base reg.
7225 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007226 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007227 CodeModel::Model M = getTargetMachine().getCodeModel();
7228
Chris Lattner4f066492009-07-11 20:29:19 +00007229 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007230 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007231 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007232 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007233 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007234 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007235 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007236
Evan Cheng1606e8e2009-03-13 07:51:59 +00007237 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007238 CP->getAlignment(),
7239 CP->getOffset(), OpFlag);
7240 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007241 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007242 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007243 if (OpFlag) {
7244 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007245 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007246 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007247 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007248 }
7249
7250 return Result;
7251}
7252
Dan Gohmand858e902010-04-17 15:26:15 +00007253SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007254 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007255
Chris Lattner18c59872009-06-27 04:16:01 +00007256 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7257 // global base reg.
7258 unsigned char OpFlag = 0;
7259 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007260 CodeModel::Model M = getTargetMachine().getCodeModel();
7261
Chris Lattner4f066492009-07-11 20:29:19 +00007262 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007263 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007264 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007265 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007266 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007267 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007268 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007269
Chris Lattner18c59872009-06-27 04:16:01 +00007270 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7271 OpFlag);
7272 DebugLoc DL = JT->getDebugLoc();
7273 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007274
Chris Lattner18c59872009-06-27 04:16:01 +00007275 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007276 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007277 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7278 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007279 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007280 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007281
Chris Lattner18c59872009-06-27 04:16:01 +00007282 return Result;
7283}
7284
7285SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007286X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007287 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007288
Chris Lattner18c59872009-06-27 04:16:01 +00007289 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7290 // global base reg.
7291 unsigned char OpFlag = 0;
7292 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007293 CodeModel::Model M = getTargetMachine().getCodeModel();
7294
Chris Lattner4f066492009-07-11 20:29:19 +00007295 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007296 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7297 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7298 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007299 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007300 } else if (Subtarget->isPICStyleGOT()) {
7301 OpFlag = X86II::MO_GOT;
7302 } else if (Subtarget->isPICStyleStubPIC()) {
7303 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7304 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7305 OpFlag = X86II::MO_DARWIN_NONLAZY;
7306 }
Eric Christopherfd179292009-08-27 18:07:15 +00007307
Chris Lattner18c59872009-06-27 04:16:01 +00007308 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007309
Chris Lattner18c59872009-06-27 04:16:01 +00007310 DebugLoc DL = Op.getDebugLoc();
7311 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007312
7313
Chris Lattner18c59872009-06-27 04:16:01 +00007314 // With PIC, the address is actually $g + Offset.
7315 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007316 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007317 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7318 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007319 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007320 Result);
7321 }
Eric Christopherfd179292009-08-27 18:07:15 +00007322
Eli Friedman586272d2011-08-11 01:48:05 +00007323 // For symbols that require a load from a stub to get the address, emit the
7324 // load.
7325 if (isGlobalStubReference(OpFlag))
7326 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007327 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007328
Chris Lattner18c59872009-06-27 04:16:01 +00007329 return Result;
7330}
7331
Dan Gohman475871a2008-07-27 21:46:04 +00007332SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007333X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007334 // Create the TargetBlockAddressAddress node.
7335 unsigned char OpFlags =
7336 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007337 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007338 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007339 DebugLoc dl = Op.getDebugLoc();
7340 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7341 /*isTarget=*/true, OpFlags);
7342
Dan Gohmanf705adb2009-10-30 01:28:02 +00007343 if (Subtarget->isPICStyleRIPRel() &&
7344 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007345 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7346 else
7347 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007348
Dan Gohman29cbade2009-11-20 23:18:13 +00007349 // With PIC, the address is actually $g + Offset.
7350 if (isGlobalRelativeToPICBase(OpFlags)) {
7351 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7352 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7353 Result);
7354 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007355
7356 return Result;
7357}
7358
7359SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007360X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007361 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007362 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007363 // Create the TargetGlobalAddress node, folding in the constant
7364 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007365 unsigned char OpFlags =
7366 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007367 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007368 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007369 if (OpFlags == X86II::MO_NO_FLAG &&
7370 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007371 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007372 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007373 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007374 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007375 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007376 }
Eric Christopherfd179292009-08-27 18:07:15 +00007377
Chris Lattner4f066492009-07-11 20:29:19 +00007378 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007379 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007380 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7381 else
7382 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007383
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007384 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007385 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007386 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7387 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007388 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007389 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007390
Chris Lattner36c25012009-07-10 07:34:39 +00007391 // For globals that require a load from a stub to get the address, emit the
7392 // load.
7393 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007394 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007395 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007396
Dan Gohman6520e202008-10-18 02:06:02 +00007397 // If there was a non-zero offset that we didn't fold, create an explicit
7398 // addition for it.
7399 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007400 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007401 DAG.getConstant(Offset, getPointerTy()));
7402
Evan Cheng0db9fe62006-04-25 20:13:52 +00007403 return Result;
7404}
7405
Evan Chengda43bcf2008-09-24 00:05:32 +00007406SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007407X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007408 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007409 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007410 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007411}
7412
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007413static SDValue
7414GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007415 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007416 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007417 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007418 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007419 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007420 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007421 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007422 GA->getOffset(),
7423 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007424 if (InFlag) {
7425 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007426 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007427 } else {
7428 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007429 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007430 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007431
7432 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007433 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007434
Rafael Espindola15f1b662009-04-24 12:59:40 +00007435 SDValue Flag = Chain.getValue(1);
7436 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007437}
7438
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007439// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007440static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007441LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007442 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007443 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007444 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7445 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007446 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007447 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007448 InFlag = Chain.getValue(1);
7449
Chris Lattnerb903bed2009-06-26 21:20:29 +00007450 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007451}
7452
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007453// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007454static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007455LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007456 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007457 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7458 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007459}
7460
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007461// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7462// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007463static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007464 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007465 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007466 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007467
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007468 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7469 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7470 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007471
Michael J. Spencerec38de22010-10-10 22:04:20 +00007472 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007473 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007474 MachinePointerInfo(Ptr),
7475 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007476
Chris Lattnerb903bed2009-06-26 21:20:29 +00007477 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007478 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7479 // initialexec.
7480 unsigned WrapperKind = X86ISD::Wrapper;
7481 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007482 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007483 } else if (is64Bit) {
7484 assert(model == TLSModel::InitialExec);
7485 OperandFlags = X86II::MO_GOTTPOFF;
7486 WrapperKind = X86ISD::WrapperRIP;
7487 } else {
7488 assert(model == TLSModel::InitialExec);
7489 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007490 }
Eric Christopherfd179292009-08-27 18:07:15 +00007491
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007492 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7493 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007494 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007495 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007496 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007497 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007498
Rafael Espindola9a580232009-02-27 13:37:18 +00007499 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007500 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007501 MachinePointerInfo::getGOT(), false, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007502
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007503 // The address of the thread local variable is the add of the thread
7504 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007505 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007506}
7507
Dan Gohman475871a2008-07-27 21:46:04 +00007508SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007509X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007510
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007511 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007512 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007513
Eric Christopher30ef0e52010-06-03 04:07:48 +00007514 if (Subtarget->isTargetELF()) {
7515 // TODO: implement the "local dynamic" model
7516 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007517
Eric Christopher30ef0e52010-06-03 04:07:48 +00007518 // If GV is an alias then use the aliasee for determining
7519 // thread-localness.
7520 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7521 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007522
7523 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007524 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007525
Eric Christopher30ef0e52010-06-03 04:07:48 +00007526 switch (model) {
7527 case TLSModel::GeneralDynamic:
7528 case TLSModel::LocalDynamic: // not implemented
7529 if (Subtarget->is64Bit())
7530 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7531 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007532
Eric Christopher30ef0e52010-06-03 04:07:48 +00007533 case TLSModel::InitialExec:
7534 case TLSModel::LocalExec:
7535 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7536 Subtarget->is64Bit());
7537 }
7538 } else if (Subtarget->isTargetDarwin()) {
7539 // Darwin only has one model of TLS. Lower to that.
7540 unsigned char OpFlag = 0;
7541 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7542 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007543
Eric Christopher30ef0e52010-06-03 04:07:48 +00007544 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7545 // global base reg.
7546 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7547 !Subtarget->is64Bit();
7548 if (PIC32)
7549 OpFlag = X86II::MO_TLVP_PIC_BASE;
7550 else
7551 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007552 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007553 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007554 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007555 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007556 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007557
Eric Christopher30ef0e52010-06-03 04:07:48 +00007558 // With PIC32, the address is actually $g + Offset.
7559 if (PIC32)
7560 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7561 DAG.getNode(X86ISD::GlobalBaseReg,
7562 DebugLoc(), getPointerTy()),
7563 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007564
Eric Christopher30ef0e52010-06-03 04:07:48 +00007565 // Lowering the machine isd will make sure everything is in the right
7566 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007567 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007568 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007569 SDValue Args[] = { Chain, Offset };
7570 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007571
Eric Christopher30ef0e52010-06-03 04:07:48 +00007572 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7573 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7574 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007575
Eric Christopher30ef0e52010-06-03 04:07:48 +00007576 // And our return value (tls address) is in the standard call return value
7577 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007578 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007579 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7580 Chain.getValue(1));
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007581 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007582
Eric Christopher30ef0e52010-06-03 04:07:48 +00007583 assert(false &&
7584 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007585
Torok Edwinc23197a2009-07-14 16:55:14 +00007586 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007587 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007588}
7589
Evan Cheng0db9fe62006-04-25 20:13:52 +00007590
Nadav Rotem43012222011-05-11 08:12:09 +00007591/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007592/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007593SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007594 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007595 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007596 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007597 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007598 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007599 SDValue ShOpLo = Op.getOperand(0);
7600 SDValue ShOpHi = Op.getOperand(1);
7601 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007602 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007603 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007604 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007605
Dan Gohman475871a2008-07-27 21:46:04 +00007606 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007607 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007608 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7609 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007610 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007611 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7612 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007613 }
Evan Chenge3413162006-01-09 18:33:28 +00007614
Owen Anderson825b72b2009-08-11 20:47:22 +00007615 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7616 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007617 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007618 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007619
Dan Gohman475871a2008-07-27 21:46:04 +00007620 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007621 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007622 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7623 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007624
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007625 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007626 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7627 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007628 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007629 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7630 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007631 }
7632
Dan Gohman475871a2008-07-27 21:46:04 +00007633 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007634 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007635}
Evan Chenga3195e82006-01-12 22:54:21 +00007636
Dan Gohmand858e902010-04-17 15:26:15 +00007637SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7638 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007639 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007640
Dale Johannesen0488fb62010-09-30 23:57:10 +00007641 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007642 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007643
Owen Anderson825b72b2009-08-11 20:47:22 +00007644 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007645 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007646
Eli Friedman36df4992009-05-27 00:47:34 +00007647 // These are really Legal; return the operand so the caller accepts it as
7648 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007649 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007650 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007651 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007652 Subtarget->is64Bit()) {
7653 return Op;
7654 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007655
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007656 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007657 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007658 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007659 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007660 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007661 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007662 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007663 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007664 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007665 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7666}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007667
Owen Andersone50ed302009-08-10 22:56:29 +00007668SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007669 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007670 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007671 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007672 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007673 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007674 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007675 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007676 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007677 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007678 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007679
Chris Lattner492a43e2010-09-22 01:28:21 +00007680 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007681
Stuart Hastings84be9582011-06-02 15:57:11 +00007682 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7683 MachineMemOperand *MMO;
7684 if (FI) {
7685 int SSFI = FI->getIndex();
7686 MMO =
7687 DAG.getMachineFunction()
7688 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7689 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7690 } else {
7691 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7692 StackSlot = StackSlot.getOperand(1);
7693 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007694 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007695 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7696 X86ISD::FILD, DL,
7697 Tys, Ops, array_lengthof(Ops),
7698 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007699
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007700 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007701 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007702 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007703
7704 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7705 // shouldn't be necessary except that RFP cannot be live across
7706 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007707 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007708 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7709 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007710 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007711 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007712 SDValue Ops[] = {
7713 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7714 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007715 MachineMemOperand *MMO =
7716 DAG.getMachineFunction()
7717 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007718 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007719
Chris Lattner492a43e2010-09-22 01:28:21 +00007720 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7721 Ops, array_lengthof(Ops),
7722 Op.getValueType(), MMO);
7723 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007724 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007725 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007726 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007727
Evan Cheng0db9fe62006-04-25 20:13:52 +00007728 return Result;
7729}
7730
Bill Wendling8b8a6362009-01-17 03:56:04 +00007731// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007732SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7733 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007734 // This algorithm is not obvious. Here it is in C code, more or less:
7735 /*
7736 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7737 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7738 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007739
Bill Wendling8b8a6362009-01-17 03:56:04 +00007740 // Copy ints to xmm registers.
7741 __m128i xh = _mm_cvtsi32_si128( hi );
7742 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007743
Bill Wendling8b8a6362009-01-17 03:56:04 +00007744 // Combine into low half of a single xmm register.
7745 __m128i x = _mm_unpacklo_epi32( xh, xl );
7746 __m128d d;
7747 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007748
Bill Wendling8b8a6362009-01-17 03:56:04 +00007749 // Merge in appropriate exponents to give the integer bits the right
7750 // magnitude.
7751 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007752
Bill Wendling8b8a6362009-01-17 03:56:04 +00007753 // Subtract away the biases to deal with the IEEE-754 double precision
7754 // implicit 1.
7755 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007756
Bill Wendling8b8a6362009-01-17 03:56:04 +00007757 // All conversions up to here are exact. The correctly rounded result is
7758 // calculated using the current rounding mode using the following
7759 // horizontal add.
7760 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7761 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7762 // store doesn't really need to be here (except
7763 // maybe to zero the other double)
7764 return sd;
7765 }
7766 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007767
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007768 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007769 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007770
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007771 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007772 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007773 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7774 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7775 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7776 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007777 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007778 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007779
Bill Wendling8b8a6362009-01-17 03:56:04 +00007780 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007781 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007782 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007783 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007784 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007785 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007786 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007787
Owen Anderson825b72b2009-08-11 20:47:22 +00007788 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7789 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007790 Op.getOperand(0),
7791 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007792 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7793 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007794 Op.getOperand(0),
7795 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007796 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7797 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007798 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007799 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007800 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007801 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007802 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007803 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007804 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007805 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007806
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007807 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007808 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007809 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7810 DAG.getUNDEF(MVT::v2f64), ShufMask);
7811 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7812 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007813 DAG.getIntPtrConstant(0));
7814}
7815
Bill Wendling8b8a6362009-01-17 03:56:04 +00007816// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007817SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7818 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007819 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007820 // FP constant to bias correct the final result.
7821 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007822 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007823
7824 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007825 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007826 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007827
Eli Friedmanf3704762011-08-29 21:15:46 +00007828 // Zero out the upper parts of the register.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00007829 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasXMMInt(),
7830 DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007831
Owen Anderson825b72b2009-08-11 20:47:22 +00007832 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007833 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007834 DAG.getIntPtrConstant(0));
7835
7836 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007837 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007838 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007839 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007840 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007841 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007842 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007843 MVT::v2f64, Bias)));
7844 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007845 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007846 DAG.getIntPtrConstant(0));
7847
7848 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007849 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007850
7851 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007852 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007853
Owen Anderson825b72b2009-08-11 20:47:22 +00007854 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007855 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007856 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007857 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007858 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007859 }
7860
7861 // Handle final rounding.
7862 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007863}
7864
Dan Gohmand858e902010-04-17 15:26:15 +00007865SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7866 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007867 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007868 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007869
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007870 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007871 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7872 // the optimization here.
7873 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007874 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007875
Owen Andersone50ed302009-08-10 22:56:29 +00007876 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007877 EVT DstVT = Op.getValueType();
7878 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007879 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007880 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007881 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007882
7883 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007884 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007885 if (SrcVT == MVT::i32) {
7886 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7887 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7888 getPointerTy(), StackSlot, WordOff);
7889 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007890 StackSlot, MachinePointerInfo(),
7891 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007892 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007893 OffsetSlot, MachinePointerInfo(),
7894 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007895 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7896 return Fild;
7897 }
7898
7899 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7900 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007901 StackSlot, MachinePointerInfo(),
7902 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007903 // For i64 source, we need to add the appropriate power of 2 if the input
7904 // was negative. This is the same as the optimization in
7905 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7906 // we must be careful to do the computation in x87 extended precision, not
7907 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007908 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7909 MachineMemOperand *MMO =
7910 DAG.getMachineFunction()
7911 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7912 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007913
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007914 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7915 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007916 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7917 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007918
7919 APInt FF(32, 0x5F800000ULL);
7920
7921 // Check whether the sign bit is set.
7922 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7923 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7924 ISD::SETLT);
7925
7926 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7927 SDValue FudgePtr = DAG.getConstantPool(
7928 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7929 getPointerTy());
7930
7931 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7932 SDValue Zero = DAG.getIntPtrConstant(0);
7933 SDValue Four = DAG.getIntPtrConstant(4);
7934 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7935 Zero, Four);
7936 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7937
7938 // Load the value out, extending it from f32 to f80.
7939 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007940 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007941 FudgePtr, MachinePointerInfo::getConstantPool(),
7942 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007943 // Extend everything to 80 bits to force it to be done on x87.
7944 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7945 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007946}
7947
Dan Gohman475871a2008-07-27 21:46:04 +00007948std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007949FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007950 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007951
Owen Andersone50ed302009-08-10 22:56:29 +00007952 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007953
7954 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007955 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7956 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007957 }
7958
Owen Anderson825b72b2009-08-11 20:47:22 +00007959 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7960 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007961 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007962
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007963 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007964 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007965 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007966 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007967 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007968 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007969 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007970 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007971
Evan Cheng87c89352007-10-15 20:11:21 +00007972 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7973 // stack slot.
7974 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007975 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007976 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007977 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007978
Michael J. Spencerec38de22010-10-10 22:04:20 +00007979
7980
Evan Cheng0db9fe62006-04-25 20:13:52 +00007981 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007982 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007983 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007984 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7985 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7986 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007987 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007988
Dan Gohman475871a2008-07-27 21:46:04 +00007989 SDValue Chain = DAG.getEntryNode();
7990 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007991 EVT TheVT = Op.getOperand(0).getValueType();
7992 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007993 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007994 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007995 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007996 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007997 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007998 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007999 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008000 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008001
Chris Lattner492a43e2010-09-22 01:28:21 +00008002 MachineMemOperand *MMO =
8003 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8004 MachineMemOperand::MOLoad, MemSize, MemSize);
8005 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8006 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008007 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008008 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008009 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8010 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008011
Chris Lattner07290932010-09-22 01:05:16 +00008012 MachineMemOperand *MMO =
8013 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8014 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008015
Evan Cheng0db9fe62006-04-25 20:13:52 +00008016 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00008017 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00008018 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8019 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00008020
Chris Lattner27a6c732007-11-24 07:07:01 +00008021 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008022}
8023
Dan Gohmand858e902010-04-17 15:26:15 +00008024SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8025 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00008026 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008027 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008028
Eli Friedman948e95a2009-05-23 09:59:16 +00008029 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00008030 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008031 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8032 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008033
Chris Lattner27a6c732007-11-24 07:07:01 +00008034 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008035 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008036 FIST, StackSlot, MachinePointerInfo(),
8037 false, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00008038}
8039
Dan Gohmand858e902010-04-17 15:26:15 +00008040SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8041 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00008042 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
8043 SDValue FIST = Vals.first, StackSlot = Vals.second;
8044 assert(FIST.getNode() && "Unexpected failure");
8045
8046 // Load the result.
8047 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008048 FIST, StackSlot, MachinePointerInfo(),
8049 false, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00008050}
8051
Dan Gohmand858e902010-04-17 15:26:15 +00008052SDValue X86TargetLowering::LowerFABS(SDValue Op,
8053 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008054 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008055 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008056 EVT VT = Op.getValueType();
8057 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008058 if (VT.isVector())
8059 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008060 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008061 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008062 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00008063 CV.push_back(C);
8064 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008065 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008066 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00008067 CV.push_back(C);
8068 CV.push_back(C);
8069 CV.push_back(C);
8070 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008071 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008072 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008073 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008074 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008075 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008076 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008077 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008078}
8079
Dan Gohmand858e902010-04-17 15:26:15 +00008080SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008081 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008082 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008083 EVT VT = Op.getValueType();
8084 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00008085 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008086 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008087 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008088 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008089 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00008090 CV.push_back(C);
8091 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008092 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008093 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00008094 CV.push_back(C);
8095 CV.push_back(C);
8096 CV.push_back(C);
8097 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008098 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008099 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008100 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008101 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008102 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008103 false, false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008104 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008105 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008106 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008107 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008108 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008109 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008110 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008111 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00008112 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008113}
8114
Dan Gohmand858e902010-04-17 15:26:15 +00008115SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008116 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008117 SDValue Op0 = Op.getOperand(0);
8118 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008119 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008120 EVT VT = Op.getValueType();
8121 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008122
8123 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008124 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008125 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008126 SrcVT = VT;
8127 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008128 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008129 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008130 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008131 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008132 }
8133
8134 // At this point the operands and the result should have the same
8135 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008136
Evan Cheng68c47cb2007-01-05 07:55:56 +00008137 // First get the sign bit of second operand.
8138 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008139 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008140 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8141 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008142 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008143 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8144 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8145 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8146 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008147 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008148 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008149 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008150 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008151 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008152 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008153 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008154
8155 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008156 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008157 // Op0 is MVT::f32, Op1 is MVT::f64.
8158 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8159 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8160 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008161 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008162 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008163 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008164 }
8165
Evan Cheng73d6cf12007-01-05 21:37:56 +00008166 // Clear first operand sign bit.
8167 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008168 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008169 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8170 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008171 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008172 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8173 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8174 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8175 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008176 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008177 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008178 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008179 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008180 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008181 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008182 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008183
8184 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008185 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008186}
8187
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008188SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8189 SDValue N0 = Op.getOperand(0);
8190 DebugLoc dl = Op.getDebugLoc();
8191 EVT VT = Op.getValueType();
8192
8193 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8194 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8195 DAG.getConstant(1, VT));
8196 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8197}
8198
Dan Gohman076aee32009-03-04 19:44:21 +00008199/// Emit nodes that will be selected as "test Op0,Op0", or something
8200/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008201SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008202 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008203 DebugLoc dl = Op.getDebugLoc();
8204
Dan Gohman31125812009-03-07 01:58:32 +00008205 // CF and OF aren't always set the way we want. Determine which
8206 // of these we need.
8207 bool NeedCF = false;
8208 bool NeedOF = false;
8209 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008210 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008211 case X86::COND_A: case X86::COND_AE:
8212 case X86::COND_B: case X86::COND_BE:
8213 NeedCF = true;
8214 break;
8215 case X86::COND_G: case X86::COND_GE:
8216 case X86::COND_L: case X86::COND_LE:
8217 case X86::COND_O: case X86::COND_NO:
8218 NeedOF = true;
8219 break;
Dan Gohman31125812009-03-07 01:58:32 +00008220 }
8221
Dan Gohman076aee32009-03-04 19:44:21 +00008222 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008223 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8224 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008225 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8226 // Emit a CMP with 0, which is the TEST pattern.
8227 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8228 DAG.getConstant(0, Op.getValueType()));
8229
8230 unsigned Opcode = 0;
8231 unsigned NumOperands = 0;
8232 switch (Op.getNode()->getOpcode()) {
8233 case ISD::ADD:
8234 // Due to an isel shortcoming, be conservative if this add is likely to be
8235 // selected as part of a load-modify-store instruction. When the root node
8236 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8237 // uses of other nodes in the match, such as the ADD in this case. This
8238 // leads to the ADD being left around and reselected, with the result being
8239 // two adds in the output. Alas, even if none our users are stores, that
8240 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8241 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8242 // climbing the DAG back to the root, and it doesn't seem to be worth the
8243 // effort.
8244 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00008245 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008246 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
8247 goto default_case;
8248
8249 if (ConstantSDNode *C =
8250 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8251 // An add of one will be selected as an INC.
8252 if (C->getAPIntValue() == 1) {
8253 Opcode = X86ISD::INC;
8254 NumOperands = 1;
8255 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008256 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008257
8258 // An add of negative one (subtract of one) will be selected as a DEC.
8259 if (C->getAPIntValue().isAllOnesValue()) {
8260 Opcode = X86ISD::DEC;
8261 NumOperands = 1;
8262 break;
8263 }
Dan Gohman076aee32009-03-04 19:44:21 +00008264 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008265
8266 // Otherwise use a regular EFLAGS-setting add.
8267 Opcode = X86ISD::ADD;
8268 NumOperands = 2;
8269 break;
8270 case ISD::AND: {
8271 // If the primary and result isn't used, don't bother using X86ISD::AND,
8272 // because a TEST instruction will be better.
8273 bool NonFlagUse = false;
8274 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8275 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8276 SDNode *User = *UI;
8277 unsigned UOpNo = UI.getOperandNo();
8278 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8279 // Look pass truncate.
8280 UOpNo = User->use_begin().getOperandNo();
8281 User = *User->use_begin();
8282 }
8283
8284 if (User->getOpcode() != ISD::BRCOND &&
8285 User->getOpcode() != ISD::SETCC &&
8286 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8287 NonFlagUse = true;
8288 break;
8289 }
Dan Gohman076aee32009-03-04 19:44:21 +00008290 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008291
8292 if (!NonFlagUse)
8293 break;
8294 }
8295 // FALL THROUGH
8296 case ISD::SUB:
8297 case ISD::OR:
8298 case ISD::XOR:
8299 // Due to the ISEL shortcoming noted above, be conservative if this op is
8300 // likely to be selected as part of a load-modify-store instruction.
8301 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8302 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8303 if (UI->getOpcode() == ISD::STORE)
8304 goto default_case;
8305
8306 // Otherwise use a regular EFLAGS-setting instruction.
8307 switch (Op.getNode()->getOpcode()) {
8308 default: llvm_unreachable("unexpected operator!");
8309 case ISD::SUB: Opcode = X86ISD::SUB; break;
8310 case ISD::OR: Opcode = X86ISD::OR; break;
8311 case ISD::XOR: Opcode = X86ISD::XOR; break;
8312 case ISD::AND: Opcode = X86ISD::AND; break;
8313 }
8314
8315 NumOperands = 2;
8316 break;
8317 case X86ISD::ADD:
8318 case X86ISD::SUB:
8319 case X86ISD::INC:
8320 case X86ISD::DEC:
8321 case X86ISD::OR:
8322 case X86ISD::XOR:
8323 case X86ISD::AND:
8324 return SDValue(Op.getNode(), 1);
8325 default:
8326 default_case:
8327 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008328 }
8329
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008330 if (Opcode == 0)
8331 // Emit a CMP with 0, which is the TEST pattern.
8332 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8333 DAG.getConstant(0, Op.getValueType()));
8334
8335 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8336 SmallVector<SDValue, 4> Ops;
8337 for (unsigned i = 0; i != NumOperands; ++i)
8338 Ops.push_back(Op.getOperand(i));
8339
8340 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8341 DAG.ReplaceAllUsesWith(Op, New);
8342 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008343}
8344
8345/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8346/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008347SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008348 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008349 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8350 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008351 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008352
8353 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008354 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008355}
8356
Evan Chengd40d03e2010-01-06 19:38:29 +00008357/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8358/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008359SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8360 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008361 SDValue Op0 = And.getOperand(0);
8362 SDValue Op1 = And.getOperand(1);
8363 if (Op0.getOpcode() == ISD::TRUNCATE)
8364 Op0 = Op0.getOperand(0);
8365 if (Op1.getOpcode() == ISD::TRUNCATE)
8366 Op1 = Op1.getOperand(0);
8367
Evan Chengd40d03e2010-01-06 19:38:29 +00008368 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008369 if (Op1.getOpcode() == ISD::SHL)
8370 std::swap(Op0, Op1);
8371 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008372 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8373 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008374 // If we looked past a truncate, check that it's only truncating away
8375 // known zeros.
8376 unsigned BitWidth = Op0.getValueSizeInBits();
8377 unsigned AndBitWidth = And.getValueSizeInBits();
8378 if (BitWidth > AndBitWidth) {
8379 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8380 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8381 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8382 return SDValue();
8383 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008384 LHS = Op1;
8385 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008386 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008387 } else if (Op1.getOpcode() == ISD::Constant) {
8388 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
8389 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00008390 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
8391 LHS = AndLHS.getOperand(0);
8392 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008393 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008394 }
Evan Cheng0488db92007-09-25 01:57:46 +00008395
Evan Chengd40d03e2010-01-06 19:38:29 +00008396 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008397 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008398 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008399 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008400 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008401 // Also promote i16 to i32 for performance / code size reason.
8402 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008403 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008404 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008405
Evan Chengd40d03e2010-01-06 19:38:29 +00008406 // If the operand types disagree, extend the shift amount to match. Since
8407 // BT ignores high bits (like shifts) we can use anyextend.
8408 if (LHS.getValueType() != RHS.getValueType())
8409 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008410
Evan Chengd40d03e2010-01-06 19:38:29 +00008411 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8412 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8413 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8414 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008415 }
8416
Evan Cheng54de3ea2010-01-05 06:52:31 +00008417 return SDValue();
8418}
8419
Dan Gohmand858e902010-04-17 15:26:15 +00008420SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008421
8422 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8423
Evan Cheng54de3ea2010-01-05 06:52:31 +00008424 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8425 SDValue Op0 = Op.getOperand(0);
8426 SDValue Op1 = Op.getOperand(1);
8427 DebugLoc dl = Op.getDebugLoc();
8428 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8429
8430 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008431 // Lower (X & (1 << N)) == 0 to BT(X, N).
8432 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8433 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008434 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008435 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008436 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008437 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8438 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8439 if (NewSetCC.getNode())
8440 return NewSetCC;
8441 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008442
Chris Lattner481eebc2010-12-19 21:23:48 +00008443 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8444 // these.
8445 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008446 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008447 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8448 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008449
Chris Lattner481eebc2010-12-19 21:23:48 +00008450 // If the input is a setcc, then reuse the input setcc or use a new one with
8451 // the inverted condition.
8452 if (Op0.getOpcode() == X86ISD::SETCC) {
8453 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8454 bool Invert = (CC == ISD::SETNE) ^
8455 cast<ConstantSDNode>(Op1)->isNullValue();
8456 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008457
Evan Cheng2c755ba2010-02-27 07:36:59 +00008458 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008459 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8460 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8461 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008462 }
8463
Evan Chenge5b51ac2010-04-17 06:13:15 +00008464 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008465 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008466 if (X86CC == X86::COND_INVALID)
8467 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008468
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008469 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008470 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008471 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008472}
8473
Craig Topper89af15e2011-09-18 08:03:58 +00008474// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008475// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008476static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008477 EVT VT = Op.getValueType();
8478
Duncan Sands28b77e92011-09-06 19:07:46 +00008479 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008480 "Unsupported value type for operation");
8481
8482 int NumElems = VT.getVectorNumElements();
8483 DebugLoc dl = Op.getDebugLoc();
8484 SDValue CC = Op.getOperand(2);
8485 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8486 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8487
8488 // Extract the LHS vectors
8489 SDValue LHS = Op.getOperand(0);
8490 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8491 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8492
8493 // Extract the RHS vectors
8494 SDValue RHS = Op.getOperand(1);
8495 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8496 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8497
8498 // Issue the operation on the smaller types and concatenate the result back
8499 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8500 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8501 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8502 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8503 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8504}
8505
8506
Dan Gohmand858e902010-04-17 15:26:15 +00008507SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008508 SDValue Cond;
8509 SDValue Op0 = Op.getOperand(0);
8510 SDValue Op1 = Op.getOperand(1);
8511 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008512 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008513 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8514 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008515 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008516
8517 if (isFP) {
8518 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008519 EVT EltVT = Op0.getValueType().getVectorElementType();
8520 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8521
8522 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008523 bool Swap = false;
8524
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008525 // SSE Condition code mapping:
8526 // 0 - EQ
8527 // 1 - LT
8528 // 2 - LE
8529 // 3 - UNORD
8530 // 4 - NEQ
8531 // 5 - NLT
8532 // 6 - NLE
8533 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008534 switch (SetCCOpcode) {
8535 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008536 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008537 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008538 case ISD::SETOGT:
8539 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008540 case ISD::SETLT:
8541 case ISD::SETOLT: SSECC = 1; break;
8542 case ISD::SETOGE:
8543 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008544 case ISD::SETLE:
8545 case ISD::SETOLE: SSECC = 2; break;
8546 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008547 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008548 case ISD::SETNE: SSECC = 4; break;
8549 case ISD::SETULE: Swap = true;
8550 case ISD::SETUGE: SSECC = 5; break;
8551 case ISD::SETULT: Swap = true;
8552 case ISD::SETUGT: SSECC = 6; break;
8553 case ISD::SETO: SSECC = 7; break;
8554 }
8555 if (Swap)
8556 std::swap(Op0, Op1);
8557
Nate Begemanfb8ead02008-07-25 19:05:58 +00008558 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008559 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008560 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008561 SDValue UNORD, EQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008562 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8563 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008564 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Craig Topper0a150352011-11-09 08:06:13 +00008565 } else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008566 SDValue ORD, NEQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008567 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8568 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008569 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008570 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008571 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008572 }
8573 // Handle all other FP comparisons here.
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008574 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008575 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008576
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008577 // Break 256-bit integer vector compare into smaller ones.
Craig Topper0a150352011-11-09 08:06:13 +00008578 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008579 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008580
Nate Begeman30a0de92008-07-17 16:51:19 +00008581 // We are handling one of the integer comparisons here. Since SSE only has
8582 // GT and EQ comparisons for integer, swapping operands and multiple
8583 // operations may be required for some comparisons.
8584 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8585 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008586
Craig Topper0a150352011-11-09 08:06:13 +00008587 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008588 default: break;
Craig Topper0a150352011-11-09 08:06:13 +00008589 case MVT::i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
8590 case MVT::i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
8591 case MVT::i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8592 case MVT::i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008593 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008594
Nate Begeman30a0de92008-07-17 16:51:19 +00008595 switch (SetCCOpcode) {
8596 default: break;
8597 case ISD::SETNE: Invert = true;
8598 case ISD::SETEQ: Opc = EQOpc; break;
8599 case ISD::SETLT: Swap = true;
8600 case ISD::SETGT: Opc = GTOpc; break;
8601 case ISD::SETGE: Swap = true;
8602 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8603 case ISD::SETULT: Swap = true;
8604 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8605 case ISD::SETUGE: Swap = true;
8606 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8607 }
8608 if (Swap)
8609 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008610
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008611 // Check that the operation in question is available (most are plain SSE2,
8612 // but PCMPGTQ and PCMPEQQ have different requirements).
8613 if (Opc == X86ISD::PCMPGTQ && !Subtarget->hasSSE42() && !Subtarget->hasAVX())
8614 return SDValue();
8615 if (Opc == X86ISD::PCMPEQQ && !Subtarget->hasSSE41() && !Subtarget->hasAVX())
8616 return SDValue();
8617
Nate Begeman30a0de92008-07-17 16:51:19 +00008618 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8619 // bits of the inputs before performing those operations.
8620 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008621 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008622 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8623 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008624 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008625 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8626 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008627 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8628 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008629 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008630
Dale Johannesenace16102009-02-03 19:33:06 +00008631 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008632
8633 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008634 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008635 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008636
Nate Begeman30a0de92008-07-17 16:51:19 +00008637 return Result;
8638}
Evan Cheng0488db92007-09-25 01:57:46 +00008639
Evan Cheng370e5342008-12-03 08:38:43 +00008640// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008641static bool isX86LogicalCmp(SDValue Op) {
8642 unsigned Opc = Op.getNode()->getOpcode();
8643 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8644 return true;
8645 if (Op.getResNo() == 1 &&
8646 (Opc == X86ISD::ADD ||
8647 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008648 Opc == X86ISD::ADC ||
8649 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008650 Opc == X86ISD::SMUL ||
8651 Opc == X86ISD::UMUL ||
8652 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008653 Opc == X86ISD::DEC ||
8654 Opc == X86ISD::OR ||
8655 Opc == X86ISD::XOR ||
8656 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008657 return true;
8658
Chris Lattner9637d5b2010-12-05 07:49:54 +00008659 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8660 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008661
Dan Gohman076aee32009-03-04 19:44:21 +00008662 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008663}
8664
Chris Lattnera2b56002010-12-05 01:23:24 +00008665static bool isZero(SDValue V) {
8666 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8667 return C && C->isNullValue();
8668}
8669
Chris Lattner96908b12010-12-05 02:00:51 +00008670static bool isAllOnes(SDValue V) {
8671 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8672 return C && C->isAllOnesValue();
8673}
8674
Dan Gohmand858e902010-04-17 15:26:15 +00008675SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008676 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008677 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008678 SDValue Op1 = Op.getOperand(1);
8679 SDValue Op2 = Op.getOperand(2);
8680 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008681 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008682
Dan Gohman1a492952009-10-20 16:22:37 +00008683 if (Cond.getOpcode() == ISD::SETCC) {
8684 SDValue NewCond = LowerSETCC(Cond, DAG);
8685 if (NewCond.getNode())
8686 Cond = NewCond;
8687 }
Evan Cheng734503b2006-09-11 02:19:56 +00008688
Chris Lattnera2b56002010-12-05 01:23:24 +00008689 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008690 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008691 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008692 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008693 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008694 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8695 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008696 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008697
Chris Lattnera2b56002010-12-05 01:23:24 +00008698 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008699
8700 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008701 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8702 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008703
8704 SDValue CmpOp0 = Cmp.getOperand(0);
8705 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8706 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008707
Chris Lattner96908b12010-12-05 02:00:51 +00008708 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008709 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8710 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008711
Chris Lattner96908b12010-12-05 02:00:51 +00008712 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8713 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008714
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008715 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008716 if (N2C == 0 || !N2C->isNullValue())
8717 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8718 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008719 }
8720 }
8721
Chris Lattnera2b56002010-12-05 01:23:24 +00008722 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008723 if (Cond.getOpcode() == ISD::AND &&
8724 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8725 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008726 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008727 Cond = Cond.getOperand(0);
8728 }
8729
Evan Cheng3f41d662007-10-08 22:16:29 +00008730 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8731 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008732 unsigned CondOpcode = Cond.getOpcode();
8733 if (CondOpcode == X86ISD::SETCC ||
8734 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008735 CC = Cond.getOperand(0);
8736
Dan Gohman475871a2008-07-27 21:46:04 +00008737 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008738 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008739 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008740
Evan Cheng3f41d662007-10-08 22:16:29 +00008741 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008742 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008743 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008744 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008745
Chris Lattnerd1980a52009-03-12 06:52:53 +00008746 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8747 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008748 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008749 addTest = false;
8750 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008751 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8752 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8753 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8754 Cond.getOperand(0).getValueType() != MVT::i8)) {
8755 SDValue LHS = Cond.getOperand(0);
8756 SDValue RHS = Cond.getOperand(1);
8757 unsigned X86Opcode;
8758 unsigned X86Cond;
8759 SDVTList VTs;
8760 switch (CondOpcode) {
8761 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8762 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8763 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8764 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8765 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8766 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8767 default: llvm_unreachable("unexpected overflowing operator");
8768 }
8769 if (CondOpcode == ISD::UMULO)
8770 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8771 MVT::i32);
8772 else
8773 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8774
8775 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
8776
8777 if (CondOpcode == ISD::UMULO)
8778 Cond = X86Op.getValue(2);
8779 else
8780 Cond = X86Op.getValue(1);
8781
8782 CC = DAG.getConstant(X86Cond, MVT::i8);
8783 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00008784 }
8785
8786 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008787 // Look pass the truncate.
8788 if (Cond.getOpcode() == ISD::TRUNCATE)
8789 Cond = Cond.getOperand(0);
8790
8791 // We know the result of AND is compared against zero. Try to match
8792 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008793 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008794 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008795 if (NewSetCC.getNode()) {
8796 CC = NewSetCC.getOperand(0);
8797 Cond = NewSetCC.getOperand(1);
8798 addTest = false;
8799 }
8800 }
8801 }
8802
8803 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008804 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008805 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008806 }
8807
Benjamin Kramere915ff32010-12-22 23:09:28 +00008808 // a < b ? -1 : 0 -> RES = ~setcc_carry
8809 // a < b ? 0 : -1 -> RES = setcc_carry
8810 // a >= b ? -1 : 0 -> RES = setcc_carry
8811 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8812 if (Cond.getOpcode() == X86ISD::CMP) {
8813 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8814
8815 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8816 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8817 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8818 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8819 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8820 return DAG.getNOT(DL, Res, Res.getValueType());
8821 return Res;
8822 }
8823 }
8824
Evan Cheng0488db92007-09-25 01:57:46 +00008825 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8826 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008827 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008828 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008829 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008830}
8831
Evan Cheng370e5342008-12-03 08:38:43 +00008832// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8833// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8834// from the AND / OR.
8835static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8836 Opc = Op.getOpcode();
8837 if (Opc != ISD::OR && Opc != ISD::AND)
8838 return false;
8839 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8840 Op.getOperand(0).hasOneUse() &&
8841 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8842 Op.getOperand(1).hasOneUse());
8843}
8844
Evan Cheng961d6d42009-02-02 08:19:07 +00008845// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8846// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008847static bool isXor1OfSetCC(SDValue Op) {
8848 if (Op.getOpcode() != ISD::XOR)
8849 return false;
8850 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8851 if (N1C && N1C->getAPIntValue() == 1) {
8852 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8853 Op.getOperand(0).hasOneUse();
8854 }
8855 return false;
8856}
8857
Dan Gohmand858e902010-04-17 15:26:15 +00008858SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008859 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008860 SDValue Chain = Op.getOperand(0);
8861 SDValue Cond = Op.getOperand(1);
8862 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008863 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008864 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00008865 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00008866
Dan Gohman1a492952009-10-20 16:22:37 +00008867 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00008868 // Check for setcc([su]{add,sub,mul}o == 0).
8869 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
8870 isa<ConstantSDNode>(Cond.getOperand(1)) &&
8871 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
8872 Cond.getOperand(0).getResNo() == 1 &&
8873 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
8874 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
8875 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
8876 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
8877 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
8878 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
8879 Inverted = true;
8880 Cond = Cond.getOperand(0);
8881 } else {
8882 SDValue NewCond = LowerSETCC(Cond, DAG);
8883 if (NewCond.getNode())
8884 Cond = NewCond;
8885 }
Dan Gohman1a492952009-10-20 16:22:37 +00008886 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008887#if 0
8888 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008889 else if (Cond.getOpcode() == X86ISD::ADD ||
8890 Cond.getOpcode() == X86ISD::SUB ||
8891 Cond.getOpcode() == X86ISD::SMUL ||
8892 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008893 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008894#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008895
Evan Chengad9c0a32009-12-15 00:53:42 +00008896 // Look pass (and (setcc_carry (cmp ...)), 1).
8897 if (Cond.getOpcode() == ISD::AND &&
8898 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8899 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008900 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008901 Cond = Cond.getOperand(0);
8902 }
8903
Evan Cheng3f41d662007-10-08 22:16:29 +00008904 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8905 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008906 unsigned CondOpcode = Cond.getOpcode();
8907 if (CondOpcode == X86ISD::SETCC ||
8908 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008909 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008910
Dan Gohman475871a2008-07-27 21:46:04 +00008911 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008912 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008913 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008914 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008915 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008916 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008917 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008918 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008919 default: break;
8920 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008921 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008922 // These can only come from an arithmetic instruction with overflow,
8923 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008924 Cond = Cond.getNode()->getOperand(1);
8925 addTest = false;
8926 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008927 }
Evan Cheng0488db92007-09-25 01:57:46 +00008928 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008929 }
8930 CondOpcode = Cond.getOpcode();
8931 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8932 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8933 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8934 Cond.getOperand(0).getValueType() != MVT::i8)) {
8935 SDValue LHS = Cond.getOperand(0);
8936 SDValue RHS = Cond.getOperand(1);
8937 unsigned X86Opcode;
8938 unsigned X86Cond;
8939 SDVTList VTs;
8940 switch (CondOpcode) {
8941 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8942 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8943 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8944 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8945 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8946 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8947 default: llvm_unreachable("unexpected overflowing operator");
8948 }
8949 if (Inverted)
8950 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
8951 if (CondOpcode == ISD::UMULO)
8952 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8953 MVT::i32);
8954 else
8955 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8956
8957 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
8958
8959 if (CondOpcode == ISD::UMULO)
8960 Cond = X86Op.getValue(2);
8961 else
8962 Cond = X86Op.getValue(1);
8963
8964 CC = DAG.getConstant(X86Cond, MVT::i8);
8965 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00008966 } else {
8967 unsigned CondOpc;
8968 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8969 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008970 if (CondOpc == ISD::OR) {
8971 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8972 // two branches instead of an explicit OR instruction with a
8973 // separate test.
8974 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008975 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008976 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008977 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008978 Chain, Dest, CC, Cmp);
8979 CC = Cond.getOperand(1).getOperand(0);
8980 Cond = Cmp;
8981 addTest = false;
8982 }
8983 } else { // ISD::AND
8984 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8985 // two branches instead of an explicit AND instruction with a
8986 // separate test. However, we only do this if this block doesn't
8987 // have a fall-through edge, because this requires an explicit
8988 // jmp when the condition is false.
8989 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008990 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008991 Op.getNode()->hasOneUse()) {
8992 X86::CondCode CCode =
8993 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8994 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008995 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008996 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008997 // Look for an unconditional branch following this conditional branch.
8998 // We need this because we need to reverse the successors in order
8999 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009000 if (User->getOpcode() == ISD::BR) {
9001 SDValue FalseBB = User->getOperand(1);
9002 SDNode *NewBR =
9003 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009004 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009005 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009006 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009007
Dale Johannesene4d209d2009-02-03 20:21:25 +00009008 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009009 Chain, Dest, CC, Cmp);
9010 X86::CondCode CCode =
9011 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9012 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009013 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009014 Cond = Cmp;
9015 addTest = false;
9016 }
9017 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009018 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009019 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9020 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9021 // It should be transformed during dag combiner except when the condition
9022 // is set by a arithmetics with overflow node.
9023 X86::CondCode CCode =
9024 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9025 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009026 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009027 Cond = Cond.getOperand(0).getOperand(1);
9028 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009029 } else if (Cond.getOpcode() == ISD::SETCC &&
9030 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9031 // For FCMP_OEQ, we can emit
9032 // two branches instead of an explicit AND instruction with a
9033 // separate test. However, we only do this if this block doesn't
9034 // have a fall-through edge, because this requires an explicit
9035 // jmp when the condition is false.
9036 if (Op.getNode()->hasOneUse()) {
9037 SDNode *User = *Op.getNode()->use_begin();
9038 // Look for an unconditional branch following this conditional branch.
9039 // We need this because we need to reverse the successors in order
9040 // to implement FCMP_OEQ.
9041 if (User->getOpcode() == ISD::BR) {
9042 SDValue FalseBB = User->getOperand(1);
9043 SDNode *NewBR =
9044 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9045 assert(NewBR == User);
9046 (void)NewBR;
9047 Dest = FalseBB;
9048
9049 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9050 Cond.getOperand(0), Cond.getOperand(1));
9051 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9052 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9053 Chain, Dest, CC, Cmp);
9054 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9055 Cond = Cmp;
9056 addTest = false;
9057 }
9058 }
9059 } else if (Cond.getOpcode() == ISD::SETCC &&
9060 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9061 // For FCMP_UNE, we can emit
9062 // two branches instead of an explicit AND instruction with a
9063 // separate test. However, we only do this if this block doesn't
9064 // have a fall-through edge, because this requires an explicit
9065 // jmp when the condition is false.
9066 if (Op.getNode()->hasOneUse()) {
9067 SDNode *User = *Op.getNode()->use_begin();
9068 // Look for an unconditional branch following this conditional branch.
9069 // We need this because we need to reverse the successors in order
9070 // to implement FCMP_UNE.
9071 if (User->getOpcode() == ISD::BR) {
9072 SDValue FalseBB = User->getOperand(1);
9073 SDNode *NewBR =
9074 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9075 assert(NewBR == User);
9076 (void)NewBR;
9077
9078 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9079 Cond.getOperand(0), Cond.getOperand(1));
9080 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9081 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9082 Chain, Dest, CC, Cmp);
9083 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9084 Cond = Cmp;
9085 addTest = false;
9086 Dest = FalseBB;
9087 }
9088 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009089 }
Evan Cheng0488db92007-09-25 01:57:46 +00009090 }
9091
9092 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009093 // Look pass the truncate.
9094 if (Cond.getOpcode() == ISD::TRUNCATE)
9095 Cond = Cond.getOperand(0);
9096
9097 // We know the result of AND is compared against zero. Try to match
9098 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009099 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009100 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9101 if (NewSetCC.getNode()) {
9102 CC = NewSetCC.getOperand(0);
9103 Cond = NewSetCC.getOperand(1);
9104 addTest = false;
9105 }
9106 }
9107 }
9108
9109 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009110 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009111 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009112 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00009113 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009114 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009115}
9116
Anton Korobeynikove060b532007-04-17 19:34:00 +00009117
9118// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9119// Calls to _alloca is needed to probe the stack when allocating more than 4k
9120// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9121// that the guard pages used by the OS virtual memory manager are allocated in
9122// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009123SDValue
9124X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009125 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009126 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
9127 EnableSegmentedStacks) &&
9128 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009129 "are being used");
9130 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009131 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009132
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009133 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009134 SDValue Chain = Op.getOperand(0);
9135 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009136 // FIXME: Ensure alignment here
9137
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009138 bool Is64Bit = Subtarget->is64Bit();
9139 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009140
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009141 if (EnableSegmentedStacks) {
9142 MachineFunction &MF = DAG.getMachineFunction();
9143 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009144
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009145 if (Is64Bit) {
9146 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009147 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009148 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009149
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009150 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
9151 I != E; I++)
9152 if (I->hasNestAttr())
9153 report_fatal_error("Cannot use segmented stacks with functions that "
9154 "have nested arguments.");
9155 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009156
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009157 const TargetRegisterClass *AddrRegClass =
9158 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9159 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9160 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9161 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9162 DAG.getRegister(Vreg, SPTy));
9163 SDValue Ops1[2] = { Value, Chain };
9164 return DAG.getMergeValues(Ops1, 2, dl);
9165 } else {
9166 SDValue Flag;
9167 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009168
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009169 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9170 Flag = Chain.getValue(1);
9171 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009172
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009173 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9174 Flag = Chain.getValue(1);
9175
9176 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9177
9178 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9179 return DAG.getMergeValues(Ops1, 2, dl);
9180 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009181}
9182
Dan Gohmand858e902010-04-17 15:26:15 +00009183SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009184 MachineFunction &MF = DAG.getMachineFunction();
9185 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9186
Dan Gohman69de1932008-02-06 22:27:42 +00009187 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009188 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009189
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009190 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009191 // vastart just stores the address of the VarArgsFrameIndex slot into the
9192 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009193 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9194 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009195 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9196 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009197 }
9198
9199 // __va_list_tag:
9200 // gp_offset (0 - 6 * 8)
9201 // fp_offset (48 - 48 + 8 * 16)
9202 // overflow_arg_area (point to parameters coming in memory).
9203 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009204 SmallVector<SDValue, 8> MemOps;
9205 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009206 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009207 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009208 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9209 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009210 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009211 MemOps.push_back(Store);
9212
9213 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009214 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009215 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009216 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009217 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9218 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009219 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009220 MemOps.push_back(Store);
9221
9222 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009223 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009224 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009225 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9226 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009227 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9228 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009229 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009230 MemOps.push_back(Store);
9231
9232 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009233 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009234 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009235 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9236 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009237 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9238 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009239 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009240 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009241 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009242}
9243
Dan Gohmand858e902010-04-17 15:26:15 +00009244SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009245 assert(Subtarget->is64Bit() &&
9246 "LowerVAARG only handles 64-bit va_arg!");
9247 assert((Subtarget->isTargetLinux() ||
9248 Subtarget->isTargetDarwin()) &&
9249 "Unhandled target in LowerVAARG");
9250 assert(Op.getNode()->getNumOperands() == 4);
9251 SDValue Chain = Op.getOperand(0);
9252 SDValue SrcPtr = Op.getOperand(1);
9253 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9254 unsigned Align = Op.getConstantOperandVal(3);
9255 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009256
Dan Gohman320afb82010-10-12 18:00:49 +00009257 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009258 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009259 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9260 uint8_t ArgMode;
9261
9262 // Decide which area this value should be read from.
9263 // TODO: Implement the AMD64 ABI in its entirety. This simple
9264 // selection mechanism works only for the basic types.
9265 if (ArgVT == MVT::f80) {
9266 llvm_unreachable("va_arg for f80 not yet implemented");
9267 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9268 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9269 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9270 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9271 } else {
9272 llvm_unreachable("Unhandled argument type in LowerVAARG");
9273 }
9274
9275 if (ArgMode == 2) {
9276 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00009277 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009278 !(DAG.getMachineFunction()
9279 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009280 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009281 }
9282
9283 // Insert VAARG_64 node into the DAG
9284 // VAARG_64 returns two values: Variable Argument Address, Chain
9285 SmallVector<SDValue, 11> InstOps;
9286 InstOps.push_back(Chain);
9287 InstOps.push_back(SrcPtr);
9288 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9289 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9290 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9291 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9292 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9293 VTs, &InstOps[0], InstOps.size(),
9294 MVT::i64,
9295 MachinePointerInfo(SV),
9296 /*Align=*/0,
9297 /*Volatile=*/false,
9298 /*ReadMem=*/true,
9299 /*WriteMem=*/true);
9300 Chain = VAARG.getValue(1);
9301
9302 // Load the next argument and return it
9303 return DAG.getLoad(ArgVT, dl,
9304 Chain,
9305 VAARG,
9306 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009307 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009308}
9309
Dan Gohmand858e902010-04-17 15:26:15 +00009310SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009311 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009312 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009313 SDValue Chain = Op.getOperand(0);
9314 SDValue DstPtr = Op.getOperand(1);
9315 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009316 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9317 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009318 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009319
Chris Lattnere72f2022010-09-21 05:40:29 +00009320 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009321 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009322 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009323 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009324}
9325
Dan Gohman475871a2008-07-27 21:46:04 +00009326SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009327X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009328 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009329 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009330 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009331 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009332 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009333 case Intrinsic::x86_sse_comieq_ss:
9334 case Intrinsic::x86_sse_comilt_ss:
9335 case Intrinsic::x86_sse_comile_ss:
9336 case Intrinsic::x86_sse_comigt_ss:
9337 case Intrinsic::x86_sse_comige_ss:
9338 case Intrinsic::x86_sse_comineq_ss:
9339 case Intrinsic::x86_sse_ucomieq_ss:
9340 case Intrinsic::x86_sse_ucomilt_ss:
9341 case Intrinsic::x86_sse_ucomile_ss:
9342 case Intrinsic::x86_sse_ucomigt_ss:
9343 case Intrinsic::x86_sse_ucomige_ss:
9344 case Intrinsic::x86_sse_ucomineq_ss:
9345 case Intrinsic::x86_sse2_comieq_sd:
9346 case Intrinsic::x86_sse2_comilt_sd:
9347 case Intrinsic::x86_sse2_comile_sd:
9348 case Intrinsic::x86_sse2_comigt_sd:
9349 case Intrinsic::x86_sse2_comige_sd:
9350 case Intrinsic::x86_sse2_comineq_sd:
9351 case Intrinsic::x86_sse2_ucomieq_sd:
9352 case Intrinsic::x86_sse2_ucomilt_sd:
9353 case Intrinsic::x86_sse2_ucomile_sd:
9354 case Intrinsic::x86_sse2_ucomigt_sd:
9355 case Intrinsic::x86_sse2_ucomige_sd:
9356 case Intrinsic::x86_sse2_ucomineq_sd: {
9357 unsigned Opc = 0;
9358 ISD::CondCode CC = ISD::SETCC_INVALID;
9359 switch (IntNo) {
9360 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009361 case Intrinsic::x86_sse_comieq_ss:
9362 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009363 Opc = X86ISD::COMI;
9364 CC = ISD::SETEQ;
9365 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009366 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009367 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009368 Opc = X86ISD::COMI;
9369 CC = ISD::SETLT;
9370 break;
9371 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009372 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009373 Opc = X86ISD::COMI;
9374 CC = ISD::SETLE;
9375 break;
9376 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009377 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009378 Opc = X86ISD::COMI;
9379 CC = ISD::SETGT;
9380 break;
9381 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009382 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009383 Opc = X86ISD::COMI;
9384 CC = ISD::SETGE;
9385 break;
9386 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009387 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009388 Opc = X86ISD::COMI;
9389 CC = ISD::SETNE;
9390 break;
9391 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009392 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009393 Opc = X86ISD::UCOMI;
9394 CC = ISD::SETEQ;
9395 break;
9396 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009397 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009398 Opc = X86ISD::UCOMI;
9399 CC = ISD::SETLT;
9400 break;
9401 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009402 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009403 Opc = X86ISD::UCOMI;
9404 CC = ISD::SETLE;
9405 break;
9406 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009407 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009408 Opc = X86ISD::UCOMI;
9409 CC = ISD::SETGT;
9410 break;
9411 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009412 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009413 Opc = X86ISD::UCOMI;
9414 CC = ISD::SETGE;
9415 break;
9416 case Intrinsic::x86_sse_ucomineq_ss:
9417 case Intrinsic::x86_sse2_ucomineq_sd:
9418 Opc = X86ISD::UCOMI;
9419 CC = ISD::SETNE;
9420 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009421 }
Evan Cheng734503b2006-09-11 02:19:56 +00009422
Dan Gohman475871a2008-07-27 21:46:04 +00009423 SDValue LHS = Op.getOperand(1);
9424 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009425 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009426 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009427 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9428 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9429 DAG.getConstant(X86CC, MVT::i8), Cond);
9430 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009431 }
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009432 // Arithmetic intrinsics.
9433 case Intrinsic::x86_sse3_hadd_ps:
9434 case Intrinsic::x86_sse3_hadd_pd:
9435 case Intrinsic::x86_avx_hadd_ps_256:
9436 case Intrinsic::x86_avx_hadd_pd_256:
9437 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9438 Op.getOperand(1), Op.getOperand(2));
9439 case Intrinsic::x86_sse3_hsub_ps:
9440 case Intrinsic::x86_sse3_hsub_pd:
9441 case Intrinsic::x86_avx_hsub_ps_256:
9442 case Intrinsic::x86_avx_hsub_pd_256:
9443 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9444 Op.getOperand(1), Op.getOperand(2));
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009445 // ptest and testp intrinsics. The intrinsic these come from are designed to
9446 // return an integer value, not just an instruction so lower it to the ptest
9447 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009448 case Intrinsic::x86_sse41_ptestz:
9449 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009450 case Intrinsic::x86_sse41_ptestnzc:
9451 case Intrinsic::x86_avx_ptestz_256:
9452 case Intrinsic::x86_avx_ptestc_256:
9453 case Intrinsic::x86_avx_ptestnzc_256:
9454 case Intrinsic::x86_avx_vtestz_ps:
9455 case Intrinsic::x86_avx_vtestc_ps:
9456 case Intrinsic::x86_avx_vtestnzc_ps:
9457 case Intrinsic::x86_avx_vtestz_pd:
9458 case Intrinsic::x86_avx_vtestc_pd:
9459 case Intrinsic::x86_avx_vtestnzc_pd:
9460 case Intrinsic::x86_avx_vtestz_ps_256:
9461 case Intrinsic::x86_avx_vtestc_ps_256:
9462 case Intrinsic::x86_avx_vtestnzc_ps_256:
9463 case Intrinsic::x86_avx_vtestz_pd_256:
9464 case Intrinsic::x86_avx_vtestc_pd_256:
9465 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9466 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009467 unsigned X86CC = 0;
9468 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009469 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009470 case Intrinsic::x86_avx_vtestz_ps:
9471 case Intrinsic::x86_avx_vtestz_pd:
9472 case Intrinsic::x86_avx_vtestz_ps_256:
9473 case Intrinsic::x86_avx_vtestz_pd_256:
9474 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009475 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009476 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009477 // ZF = 1
9478 X86CC = X86::COND_E;
9479 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009480 case Intrinsic::x86_avx_vtestc_ps:
9481 case Intrinsic::x86_avx_vtestc_pd:
9482 case Intrinsic::x86_avx_vtestc_ps_256:
9483 case Intrinsic::x86_avx_vtestc_pd_256:
9484 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009485 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009486 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009487 // CF = 1
9488 X86CC = X86::COND_B;
9489 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009490 case Intrinsic::x86_avx_vtestnzc_ps:
9491 case Intrinsic::x86_avx_vtestnzc_pd:
9492 case Intrinsic::x86_avx_vtestnzc_ps_256:
9493 case Intrinsic::x86_avx_vtestnzc_pd_256:
9494 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009495 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009496 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009497 // ZF and CF = 0
9498 X86CC = X86::COND_A;
9499 break;
9500 }
Eric Christopherfd179292009-08-27 18:07:15 +00009501
Eric Christopher71c67532009-07-29 00:28:05 +00009502 SDValue LHS = Op.getOperand(1);
9503 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009504 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9505 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009506 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9507 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9508 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009509 }
Evan Cheng5759f972008-05-04 09:15:50 +00009510
9511 // Fix vector shift instructions where the last operand is a non-immediate
9512 // i32 value.
9513 case Intrinsic::x86_sse2_pslli_w:
9514 case Intrinsic::x86_sse2_pslli_d:
9515 case Intrinsic::x86_sse2_pslli_q:
9516 case Intrinsic::x86_sse2_psrli_w:
9517 case Intrinsic::x86_sse2_psrli_d:
9518 case Intrinsic::x86_sse2_psrli_q:
9519 case Intrinsic::x86_sse2_psrai_w:
9520 case Intrinsic::x86_sse2_psrai_d:
9521 case Intrinsic::x86_mmx_pslli_w:
9522 case Intrinsic::x86_mmx_pslli_d:
9523 case Intrinsic::x86_mmx_pslli_q:
9524 case Intrinsic::x86_mmx_psrli_w:
9525 case Intrinsic::x86_mmx_psrli_d:
9526 case Intrinsic::x86_mmx_psrli_q:
9527 case Intrinsic::x86_mmx_psrai_w:
9528 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009529 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009530 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009531 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009532
9533 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009534 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009535 switch (IntNo) {
9536 case Intrinsic::x86_sse2_pslli_w:
9537 NewIntNo = Intrinsic::x86_sse2_psll_w;
9538 break;
9539 case Intrinsic::x86_sse2_pslli_d:
9540 NewIntNo = Intrinsic::x86_sse2_psll_d;
9541 break;
9542 case Intrinsic::x86_sse2_pslli_q:
9543 NewIntNo = Intrinsic::x86_sse2_psll_q;
9544 break;
9545 case Intrinsic::x86_sse2_psrli_w:
9546 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9547 break;
9548 case Intrinsic::x86_sse2_psrli_d:
9549 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9550 break;
9551 case Intrinsic::x86_sse2_psrli_q:
9552 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9553 break;
9554 case Intrinsic::x86_sse2_psrai_w:
9555 NewIntNo = Intrinsic::x86_sse2_psra_w;
9556 break;
9557 case Intrinsic::x86_sse2_psrai_d:
9558 NewIntNo = Intrinsic::x86_sse2_psra_d;
9559 break;
9560 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009561 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009562 switch (IntNo) {
9563 case Intrinsic::x86_mmx_pslli_w:
9564 NewIntNo = Intrinsic::x86_mmx_psll_w;
9565 break;
9566 case Intrinsic::x86_mmx_pslli_d:
9567 NewIntNo = Intrinsic::x86_mmx_psll_d;
9568 break;
9569 case Intrinsic::x86_mmx_pslli_q:
9570 NewIntNo = Intrinsic::x86_mmx_psll_q;
9571 break;
9572 case Intrinsic::x86_mmx_psrli_w:
9573 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9574 break;
9575 case Intrinsic::x86_mmx_psrli_d:
9576 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9577 break;
9578 case Intrinsic::x86_mmx_psrli_q:
9579 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9580 break;
9581 case Intrinsic::x86_mmx_psrai_w:
9582 NewIntNo = Intrinsic::x86_mmx_psra_w;
9583 break;
9584 case Intrinsic::x86_mmx_psrai_d:
9585 NewIntNo = Intrinsic::x86_mmx_psra_d;
9586 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009587 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009588 }
9589 break;
9590 }
9591 }
Mon P Wangefa42202009-09-03 19:56:25 +00009592
9593 // The vector shift intrinsics with scalars uses 32b shift amounts but
9594 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9595 // to be zero.
9596 SDValue ShOps[4];
9597 ShOps[0] = ShAmt;
9598 ShOps[1] = DAG.getConstant(0, MVT::i32);
9599 if (ShAmtVT == MVT::v4i32) {
9600 ShOps[2] = DAG.getUNDEF(MVT::i32);
9601 ShOps[3] = DAG.getUNDEF(MVT::i32);
9602 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9603 } else {
9604 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009605// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009606 }
9607
Owen Andersone50ed302009-08-10 22:56:29 +00009608 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009609 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009610 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009611 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009612 Op.getOperand(1), ShAmt);
9613 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009614 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009615}
Evan Cheng72261582005-12-20 06:22:03 +00009616
Dan Gohmand858e902010-04-17 15:26:15 +00009617SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9618 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009619 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9620 MFI->setReturnAddressIsTaken(true);
9621
Bill Wendling64e87322009-01-16 19:25:27 +00009622 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009623 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009624
9625 if (Depth > 0) {
9626 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9627 SDValue Offset =
9628 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009629 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009630 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009631 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009632 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009633 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009634 }
9635
9636 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009637 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009638 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009639 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009640}
9641
Dan Gohmand858e902010-04-17 15:26:15 +00009642SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009643 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9644 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009645
Owen Andersone50ed302009-08-10 22:56:29 +00009646 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009647 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009648 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9649 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009650 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009651 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009652 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9653 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009654 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009655 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009656}
9657
Dan Gohman475871a2008-07-27 21:46:04 +00009658SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009659 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009660 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009661}
9662
Dan Gohmand858e902010-04-17 15:26:15 +00009663SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009664 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009665 SDValue Chain = Op.getOperand(0);
9666 SDValue Offset = Op.getOperand(1);
9667 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009668 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009669
Dan Gohmand8816272010-08-11 18:14:00 +00009670 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9671 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9672 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009673 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009674
Dan Gohmand8816272010-08-11 18:14:00 +00009675 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9676 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009677 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009678 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9679 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009680 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009681 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009682
Dale Johannesene4d209d2009-02-03 20:21:25 +00009683 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009684 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009685 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009686}
9687
Duncan Sands4a544a72011-09-06 13:37:06 +00009688SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9689 SelectionDAG &DAG) const {
9690 return Op.getOperand(0);
9691}
9692
9693SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9694 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009695 SDValue Root = Op.getOperand(0);
9696 SDValue Trmp = Op.getOperand(1); // trampoline
9697 SDValue FPtr = Op.getOperand(2); // nested function
9698 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009699 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009700
Dan Gohman69de1932008-02-06 22:27:42 +00009701 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009702
9703 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009704 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009705
9706 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009707 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9708 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009709
Evan Cheng0e6a0522011-07-18 20:57:22 +00009710 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9711 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009712
9713 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9714
9715 // Load the pointer to the nested function into R11.
9716 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009717 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009718 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009719 Addr, MachinePointerInfo(TrmpAddr),
9720 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009721
Owen Anderson825b72b2009-08-11 20:47:22 +00009722 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9723 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009724 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9725 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009726 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009727
9728 // Load the 'nest' parameter value into R10.
9729 // R10 is specified in X86CallingConv.td
9730 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009731 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9732 DAG.getConstant(10, MVT::i64));
9733 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009734 Addr, MachinePointerInfo(TrmpAddr, 10),
9735 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009736
Owen Anderson825b72b2009-08-11 20:47:22 +00009737 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9738 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009739 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9740 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009741 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009742
9743 // Jump to the nested function.
9744 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009745 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9746 DAG.getConstant(20, MVT::i64));
9747 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009748 Addr, MachinePointerInfo(TrmpAddr, 20),
9749 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009750
9751 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009752 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9753 DAG.getConstant(22, MVT::i64));
9754 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009755 MachinePointerInfo(TrmpAddr, 22),
9756 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009757
Duncan Sands4a544a72011-09-06 13:37:06 +00009758 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009759 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009760 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009761 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009762 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009763 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009764
9765 switch (CC) {
9766 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009767 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009768 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009769 case CallingConv::X86_StdCall: {
9770 // Pass 'nest' parameter in ECX.
9771 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009772 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009773
9774 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009775 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009776 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009777
Chris Lattner58d74912008-03-12 17:45:29 +00009778 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009779 unsigned InRegCount = 0;
9780 unsigned Idx = 1;
9781
9782 for (FunctionType::param_iterator I = FTy->param_begin(),
9783 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009784 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009785 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009786 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009787
9788 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009789 report_fatal_error("Nest register in use - reduce number of inreg"
9790 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009791 }
9792 }
9793 break;
9794 }
9795 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009796 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009797 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009798 // Pass 'nest' parameter in EAX.
9799 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009800 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009801 break;
9802 }
9803
Dan Gohman475871a2008-07-27 21:46:04 +00009804 SDValue OutChains[4];
9805 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009806
Owen Anderson825b72b2009-08-11 20:47:22 +00009807 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9808 DAG.getConstant(10, MVT::i32));
9809 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009810
Chris Lattnera62fe662010-02-05 19:20:30 +00009811 // This is storing the opcode for MOV32ri.
9812 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009813 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009814 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009815 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009816 Trmp, MachinePointerInfo(TrmpAddr),
9817 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009818
Owen Anderson825b72b2009-08-11 20:47:22 +00009819 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9820 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009821 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9822 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009823 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009824
Chris Lattnera62fe662010-02-05 19:20:30 +00009825 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009826 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9827 DAG.getConstant(5, MVT::i32));
9828 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009829 MachinePointerInfo(TrmpAddr, 5),
9830 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009831
Owen Anderson825b72b2009-08-11 20:47:22 +00009832 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9833 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009834 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9835 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009836 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009837
Duncan Sands4a544a72011-09-06 13:37:06 +00009838 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009839 }
9840}
9841
Dan Gohmand858e902010-04-17 15:26:15 +00009842SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9843 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009844 /*
9845 The rounding mode is in bits 11:10 of FPSR, and has the following
9846 settings:
9847 00 Round to nearest
9848 01 Round to -inf
9849 10 Round to +inf
9850 11 Round to 0
9851
9852 FLT_ROUNDS, on the other hand, expects the following:
9853 -1 Undefined
9854 0 Round to 0
9855 1 Round to nearest
9856 2 Round to +inf
9857 3 Round to -inf
9858
9859 To perform the conversion, we do:
9860 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9861 */
9862
9863 MachineFunction &MF = DAG.getMachineFunction();
9864 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009865 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009866 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009867 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009868 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009869
9870 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009871 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009872 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009873
Michael J. Spencerec38de22010-10-10 22:04:20 +00009874
Chris Lattner2156b792010-09-22 01:11:26 +00009875 MachineMemOperand *MMO =
9876 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9877 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009878
Chris Lattner2156b792010-09-22 01:11:26 +00009879 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9880 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9881 DAG.getVTList(MVT::Other),
9882 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009883
9884 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009885 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +00009886 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009887
9888 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009889 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009890 DAG.getNode(ISD::SRL, DL, MVT::i16,
9891 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009892 CWD, DAG.getConstant(0x800, MVT::i16)),
9893 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009894 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009895 DAG.getNode(ISD::SRL, DL, MVT::i16,
9896 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009897 CWD, DAG.getConstant(0x400, MVT::i16)),
9898 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009899
Dan Gohman475871a2008-07-27 21:46:04 +00009900 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009901 DAG.getNode(ISD::AND, DL, MVT::i16,
9902 DAG.getNode(ISD::ADD, DL, MVT::i16,
9903 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009904 DAG.getConstant(1, MVT::i16)),
9905 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009906
9907
Duncan Sands83ec4b62008-06-06 12:08:01 +00009908 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009909 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009910}
9911
Dan Gohmand858e902010-04-17 15:26:15 +00009912SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009913 EVT VT = Op.getValueType();
9914 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009915 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009916 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009917
9918 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009919 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009920 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009921 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009922 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009923 }
Evan Cheng18efe262007-12-14 02:13:44 +00009924
Evan Cheng152804e2007-12-14 08:30:15 +00009925 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009926 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009927 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009928
9929 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009930 SDValue Ops[] = {
9931 Op,
9932 DAG.getConstant(NumBits+NumBits-1, OpVT),
9933 DAG.getConstant(X86::COND_E, MVT::i8),
9934 Op.getValue(1)
9935 };
9936 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009937
9938 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009939 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009940
Owen Anderson825b72b2009-08-11 20:47:22 +00009941 if (VT == MVT::i8)
9942 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009943 return Op;
9944}
9945
Dan Gohmand858e902010-04-17 15:26:15 +00009946SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009947 EVT VT = Op.getValueType();
9948 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009949 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009950 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009951
9952 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009953 if (VT == MVT::i8) {
9954 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009955 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009956 }
Evan Cheng152804e2007-12-14 08:30:15 +00009957
9958 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009959 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009960 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009961
9962 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009963 SDValue Ops[] = {
9964 Op,
9965 DAG.getConstant(NumBits, OpVT),
9966 DAG.getConstant(X86::COND_E, MVT::i8),
9967 Op.getValue(1)
9968 };
9969 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009970
Owen Anderson825b72b2009-08-11 20:47:22 +00009971 if (VT == MVT::i8)
9972 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009973 return Op;
9974}
9975
Craig Topper13894fa2011-08-24 06:14:18 +00009976// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9977// ones, and then concatenate the result back.
9978static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009979 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009980
9981 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9982 "Unsupported value type for operation");
9983
9984 int NumElems = VT.getVectorNumElements();
9985 DebugLoc dl = Op.getDebugLoc();
9986 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9987 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9988
9989 // Extract the LHS vectors
9990 SDValue LHS = Op.getOperand(0);
9991 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9992 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9993
9994 // Extract the RHS vectors
9995 SDValue RHS = Op.getOperand(1);
9996 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9997 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9998
9999 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10000 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10001
10002 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10003 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10004 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10005}
10006
10007SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
10008 assert(Op.getValueType().getSizeInBits() == 256 &&
10009 Op.getValueType().isInteger() &&
10010 "Only handle AVX 256-bit vector integer operation");
10011 return Lower256IntArith(Op, DAG);
10012}
10013
10014SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
10015 assert(Op.getValueType().getSizeInBits() == 256 &&
10016 Op.getValueType().isInteger() &&
10017 "Only handle AVX 256-bit vector integer operation");
10018 return Lower256IntArith(Op, DAG);
10019}
10020
10021SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
10022 EVT VT = Op.getValueType();
10023
10024 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topperaaa643c2011-11-09 07:28:55 +000010025 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010026 return Lower256IntArith(Op, DAG);
10027
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010028 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010029
Craig Topperaaa643c2011-11-09 07:28:55 +000010030 SDValue A = Op.getOperand(0);
10031 SDValue B = Op.getOperand(1);
10032
10033 if (VT == MVT::v4i64) {
10034 assert(Subtarget->hasAVX2() && "Lowering v4i64 multiply requires AVX2");
10035
10036 // ulong2 Ahi = __builtin_ia32_psrlqi256( a, 32);
10037 // ulong2 Bhi = __builtin_ia32_psrlqi256( b, 32);
10038 // ulong2 AloBlo = __builtin_ia32_pmuludq256( a, b );
10039 // ulong2 AloBhi = __builtin_ia32_pmuludq256( a, Bhi );
10040 // ulong2 AhiBlo = __builtin_ia32_pmuludq256( Ahi, b );
10041 //
10042 // AloBhi = __builtin_ia32_psllqi256( AloBhi, 32 );
10043 // AhiBlo = __builtin_ia32_psllqi256( AhiBlo, 32 );
10044 // return AloBlo + AloBhi + AhiBlo;
10045
10046 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10047 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
10048 A, DAG.getConstant(32, MVT::i32));
10049 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10050 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
10051 B, DAG.getConstant(32, MVT::i32));
10052 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10053 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10054 A, B);
10055 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10056 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10057 A, Bhi);
10058 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10059 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10060 Ahi, B);
10061 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10062 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
10063 AloBhi, DAG.getConstant(32, MVT::i32));
10064 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10065 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
10066 AhiBlo, DAG.getConstant(32, MVT::i32));
10067 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10068 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
10069 return Res;
10070 }
10071
10072 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
10073
Mon P Wangaf9b9522008-12-18 21:42:19 +000010074 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
10075 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
10076 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
10077 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
10078 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
10079 //
10080 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
10081 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
10082 // return AloBlo + AloBhi + AhiBlo;
10083
Dale Johannesene4d209d2009-02-03 20:21:25 +000010084 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010085 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10086 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010087 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010088 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10089 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010090 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010091 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010092 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010093 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010094 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010095 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010096 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010097 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010098 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010099 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010100 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10101 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010102 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010103 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10104 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010105 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10106 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010107 return Res;
10108}
10109
Nadav Rotem43012222011-05-11 08:12:09 +000010110SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10111
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010112 EVT VT = Op.getValueType();
10113 DebugLoc dl = Op.getDebugLoc();
10114 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010115 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010116 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010117
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010118 if (!Subtarget->hasXMMInt())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010119 return SDValue();
10120
Nadav Rotem43012222011-05-11 08:12:09 +000010121 // Optimize shl/srl/sra with constant shift amount.
10122 if (isSplatVector(Amt.getNode())) {
10123 SDValue SclrAmt = Amt->getOperand(0);
10124 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10125 uint64_t ShiftAmt = C->getZExtValue();
10126
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010127 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SHL) {
10128 // Make a large shift.
10129 SDValue SHL =
10130 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10131 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10132 R, DAG.getConstant(ShiftAmt, MVT::i32));
10133 // Zero out the rightmost bits.
10134 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10135 MVT::i8));
10136 return DAG.getNode(ISD::AND, dl, VT, SHL,
10137 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10138 }
10139
Nadav Rotem43012222011-05-11 08:12:09 +000010140 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
10141 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10142 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10143 R, DAG.getConstant(ShiftAmt, MVT::i32));
10144
10145 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
10146 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10147 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10148 R, DAG.getConstant(ShiftAmt, MVT::i32));
10149
10150 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
10151 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10152 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10153 R, DAG.getConstant(ShiftAmt, MVT::i32));
10154
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010155 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRL) {
10156 // Make a large shift.
10157 SDValue SRL =
10158 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10159 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10160 R, DAG.getConstant(ShiftAmt, MVT::i32));
10161 // Zero out the leftmost bits.
10162 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10163 MVT::i8));
10164 return DAG.getNode(ISD::AND, dl, VT, SRL,
10165 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10166 }
10167
Nadav Rotem43012222011-05-11 08:12:09 +000010168 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
10169 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10170 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10171 R, DAG.getConstant(ShiftAmt, MVT::i32));
10172
10173 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
10174 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10175 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
10176 R, DAG.getConstant(ShiftAmt, MVT::i32));
10177
10178 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
10179 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10180 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10181 R, DAG.getConstant(ShiftAmt, MVT::i32));
10182
10183 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
10184 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10185 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
10186 R, DAG.getConstant(ShiftAmt, MVT::i32));
10187
10188 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
10189 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10190 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
10191 R, DAG.getConstant(ShiftAmt, MVT::i32));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010192
10193 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRA) {
10194 if (ShiftAmt == 7) {
10195 // R s>> 7 === R s< 0
10196 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10197 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10198 }
10199
10200 // R s>> a === ((R u>> a) ^ m) - m
10201 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10202 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10203 MVT::i8));
10204 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10205 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10206 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10207 return Res;
10208 }
Craig Topper46154eb2011-11-11 07:39:23 +000010209
10210 if (Subtarget->hasAVX2()) {
10211 if (VT == MVT::v4i64 && Op.getOpcode() == ISD::SHL)
10212 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10213 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
10214 R, DAG.getConstant(ShiftAmt, MVT::i32));
10215
10216 if (VT == MVT::v8i32 && Op.getOpcode() == ISD::SHL)
10217 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10218 DAG.getConstant(Intrinsic::x86_avx2_pslli_d, MVT::i32),
10219 R, DAG.getConstant(ShiftAmt, MVT::i32));
10220
10221 if (VT == MVT::v16i16 && Op.getOpcode() == ISD::SHL)
10222 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10223 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
10224 R, DAG.getConstant(ShiftAmt, MVT::i32));
10225
10226 if (VT == MVT::v4i64 && Op.getOpcode() == ISD::SRL)
10227 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10228 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
10229 R, DAG.getConstant(ShiftAmt, MVT::i32));
10230
10231 if (VT == MVT::v8i32 && Op.getOpcode() == ISD::SRL)
10232 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10233 DAG.getConstant(Intrinsic::x86_avx2_psrli_d, MVT::i32),
10234 R, DAG.getConstant(ShiftAmt, MVT::i32));
10235
10236 if (VT == MVT::v16i16 && Op.getOpcode() == ISD::SRL)
10237 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10238 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
10239 R, DAG.getConstant(ShiftAmt, MVT::i32));
10240
10241 if (VT == MVT::v8i32 && Op.getOpcode() == ISD::SRA)
10242 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10243 DAG.getConstant(Intrinsic::x86_avx2_psrai_d, MVT::i32),
10244 R, DAG.getConstant(ShiftAmt, MVT::i32));
10245
10246 if (VT == MVT::v16i16 && Op.getOpcode() == ISD::SRA)
10247 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10248 DAG.getConstant(Intrinsic::x86_avx2_psrai_w, MVT::i32),
10249 R, DAG.getConstant(ShiftAmt, MVT::i32));
10250 }
Nadav Rotem43012222011-05-11 08:12:09 +000010251 }
10252 }
10253
Craig Topper46154eb2011-11-11 07:39:23 +000010254 // AVX2 variable shifts
10255 if (Subtarget->hasAVX2()) {
10256 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL)
10257 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10258 DAG.getConstant(Intrinsic::x86_avx2_psllv_d, MVT::i32),
10259 R, Amt);
10260 if (VT == MVT::v8i32 && Op->getOpcode() == ISD::SHL)
10261 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10262 DAG.getConstant(Intrinsic::x86_avx2_psllv_d_256, MVT::i32),
10263 R, Amt);
10264 if (VT == MVT::v2i64 && Op->getOpcode() == ISD::SHL)
10265 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10266 DAG.getConstant(Intrinsic::x86_avx2_psllv_q, MVT::i32),
10267 R, Amt);
10268 if (VT == MVT::v4i64 && Op->getOpcode() == ISD::SHL)
10269 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10270 DAG.getConstant(Intrinsic::x86_avx2_psllv_q_256, MVT::i32),
10271 R, Amt);
10272
10273 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SRL)
10274 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10275 DAG.getConstant(Intrinsic::x86_avx2_psrlv_d, MVT::i32),
10276 R, Amt);
10277 if (VT == MVT::v8i32 && Op->getOpcode() == ISD::SRL)
10278 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10279 DAG.getConstant(Intrinsic::x86_avx2_psrlv_d_256, MVT::i32),
10280 R, Amt);
10281 if (VT == MVT::v2i64 && Op->getOpcode() == ISD::SRL)
10282 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10283 DAG.getConstant(Intrinsic::x86_avx2_psrlv_q, MVT::i32),
10284 R, Amt);
10285 if (VT == MVT::v4i64 && Op->getOpcode() == ISD::SRL)
10286 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10287 DAG.getConstant(Intrinsic::x86_avx2_psrlv_q_256, MVT::i32),
10288 R, Amt);
10289
10290 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SRA)
10291 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10292 DAG.getConstant(Intrinsic::x86_avx2_psrav_d, MVT::i32),
10293 R, Amt);
10294 if (VT == MVT::v8i32 && Op->getOpcode() == ISD::SRA)
10295 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10296 DAG.getConstant(Intrinsic::x86_avx2_psrav_d_256, MVT::i32),
10297 R, Amt);
10298 }
10299
Nadav Rotem43012222011-05-11 08:12:09 +000010300 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010301 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010302 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10303 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10304 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
10305
10306 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010307
Nate Begeman51409212010-07-28 00:21:48 +000010308 std::vector<Constant*> CV(4, CI);
10309 Constant *C = ConstantVector::get(CV);
10310 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10311 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010312 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010313 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010314
10315 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010316 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010317 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10318 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10319 }
Nadav Rotem43012222011-05-11 08:12:09 +000010320 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010321 // a = a << 5;
10322 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10323 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10324 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
10325
10326 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
10327 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
10328
10329 std::vector<Constant*> CVM1(16, CM1);
10330 std::vector<Constant*> CVM2(16, CM2);
10331 Constant *C = ConstantVector::get(CVM1);
10332 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10333 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010334 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010335 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010336
10337 // r = pblendv(r, psllw(r & (char16)15, 4), a);
10338 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10339 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10340 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10341 DAG.getConstant(4, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010342 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010343 // a += a
10344 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010345
Nate Begeman51409212010-07-28 00:21:48 +000010346 C = ConstantVector::get(CVM2);
10347 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10348 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010349 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010350 false, false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010351
Nate Begeman51409212010-07-28 00:21:48 +000010352 // r = pblendv(r, psllw(r & (char16)63, 2), a);
10353 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10354 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10355 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10356 DAG.getConstant(2, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010357 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010358 // a += a
10359 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010360
Nate Begeman51409212010-07-28 00:21:48 +000010361 // return pblendv(r, r+r, a);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010362 R = DAG.getNode(ISD::VSELECT, dl, VT, Op,
10363 R, DAG.getNode(ISD::ADD, dl, VT, R, R));
Nate Begeman51409212010-07-28 00:21:48 +000010364 return R;
10365 }
Craig Topper46154eb2011-11-11 07:39:23 +000010366
10367 // Decompose 256-bit shifts into smaller 128-bit shifts.
10368 if (VT.getSizeInBits() == 256) {
10369 int NumElems = VT.getVectorNumElements();
10370 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10371 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10372
10373 // Extract the two vectors
10374 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
10375 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
10376 DAG, dl);
10377
10378 // Recreate the shift amount vectors
10379 SDValue Amt1, Amt2;
10380 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10381 // Constant shift amount
10382 SmallVector<SDValue, 4> Amt1Csts;
10383 SmallVector<SDValue, 4> Amt2Csts;
10384 for (int i = 0; i < NumElems/2; ++i)
10385 Amt1Csts.push_back(Amt->getOperand(i));
10386 for (int i = NumElems/2; i < NumElems; ++i)
10387 Amt2Csts.push_back(Amt->getOperand(i));
10388
10389 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10390 &Amt1Csts[0], NumElems/2);
10391 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10392 &Amt2Csts[0], NumElems/2);
10393 } else {
10394 // Variable shift amount
10395 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
10396 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
10397 DAG, dl);
10398 }
10399
10400 // Issue new vector shifts for the smaller types
10401 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10402 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10403
10404 // Concatenate the result back
10405 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10406 }
10407
Nate Begeman51409212010-07-28 00:21:48 +000010408 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010409}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010410
Dan Gohmand858e902010-04-17 15:26:15 +000010411SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010412 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10413 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010414 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10415 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010416 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010417 SDValue LHS = N->getOperand(0);
10418 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010419 unsigned BaseOp = 0;
10420 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010421 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010422 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010423 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010424 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010425 // A subtract of one will be selected as a INC. Note that INC doesn't
10426 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010427 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10428 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010429 BaseOp = X86ISD::INC;
10430 Cond = X86::COND_O;
10431 break;
10432 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010433 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010434 Cond = X86::COND_O;
10435 break;
10436 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010437 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010438 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010439 break;
10440 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010441 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10442 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010443 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10444 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010445 BaseOp = X86ISD::DEC;
10446 Cond = X86::COND_O;
10447 break;
10448 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010449 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010450 Cond = X86::COND_O;
10451 break;
10452 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010453 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010454 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010455 break;
10456 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010457 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010458 Cond = X86::COND_O;
10459 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010460 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10461 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10462 MVT::i32);
10463 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010464
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010465 SDValue SetCC =
10466 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10467 DAG.getConstant(X86::COND_O, MVT::i32),
10468 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010469
Dan Gohman6e5fda22011-07-22 18:45:15 +000010470 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010471 }
Bill Wendling74c37652008-12-09 22:08:41 +000010472 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010473
Bill Wendling61edeb52008-12-02 01:06:39 +000010474 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010475 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010476 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010477
Bill Wendling61edeb52008-12-02 01:06:39 +000010478 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010479 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10480 DAG.getConstant(Cond, MVT::i32),
10481 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010482
Dan Gohman6e5fda22011-07-22 18:45:15 +000010483 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010484}
10485
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010486SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10487 DebugLoc dl = Op.getDebugLoc();
10488 SDNode* Node = Op.getNode();
10489 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
10490 EVT VT = Node->getValueType(0);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010491 if (Subtarget->hasXMMInt() && VT.isVector()) {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010492 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10493 ExtraVT.getScalarType().getSizeInBits();
10494 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10495
10496 unsigned SHLIntrinsicsID = 0;
10497 unsigned SRAIntrinsicsID = 0;
10498 switch (VT.getSimpleVT().SimpleTy) {
10499 default:
10500 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010501 case MVT::v4i32: {
10502 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10503 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10504 break;
10505 }
10506 case MVT::v8i16: {
10507 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10508 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10509 break;
10510 }
10511 }
10512
10513 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10514 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
10515 Node->getOperand(0), ShAmt);
10516
Nadav Rotema7934dd2011-10-10 19:31:45 +000010517 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10518 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10519 Tmp1, ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010520 }
10521
10522 return SDValue();
10523}
10524
10525
Eric Christopher9a9d2752010-07-22 02:48:34 +000010526SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10527 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010528
Eric Christopher77ed1352011-07-08 00:04:56 +000010529 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10530 // There isn't any reason to disable it if the target processor supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010531 if (!Subtarget->hasXMMInt() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010532 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010533 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010534 SDValue Ops[] = {
10535 DAG.getRegister(X86::ESP, MVT::i32), // Base
10536 DAG.getTargetConstant(1, MVT::i8), // Scale
10537 DAG.getRegister(0, MVT::i32), // Index
10538 DAG.getTargetConstant(0, MVT::i32), // Disp
10539 DAG.getRegister(0, MVT::i32), // Segment.
10540 Zero,
10541 Chain
10542 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010543 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010544 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10545 array_lengthof(Ops));
10546 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010547 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010548
Eric Christopher9a9d2752010-07-22 02:48:34 +000010549 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010550 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010551 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010552
Chris Lattner132929a2010-08-14 17:26:09 +000010553 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10554 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10555 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10556 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010557
Chris Lattner132929a2010-08-14 17:26:09 +000010558 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10559 if (!Op1 && !Op2 && !Op3 && Op4)
10560 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010561
Chris Lattner132929a2010-08-14 17:26:09 +000010562 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10563 if (Op1 && !Op2 && !Op3 && !Op4)
10564 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010565
10566 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010567 // (MFENCE)>;
10568 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010569}
10570
Eli Friedman14648462011-07-27 22:21:52 +000010571SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10572 SelectionDAG &DAG) const {
10573 DebugLoc dl = Op.getDebugLoc();
10574 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10575 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10576 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10577 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10578
10579 // The only fence that needs an instruction is a sequentially-consistent
10580 // cross-thread fence.
10581 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10582 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10583 // no-sse2). There isn't any reason to disable it if the target processor
10584 // supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010585 if (Subtarget->hasXMMInt() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010586 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10587
10588 SDValue Chain = Op.getOperand(0);
10589 SDValue Zero = DAG.getConstant(0, MVT::i32);
10590 SDValue Ops[] = {
10591 DAG.getRegister(X86::ESP, MVT::i32), // Base
10592 DAG.getTargetConstant(1, MVT::i8), // Scale
10593 DAG.getRegister(0, MVT::i32), // Index
10594 DAG.getTargetConstant(0, MVT::i32), // Disp
10595 DAG.getRegister(0, MVT::i32), // Segment.
10596 Zero,
10597 Chain
10598 };
10599 SDNode *Res =
10600 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10601 array_lengthof(Ops));
10602 return SDValue(Res, 0);
10603 }
10604
10605 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10606 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10607}
10608
10609
Dan Gohmand858e902010-04-17 15:26:15 +000010610SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010611 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010612 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010613 unsigned Reg = 0;
10614 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010615 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010616 default:
10617 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010618 case MVT::i8: Reg = X86::AL; size = 1; break;
10619 case MVT::i16: Reg = X86::AX; size = 2; break;
10620 case MVT::i32: Reg = X86::EAX; size = 4; break;
10621 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010622 assert(Subtarget->is64Bit() && "Node not type legal!");
10623 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010624 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010625 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010626 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010627 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010628 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010629 Op.getOperand(1),
10630 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010631 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010632 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010633 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010634 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10635 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10636 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010637 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010638 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010639 return cpOut;
10640}
10641
Duncan Sands1607f052008-12-01 11:39:25 +000010642SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010643 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010644 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010645 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010646 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010647 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010648 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010649 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10650 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010651 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010652 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10653 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010654 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010655 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010656 rdx.getValue(1)
10657 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010658 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010659}
10660
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010661SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010662 SelectionDAG &DAG) const {
10663 EVT SrcVT = Op.getOperand(0).getValueType();
10664 EVT DstVT = Op.getValueType();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010665 assert(Subtarget->is64Bit() && !Subtarget->hasXMMInt() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010666 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010667 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010668 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010669 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010670 // i64 <=> MMX conversions are Legal.
10671 if (SrcVT==MVT::i64 && DstVT.isVector())
10672 return Op;
10673 if (DstVT==MVT::i64 && SrcVT.isVector())
10674 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010675 // MMX <=> MMX conversions are Legal.
10676 if (SrcVT.isVector() && DstVT.isVector())
10677 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010678 // All other conversions need to be expanded.
10679 return SDValue();
10680}
Chris Lattner5b856542010-12-20 00:59:46 +000010681
Dan Gohmand858e902010-04-17 15:26:15 +000010682SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010683 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010684 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010685 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010686 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010687 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010688 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010689 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010690 Node->getOperand(0),
10691 Node->getOperand(1), negOp,
10692 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010693 cast<AtomicSDNode>(Node)->getAlignment(),
10694 cast<AtomicSDNode>(Node)->getOrdering(),
10695 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010696}
10697
Eli Friedman327236c2011-08-24 20:50:09 +000010698static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10699 SDNode *Node = Op.getNode();
10700 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010701 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010702
10703 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010704 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10705 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10706 // (The only way to get a 16-byte store is cmpxchg16b)
10707 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10708 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10709 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010710 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10711 cast<AtomicSDNode>(Node)->getMemoryVT(),
10712 Node->getOperand(0),
10713 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010714 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010715 cast<AtomicSDNode>(Node)->getOrdering(),
10716 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010717 return Swap.getValue(1);
10718 }
10719 // Other atomic stores have a simple pattern.
10720 return Op;
10721}
10722
Chris Lattner5b856542010-12-20 00:59:46 +000010723static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10724 EVT VT = Op.getNode()->getValueType(0);
10725
10726 // Let legalize expand this if it isn't a legal type yet.
10727 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10728 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010729
Chris Lattner5b856542010-12-20 00:59:46 +000010730 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010731
Chris Lattner5b856542010-12-20 00:59:46 +000010732 unsigned Opc;
10733 bool ExtraOp = false;
10734 switch (Op.getOpcode()) {
10735 default: assert(0 && "Invalid code");
10736 case ISD::ADDC: Opc = X86ISD::ADD; break;
10737 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10738 case ISD::SUBC: Opc = X86ISD::SUB; break;
10739 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10740 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010741
Chris Lattner5b856542010-12-20 00:59:46 +000010742 if (!ExtraOp)
10743 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10744 Op.getOperand(1));
10745 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10746 Op.getOperand(1), Op.getOperand(2));
10747}
10748
Evan Cheng0db9fe62006-04-25 20:13:52 +000010749/// LowerOperation - Provide custom lowering hooks for some operations.
10750///
Dan Gohmand858e902010-04-17 15:26:15 +000010751SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010752 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010753 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010754 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010755 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010756 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010757 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10758 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010759 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010760 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010761 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010762 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10763 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10764 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010765 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010766 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010767 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10768 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10769 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010770 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010771 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010772 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010773 case ISD::SHL_PARTS:
10774 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010775 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010776 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010777 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010778 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010779 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010780 case ISD::FABS: return LowerFABS(Op, DAG);
10781 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010782 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010783 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010784 case ISD::SETCC: return LowerSETCC(Op, DAG);
10785 case ISD::SELECT: return LowerSELECT(Op, DAG);
10786 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010787 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010788 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010789 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010790 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010791 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010792 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10793 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010794 case ISD::FRAME_TO_ARGS_OFFSET:
10795 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010796 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010797 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010798 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10799 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010800 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010801 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10802 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010803 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010804 case ISD::SRA:
10805 case ISD::SRL:
10806 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010807 case ISD::SADDO:
10808 case ISD::UADDO:
10809 case ISD::SSUBO:
10810 case ISD::USUBO:
10811 case ISD::SMULO:
10812 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010813 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010814 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010815 case ISD::ADDC:
10816 case ISD::ADDE:
10817 case ISD::SUBC:
10818 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010819 case ISD::ADD: return LowerADD(Op, DAG);
10820 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010821 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010822}
10823
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010824static void ReplaceATOMIC_LOAD(SDNode *Node,
10825 SmallVectorImpl<SDValue> &Results,
10826 SelectionDAG &DAG) {
10827 DebugLoc dl = Node->getDebugLoc();
10828 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10829
10830 // Convert wide load -> cmpxchg8b/cmpxchg16b
10831 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10832 // (The only way to get a 16-byte load is cmpxchg16b)
10833 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010834 SDValue Zero = DAG.getConstant(0, VT);
10835 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010836 Node->getOperand(0),
10837 Node->getOperand(1), Zero, Zero,
10838 cast<AtomicSDNode>(Node)->getMemOperand(),
10839 cast<AtomicSDNode>(Node)->getOrdering(),
10840 cast<AtomicSDNode>(Node)->getSynchScope());
10841 Results.push_back(Swap.getValue(0));
10842 Results.push_back(Swap.getValue(1));
10843}
10844
Duncan Sands1607f052008-12-01 11:39:25 +000010845void X86TargetLowering::
10846ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010847 SelectionDAG &DAG, unsigned NewOp) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010848 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000010849 assert (Node->getValueType(0) == MVT::i64 &&
10850 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010851
10852 SDValue Chain = Node->getOperand(0);
10853 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010854 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010855 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010856 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010857 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010858 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010859 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010860 SDValue Result =
10861 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10862 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010863 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010864 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010865 Results.push_back(Result.getValue(2));
10866}
10867
Duncan Sands126d9072008-07-04 11:47:58 +000010868/// ReplaceNodeResults - Replace a node with an illegal result type
10869/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010870void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10871 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010872 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010873 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010874 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010875 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010876 assert(false && "Do not know how to custom type legalize this operation!");
10877 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010878 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010879 case ISD::ADDC:
10880 case ISD::ADDE:
10881 case ISD::SUBC:
10882 case ISD::SUBE:
10883 // We don't want to expand or promote these.
10884 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010885 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010886 std::pair<SDValue,SDValue> Vals =
10887 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010888 SDValue FIST = Vals.first, StackSlot = Vals.second;
10889 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010890 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010891 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010892 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010893 MachinePointerInfo(),
10894 false, false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010895 }
10896 return;
10897 }
10898 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010899 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010900 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010901 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010902 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010903 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010904 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010905 eax.getValue(2));
10906 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10907 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010908 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010909 Results.push_back(edx.getValue(1));
10910 return;
10911 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010912 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010913 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010914 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010915 bool Regs64bit = T == MVT::i128;
10916 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010917 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010918 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10919 DAG.getConstant(0, HalfT));
10920 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10921 DAG.getConstant(1, HalfT));
10922 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10923 Regs64bit ? X86::RAX : X86::EAX,
10924 cpInL, SDValue());
10925 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10926 Regs64bit ? X86::RDX : X86::EDX,
10927 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010928 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010929 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10930 DAG.getConstant(0, HalfT));
10931 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10932 DAG.getConstant(1, HalfT));
10933 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10934 Regs64bit ? X86::RBX : X86::EBX,
10935 swapInL, cpInH.getValue(1));
10936 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10937 Regs64bit ? X86::RCX : X86::ECX,
10938 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010939 SDValue Ops[] = { swapInH.getValue(0),
10940 N->getOperand(1),
10941 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010942 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010943 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010944 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10945 X86ISD::LCMPXCHG8_DAG;
10946 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010947 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010948 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10949 Regs64bit ? X86::RAX : X86::EAX,
10950 HalfT, Result.getValue(1));
10951 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10952 Regs64bit ? X86::RDX : X86::EDX,
10953 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010954 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010955 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010956 Results.push_back(cpOutH.getValue(1));
10957 return;
10958 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010959 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010960 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10961 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010962 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010963 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10964 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010965 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010966 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10967 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010968 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010969 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10970 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010971 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010972 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10973 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010974 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010975 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10976 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010977 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010978 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10979 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010980 case ISD::ATOMIC_LOAD:
10981 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010982 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010983}
10984
Evan Cheng72261582005-12-20 06:22:03 +000010985const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10986 switch (Opcode) {
10987 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010988 case X86ISD::BSF: return "X86ISD::BSF";
10989 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010990 case X86ISD::SHLD: return "X86ISD::SHLD";
10991 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010992 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010993 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010994 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010995 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010996 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010997 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010998 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10999 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11000 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011001 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011002 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011003 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011004 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011005 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011006 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011007 case X86ISD::COMI: return "X86ISD::COMI";
11008 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011009 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011010 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011011 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11012 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011013 case X86ISD::CMOV: return "X86ISD::CMOV";
11014 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011015 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011016 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11017 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011018 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011019 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011020 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011021 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011022 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011023 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11024 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011025 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011026 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011027 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +000011028 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
11029 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
11030 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Evan Cheng8ca29322006-11-10 21:43:37 +000011031 case X86ISD::FMAX: return "X86ISD::FMAX";
11032 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000011033 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11034 case X86ISD::FRCP: return "X86ISD::FRCP";
Duncan Sands17470be2011-09-22 20:15:48 +000011035 case X86ISD::FHADD: return "X86ISD::FHADD";
11036 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011037 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011038 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011039 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011040 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011041 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011042 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11043 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011044 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11045 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11046 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11047 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11048 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11049 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011050 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
11051 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000011052 case X86ISD::VSHL: return "X86ISD::VSHL";
11053 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000011054 case X86ISD::CMPPD: return "X86ISD::CMPPD";
11055 case X86ISD::CMPPS: return "X86ISD::CMPPS";
11056 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
11057 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
11058 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
11059 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
11060 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
11061 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
11062 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
11063 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011064 case X86ISD::ADD: return "X86ISD::ADD";
11065 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011066 case X86ISD::ADC: return "X86ISD::ADC";
11067 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011068 case X86ISD::SMUL: return "X86ISD::SMUL";
11069 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011070 case X86ISD::INC: return "X86ISD::INC";
11071 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011072 case X86ISD::OR: return "X86ISD::OR";
11073 case X86ISD::XOR: return "X86ISD::XOR";
11074 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011075 case X86ISD::ANDN: return "X86ISD::ANDN";
Evan Cheng73f24c92009-03-30 21:36:47 +000011076 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011077 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011078 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011079 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11080 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11081 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
11082 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
11083 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
11084 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
11085 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
11086 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
11087 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011088 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011089 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011090 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011091 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11092 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011093 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11094 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11095 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
11096 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
11097 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
11098 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11099 case X86ISD::MOVSS: return "X86ISD::MOVSS";
11100 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
11101 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +000011102 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011103 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
11104 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
11105 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
11106 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
11107 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
11108 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
11109 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
11110 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
11111 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
11112 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011113 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000011114 case X86ISD::VPERMILPS: return "X86ISD::VPERMILPS";
11115 case X86ISD::VPERMILPSY: return "X86ISD::VPERMILPSY";
11116 case X86ISD::VPERMILPD: return "X86ISD::VPERMILPD";
11117 case X86ISD::VPERMILPDY: return "X86ISD::VPERMILPDY";
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000011118 case X86ISD::VPERM2F128: return "X86ISD::VPERM2F128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011119 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011120 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011121 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011122 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011123 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000011124 }
11125}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011126
Chris Lattnerc9addb72007-03-30 23:15:24 +000011127// isLegalAddressingMode - Return true if the addressing mode represented
11128// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011129bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011130 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011131 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011132 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011133 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011134
Chris Lattnerc9addb72007-03-30 23:15:24 +000011135 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011136 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011137 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011138
Chris Lattnerc9addb72007-03-30 23:15:24 +000011139 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011140 unsigned GVFlags =
11141 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011142
Chris Lattnerdfed4132009-07-10 07:38:24 +000011143 // If a reference to this global requires an extra load, we can't fold it.
11144 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011145 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011146
Chris Lattnerdfed4132009-07-10 07:38:24 +000011147 // If BaseGV requires a register for the PIC base, we cannot also have a
11148 // BaseReg specified.
11149 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011150 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011151
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011152 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011153 if ((M != CodeModel::Small || R != Reloc::Static) &&
11154 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011155 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011156 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011157
Chris Lattnerc9addb72007-03-30 23:15:24 +000011158 switch (AM.Scale) {
11159 case 0:
11160 case 1:
11161 case 2:
11162 case 4:
11163 case 8:
11164 // These scales always work.
11165 break;
11166 case 3:
11167 case 5:
11168 case 9:
11169 // These scales are formed with basereg+scalereg. Only accept if there is
11170 // no basereg yet.
11171 if (AM.HasBaseReg)
11172 return false;
11173 break;
11174 default: // Other stuff never works.
11175 return false;
11176 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011177
Chris Lattnerc9addb72007-03-30 23:15:24 +000011178 return true;
11179}
11180
11181
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011182bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011183 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011184 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011185 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11186 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011187 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011188 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011189 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011190}
11191
Owen Andersone50ed302009-08-10 22:56:29 +000011192bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011193 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011194 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011195 unsigned NumBits1 = VT1.getSizeInBits();
11196 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011197 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011198 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011199 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011200}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011201
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011202bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011203 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011204 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011205}
11206
Owen Andersone50ed302009-08-10 22:56:29 +000011207bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011208 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011209 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011210}
11211
Owen Andersone50ed302009-08-10 22:56:29 +000011212bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011213 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011214 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011215}
11216
Evan Cheng60c07e12006-07-05 22:17:51 +000011217/// isShuffleMaskLegal - Targets can use this to indicate that they only
11218/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11219/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11220/// are assumed to be legal.
11221bool
Eric Christopherfd179292009-08-27 18:07:15 +000011222X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011223 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011224 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011225 if (VT.getSizeInBits() == 64)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000011226 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX());
Nate Begeman9008ca62009-04-27 18:41:29 +000011227
Nate Begemana09008b2009-10-19 02:17:23 +000011228 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011229 return (VT.getVectorNumElements() == 2 ||
11230 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11231 isMOVLMask(M, VT) ||
11232 isSHUFPMask(M, VT) ||
11233 isPSHUFDMask(M, VT) ||
11234 isPSHUFHWMask(M, VT) ||
11235 isPSHUFLWMask(M, VT) ||
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000011236 isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011237 isUNPCKLMask(M, VT) ||
11238 isUNPCKHMask(M, VT) ||
11239 isUNPCKL_v_undef_Mask(M, VT) ||
11240 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011241}
11242
Dan Gohman7d8143f2008-04-09 20:09:42 +000011243bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011244X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011245 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011246 unsigned NumElts = VT.getVectorNumElements();
11247 // FIXME: This collection of masks seems suspect.
11248 if (NumElts == 2)
11249 return true;
11250 if (NumElts == 4 && VT.getSizeInBits() == 128) {
11251 return (isMOVLMask(Mask, VT) ||
11252 isCommutedMOVLMask(Mask, VT, true) ||
11253 isSHUFPMask(Mask, VT) ||
11254 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011255 }
11256 return false;
11257}
11258
11259//===----------------------------------------------------------------------===//
11260// X86 Scheduler Hooks
11261//===----------------------------------------------------------------------===//
11262
Mon P Wang63307c32008-05-05 19:05:59 +000011263// private utility function
11264MachineBasicBlock *
11265X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11266 MachineBasicBlock *MBB,
11267 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011268 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011269 unsigned LoadOpc,
11270 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011271 unsigned notOpc,
11272 unsigned EAXreg,
11273 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011274 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011275 // For the atomic bitwise operator, we generate
11276 // thisMBB:
11277 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011278 // ld t1 = [bitinstr.addr]
11279 // op t2 = t1, [bitinstr.val]
11280 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011281 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11282 // bz newMBB
11283 // fallthrough -->nextMBB
11284 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11285 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011286 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011287 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011288
Mon P Wang63307c32008-05-05 19:05:59 +000011289 /// First build the CFG
11290 MachineFunction *F = MBB->getParent();
11291 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011292 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11293 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11294 F->insert(MBBIter, newMBB);
11295 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011296
Dan Gohman14152b42010-07-06 20:24:04 +000011297 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11298 nextMBB->splice(nextMBB->begin(), thisMBB,
11299 llvm::next(MachineBasicBlock::iterator(bInstr)),
11300 thisMBB->end());
11301 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011302
Mon P Wang63307c32008-05-05 19:05:59 +000011303 // Update thisMBB to fall through to newMBB
11304 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011305
Mon P Wang63307c32008-05-05 19:05:59 +000011306 // newMBB jumps to itself and fall through to nextMBB
11307 newMBB->addSuccessor(nextMBB);
11308 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011309
Mon P Wang63307c32008-05-05 19:05:59 +000011310 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011311 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011312 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011313 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011314 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011315 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011316 int numArgs = bInstr->getNumOperands() - 1;
11317 for (int i=0; i < numArgs; ++i)
11318 argOpers[i] = &bInstr->getOperand(i+1);
11319
11320 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011321 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011322 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011323
Dale Johannesen140be2d2008-08-19 18:47:28 +000011324 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011325 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011326 for (int i=0; i <= lastAddrIndx; ++i)
11327 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011328
Dale Johannesen140be2d2008-08-19 18:47:28 +000011329 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011330 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011331 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011332 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011333 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011334 tt = t1;
11335
Dale Johannesen140be2d2008-08-19 18:47:28 +000011336 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011337 assert((argOpers[valArgIndx]->isReg() ||
11338 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011339 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011340 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011341 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011342 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011343 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011344 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000011345 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011346
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011347 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000011348 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011349
Dale Johannesene4d209d2009-02-03 20:21:25 +000011350 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011351 for (int i=0; i <= lastAddrIndx; ++i)
11352 (*MIB).addOperand(*argOpers[i]);
11353 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000011354 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011355 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11356 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011357
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011358 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011359 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011360
Mon P Wang63307c32008-05-05 19:05:59 +000011361 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011362 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011363
Dan Gohman14152b42010-07-06 20:24:04 +000011364 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011365 return nextMBB;
11366}
11367
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011368// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011369MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011370X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11371 MachineBasicBlock *MBB,
11372 unsigned regOpcL,
11373 unsigned regOpcH,
11374 unsigned immOpcL,
11375 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011376 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011377 // For the atomic bitwise operator, we generate
11378 // thisMBB (instructions are in pairs, except cmpxchg8b)
11379 // ld t1,t2 = [bitinstr.addr]
11380 // newMBB:
11381 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11382 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011383 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011384 // mov ECX, EBX <- t5, t6
11385 // mov EAX, EDX <- t1, t2
11386 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11387 // mov t3, t4 <- EAX, EDX
11388 // bz newMBB
11389 // result in out1, out2
11390 // fallthrough -->nextMBB
11391
11392 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11393 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011394 const unsigned NotOpc = X86::NOT32r;
11395 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11396 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11397 MachineFunction::iterator MBBIter = MBB;
11398 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011399
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011400 /// First build the CFG
11401 MachineFunction *F = MBB->getParent();
11402 MachineBasicBlock *thisMBB = MBB;
11403 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11404 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11405 F->insert(MBBIter, newMBB);
11406 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011407
Dan Gohman14152b42010-07-06 20:24:04 +000011408 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11409 nextMBB->splice(nextMBB->begin(), thisMBB,
11410 llvm::next(MachineBasicBlock::iterator(bInstr)),
11411 thisMBB->end());
11412 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011413
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011414 // Update thisMBB to fall through to newMBB
11415 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011416
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011417 // newMBB jumps to itself and fall through to nextMBB
11418 newMBB->addSuccessor(nextMBB);
11419 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011420
Dale Johannesene4d209d2009-02-03 20:21:25 +000011421 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011422 // Insert instructions into newMBB based on incoming instruction
11423 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011424 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011425 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011426 MachineOperand& dest1Oper = bInstr->getOperand(0);
11427 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011428 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11429 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011430 argOpers[i] = &bInstr->getOperand(i+2);
11431
Dan Gohman71ea4e52010-05-14 21:01:44 +000011432 // We use some of the operands multiple times, so conservatively just
11433 // clear any kill flags that might be present.
11434 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11435 argOpers[i]->setIsKill(false);
11436 }
11437
Evan Chengad5b52f2010-01-08 19:14:57 +000011438 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011439 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011440
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011441 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011442 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011443 for (int i=0; i <= lastAddrIndx; ++i)
11444 (*MIB).addOperand(*argOpers[i]);
11445 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011446 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011447 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011448 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011449 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011450 MachineOperand newOp3 = *(argOpers[3]);
11451 if (newOp3.isImm())
11452 newOp3.setImm(newOp3.getImm()+4);
11453 else
11454 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011455 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011456 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011457
11458 // t3/4 are defined later, at the bottom of the loop
11459 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11460 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011461 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011462 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011463 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011464 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11465
Evan Cheng306b4ca2010-01-08 23:41:50 +000011466 // The subsequent operations should be using the destination registers of
11467 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011468 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011469 t1 = F->getRegInfo().createVirtualRegister(RC);
11470 t2 = F->getRegInfo().createVirtualRegister(RC);
11471 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11472 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011473 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011474 t1 = dest1Oper.getReg();
11475 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011476 }
11477
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011478 int valArgIndx = lastAddrIndx + 1;
11479 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011480 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011481 "invalid operand");
11482 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11483 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011484 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011485 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011486 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011487 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011488 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011489 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011490 (*MIB).addOperand(*argOpers[valArgIndx]);
11491 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011492 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011493 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011494 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011495 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011496 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011497 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011498 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011499 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011500 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011501 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011502
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011503 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011504 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011505 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011506 MIB.addReg(t2);
11507
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011508 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011509 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011510 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011511 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011512
Dale Johannesene4d209d2009-02-03 20:21:25 +000011513 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011514 for (int i=0; i <= lastAddrIndx; ++i)
11515 (*MIB).addOperand(*argOpers[i]);
11516
11517 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011518 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11519 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011520
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011521 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011522 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011523 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011524 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011525
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011526 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011527 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011528
Dan Gohman14152b42010-07-06 20:24:04 +000011529 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011530 return nextMBB;
11531}
11532
11533// private utility function
11534MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011535X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11536 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011537 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011538 // For the atomic min/max operator, we generate
11539 // thisMBB:
11540 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011541 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011542 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011543 // cmp t1, t2
11544 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011545 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011546 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11547 // bz newMBB
11548 // fallthrough -->nextMBB
11549 //
11550 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11551 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011552 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011553 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011554
Mon P Wang63307c32008-05-05 19:05:59 +000011555 /// First build the CFG
11556 MachineFunction *F = MBB->getParent();
11557 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011558 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11559 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11560 F->insert(MBBIter, newMBB);
11561 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011562
Dan Gohman14152b42010-07-06 20:24:04 +000011563 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11564 nextMBB->splice(nextMBB->begin(), thisMBB,
11565 llvm::next(MachineBasicBlock::iterator(mInstr)),
11566 thisMBB->end());
11567 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011568
Mon P Wang63307c32008-05-05 19:05:59 +000011569 // Update thisMBB to fall through to newMBB
11570 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011571
Mon P Wang63307c32008-05-05 19:05:59 +000011572 // newMBB jumps to newMBB and fall through to nextMBB
11573 newMBB->addSuccessor(nextMBB);
11574 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011575
Dale Johannesene4d209d2009-02-03 20:21:25 +000011576 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011577 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011578 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011579 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011580 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011581 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011582 int numArgs = mInstr->getNumOperands() - 1;
11583 for (int i=0; i < numArgs; ++i)
11584 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011585
Mon P Wang63307c32008-05-05 19:05:59 +000011586 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011587 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011588 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011589
Mon P Wangab3e7472008-05-05 22:56:23 +000011590 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011591 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011592 for (int i=0; i <= lastAddrIndx; ++i)
11593 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011594
Mon P Wang63307c32008-05-05 19:05:59 +000011595 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011596 assert((argOpers[valArgIndx]->isReg() ||
11597 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011598 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011599
11600 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011601 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011602 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011603 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011604 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011605 (*MIB).addOperand(*argOpers[valArgIndx]);
11606
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011607 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011608 MIB.addReg(t1);
11609
Dale Johannesene4d209d2009-02-03 20:21:25 +000011610 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011611 MIB.addReg(t1);
11612 MIB.addReg(t2);
11613
11614 // Generate movc
11615 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011616 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011617 MIB.addReg(t2);
11618 MIB.addReg(t1);
11619
11620 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011621 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011622 for (int i=0; i <= lastAddrIndx; ++i)
11623 (*MIB).addOperand(*argOpers[i]);
11624 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011625 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011626 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11627 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011628
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011629 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011630 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011631
Mon P Wang63307c32008-05-05 19:05:59 +000011632 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011633 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011634
Dan Gohman14152b42010-07-06 20:24:04 +000011635 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011636 return nextMBB;
11637}
11638
Eric Christopherf83a5de2009-08-27 18:08:16 +000011639// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011640// or XMM0_V32I8 in AVX all of this code can be replaced with that
11641// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011642MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011643X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011644 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011645 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
11646 "Target must have SSE4.2 or AVX features enabled");
11647
Eric Christopherb120ab42009-08-18 22:50:32 +000011648 DebugLoc dl = MI->getDebugLoc();
11649 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011650 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011651 if (!Subtarget->hasAVX()) {
11652 if (memArg)
11653 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11654 else
11655 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11656 } else {
11657 if (memArg)
11658 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11659 else
11660 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11661 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011662
Eric Christopher41c902f2010-11-30 08:20:21 +000011663 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011664 for (unsigned i = 0; i < numArgs; ++i) {
11665 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011666 if (!(Op.isReg() && Op.isImplicit()))
11667 MIB.addOperand(Op);
11668 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011669 BuildMI(*BB, MI, dl,
11670 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11671 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011672 .addReg(X86::XMM0);
11673
Dan Gohman14152b42010-07-06 20:24:04 +000011674 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011675 return BB;
11676}
11677
11678MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011679X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011680 DebugLoc dl = MI->getDebugLoc();
11681 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011682
Eric Christopher228232b2010-11-30 07:20:12 +000011683 // Address into RAX/EAX, other two args into ECX, EDX.
11684 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11685 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11686 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11687 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011688 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011689
Eric Christopher228232b2010-11-30 07:20:12 +000011690 unsigned ValOps = X86::AddrNumOperands;
11691 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11692 .addReg(MI->getOperand(ValOps).getReg());
11693 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11694 .addReg(MI->getOperand(ValOps+1).getReg());
11695
11696 // The instruction doesn't actually take any operands though.
11697 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011698
Eric Christopher228232b2010-11-30 07:20:12 +000011699 MI->eraseFromParent(); // The pseudo is gone now.
11700 return BB;
11701}
11702
11703MachineBasicBlock *
11704X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011705 DebugLoc dl = MI->getDebugLoc();
11706 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011707
Eric Christopher228232b2010-11-30 07:20:12 +000011708 // First arg in ECX, the second in EAX.
11709 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11710 .addReg(MI->getOperand(0).getReg());
11711 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11712 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011713
Eric Christopher228232b2010-11-30 07:20:12 +000011714 // The instruction doesn't actually take any operands though.
11715 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011716
Eric Christopher228232b2010-11-30 07:20:12 +000011717 MI->eraseFromParent(); // The pseudo is gone now.
11718 return BB;
11719}
11720
11721MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011722X86TargetLowering::EmitVAARG64WithCustomInserter(
11723 MachineInstr *MI,
11724 MachineBasicBlock *MBB) const {
11725 // Emit va_arg instruction on X86-64.
11726
11727 // Operands to this pseudo-instruction:
11728 // 0 ) Output : destination address (reg)
11729 // 1-5) Input : va_list address (addr, i64mem)
11730 // 6 ) ArgSize : Size (in bytes) of vararg type
11731 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11732 // 8 ) Align : Alignment of type
11733 // 9 ) EFLAGS (implicit-def)
11734
11735 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11736 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11737
11738 unsigned DestReg = MI->getOperand(0).getReg();
11739 MachineOperand &Base = MI->getOperand(1);
11740 MachineOperand &Scale = MI->getOperand(2);
11741 MachineOperand &Index = MI->getOperand(3);
11742 MachineOperand &Disp = MI->getOperand(4);
11743 MachineOperand &Segment = MI->getOperand(5);
11744 unsigned ArgSize = MI->getOperand(6).getImm();
11745 unsigned ArgMode = MI->getOperand(7).getImm();
11746 unsigned Align = MI->getOperand(8).getImm();
11747
11748 // Memory Reference
11749 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11750 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11751 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11752
11753 // Machine Information
11754 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11755 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11756 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11757 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11758 DebugLoc DL = MI->getDebugLoc();
11759
11760 // struct va_list {
11761 // i32 gp_offset
11762 // i32 fp_offset
11763 // i64 overflow_area (address)
11764 // i64 reg_save_area (address)
11765 // }
11766 // sizeof(va_list) = 24
11767 // alignment(va_list) = 8
11768
11769 unsigned TotalNumIntRegs = 6;
11770 unsigned TotalNumXMMRegs = 8;
11771 bool UseGPOffset = (ArgMode == 1);
11772 bool UseFPOffset = (ArgMode == 2);
11773 unsigned MaxOffset = TotalNumIntRegs * 8 +
11774 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11775
11776 /* Align ArgSize to a multiple of 8 */
11777 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11778 bool NeedsAlign = (Align > 8);
11779
11780 MachineBasicBlock *thisMBB = MBB;
11781 MachineBasicBlock *overflowMBB;
11782 MachineBasicBlock *offsetMBB;
11783 MachineBasicBlock *endMBB;
11784
11785 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11786 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11787 unsigned OffsetReg = 0;
11788
11789 if (!UseGPOffset && !UseFPOffset) {
11790 // If we only pull from the overflow region, we don't create a branch.
11791 // We don't need to alter control flow.
11792 OffsetDestReg = 0; // unused
11793 OverflowDestReg = DestReg;
11794
11795 offsetMBB = NULL;
11796 overflowMBB = thisMBB;
11797 endMBB = thisMBB;
11798 } else {
11799 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11800 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11801 // If not, pull from overflow_area. (branch to overflowMBB)
11802 //
11803 // thisMBB
11804 // | .
11805 // | .
11806 // offsetMBB overflowMBB
11807 // | .
11808 // | .
11809 // endMBB
11810
11811 // Registers for the PHI in endMBB
11812 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11813 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11814
11815 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11816 MachineFunction *MF = MBB->getParent();
11817 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11818 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11819 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11820
11821 MachineFunction::iterator MBBIter = MBB;
11822 ++MBBIter;
11823
11824 // Insert the new basic blocks
11825 MF->insert(MBBIter, offsetMBB);
11826 MF->insert(MBBIter, overflowMBB);
11827 MF->insert(MBBIter, endMBB);
11828
11829 // Transfer the remainder of MBB and its successor edges to endMBB.
11830 endMBB->splice(endMBB->begin(), thisMBB,
11831 llvm::next(MachineBasicBlock::iterator(MI)),
11832 thisMBB->end());
11833 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11834
11835 // Make offsetMBB and overflowMBB successors of thisMBB
11836 thisMBB->addSuccessor(offsetMBB);
11837 thisMBB->addSuccessor(overflowMBB);
11838
11839 // endMBB is a successor of both offsetMBB and overflowMBB
11840 offsetMBB->addSuccessor(endMBB);
11841 overflowMBB->addSuccessor(endMBB);
11842
11843 // Load the offset value into a register
11844 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11845 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11846 .addOperand(Base)
11847 .addOperand(Scale)
11848 .addOperand(Index)
11849 .addDisp(Disp, UseFPOffset ? 4 : 0)
11850 .addOperand(Segment)
11851 .setMemRefs(MMOBegin, MMOEnd);
11852
11853 // Check if there is enough room left to pull this argument.
11854 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11855 .addReg(OffsetReg)
11856 .addImm(MaxOffset + 8 - ArgSizeA8);
11857
11858 // Branch to "overflowMBB" if offset >= max
11859 // Fall through to "offsetMBB" otherwise
11860 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11861 .addMBB(overflowMBB);
11862 }
11863
11864 // In offsetMBB, emit code to use the reg_save_area.
11865 if (offsetMBB) {
11866 assert(OffsetReg != 0);
11867
11868 // Read the reg_save_area address.
11869 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11870 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11871 .addOperand(Base)
11872 .addOperand(Scale)
11873 .addOperand(Index)
11874 .addDisp(Disp, 16)
11875 .addOperand(Segment)
11876 .setMemRefs(MMOBegin, MMOEnd);
11877
11878 // Zero-extend the offset
11879 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11880 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11881 .addImm(0)
11882 .addReg(OffsetReg)
11883 .addImm(X86::sub_32bit);
11884
11885 // Add the offset to the reg_save_area to get the final address.
11886 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11887 .addReg(OffsetReg64)
11888 .addReg(RegSaveReg);
11889
11890 // Compute the offset for the next argument
11891 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11892 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11893 .addReg(OffsetReg)
11894 .addImm(UseFPOffset ? 16 : 8);
11895
11896 // Store it back into the va_list.
11897 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11898 .addOperand(Base)
11899 .addOperand(Scale)
11900 .addOperand(Index)
11901 .addDisp(Disp, UseFPOffset ? 4 : 0)
11902 .addOperand(Segment)
11903 .addReg(NextOffsetReg)
11904 .setMemRefs(MMOBegin, MMOEnd);
11905
11906 // Jump to endMBB
11907 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11908 .addMBB(endMBB);
11909 }
11910
11911 //
11912 // Emit code to use overflow area
11913 //
11914
11915 // Load the overflow_area address into a register.
11916 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11917 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11918 .addOperand(Base)
11919 .addOperand(Scale)
11920 .addOperand(Index)
11921 .addDisp(Disp, 8)
11922 .addOperand(Segment)
11923 .setMemRefs(MMOBegin, MMOEnd);
11924
11925 // If we need to align it, do so. Otherwise, just copy the address
11926 // to OverflowDestReg.
11927 if (NeedsAlign) {
11928 // Align the overflow address
11929 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11930 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11931
11932 // aligned_addr = (addr + (align-1)) & ~(align-1)
11933 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11934 .addReg(OverflowAddrReg)
11935 .addImm(Align-1);
11936
11937 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11938 .addReg(TmpReg)
11939 .addImm(~(uint64_t)(Align-1));
11940 } else {
11941 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11942 .addReg(OverflowAddrReg);
11943 }
11944
11945 // Compute the next overflow address after this argument.
11946 // (the overflow address should be kept 8-byte aligned)
11947 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11948 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11949 .addReg(OverflowDestReg)
11950 .addImm(ArgSizeA8);
11951
11952 // Store the new overflow address.
11953 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11954 .addOperand(Base)
11955 .addOperand(Scale)
11956 .addOperand(Index)
11957 .addDisp(Disp, 8)
11958 .addOperand(Segment)
11959 .addReg(NextAddrReg)
11960 .setMemRefs(MMOBegin, MMOEnd);
11961
11962 // If we branched, emit the PHI to the front of endMBB.
11963 if (offsetMBB) {
11964 BuildMI(*endMBB, endMBB->begin(), DL,
11965 TII->get(X86::PHI), DestReg)
11966 .addReg(OffsetDestReg).addMBB(offsetMBB)
11967 .addReg(OverflowDestReg).addMBB(overflowMBB);
11968 }
11969
11970 // Erase the pseudo instruction
11971 MI->eraseFromParent();
11972
11973 return endMBB;
11974}
11975
11976MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011977X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11978 MachineInstr *MI,
11979 MachineBasicBlock *MBB) const {
11980 // Emit code to save XMM registers to the stack. The ABI says that the
11981 // number of registers to save is given in %al, so it's theoretically
11982 // possible to do an indirect jump trick to avoid saving all of them,
11983 // however this code takes a simpler approach and just executes all
11984 // of the stores if %al is non-zero. It's less code, and it's probably
11985 // easier on the hardware branch predictor, and stores aren't all that
11986 // expensive anyway.
11987
11988 // Create the new basic blocks. One block contains all the XMM stores,
11989 // and one block is the final destination regardless of whether any
11990 // stores were performed.
11991 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11992 MachineFunction *F = MBB->getParent();
11993 MachineFunction::iterator MBBIter = MBB;
11994 ++MBBIter;
11995 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11996 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11997 F->insert(MBBIter, XMMSaveMBB);
11998 F->insert(MBBIter, EndMBB);
11999
Dan Gohman14152b42010-07-06 20:24:04 +000012000 // Transfer the remainder of MBB and its successor edges to EndMBB.
12001 EndMBB->splice(EndMBB->begin(), MBB,
12002 llvm::next(MachineBasicBlock::iterator(MI)),
12003 MBB->end());
12004 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12005
Dan Gohmand6708ea2009-08-15 01:38:56 +000012006 // The original block will now fall through to the XMM save block.
12007 MBB->addSuccessor(XMMSaveMBB);
12008 // The XMMSaveMBB will fall through to the end block.
12009 XMMSaveMBB->addSuccessor(EndMBB);
12010
12011 // Now add the instructions.
12012 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12013 DebugLoc DL = MI->getDebugLoc();
12014
12015 unsigned CountReg = MI->getOperand(0).getReg();
12016 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12017 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12018
12019 if (!Subtarget->isTargetWin64()) {
12020 // If %al is 0, branch around the XMM save block.
12021 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012022 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012023 MBB->addSuccessor(EndMBB);
12024 }
12025
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012026 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012027 // In the XMM save block, save all the XMM argument registers.
12028 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
12029 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000012030 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000012031 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000012032 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000012033 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000012034 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012035 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000012036 .addFrameIndex(RegSaveFrameIndex)
12037 .addImm(/*Scale=*/1)
12038 .addReg(/*IndexReg=*/0)
12039 .addImm(/*Disp=*/Offset)
12040 .addReg(/*Segment=*/0)
12041 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000012042 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012043 }
12044
Dan Gohman14152b42010-07-06 20:24:04 +000012045 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012046
12047 return EndMBB;
12048}
Mon P Wang63307c32008-05-05 19:05:59 +000012049
Evan Cheng60c07e12006-07-05 22:17:51 +000012050MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012051X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012052 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012053 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12054 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012055
Chris Lattner52600972009-09-02 05:57:00 +000012056 // To "insert" a SELECT_CC instruction, we actually have to insert the
12057 // diamond control-flow pattern. The incoming instruction knows the
12058 // destination vreg to set, the condition code register to branch on, the
12059 // true/false values to select between, and a branch opcode to use.
12060 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12061 MachineFunction::iterator It = BB;
12062 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012063
Chris Lattner52600972009-09-02 05:57:00 +000012064 // thisMBB:
12065 // ...
12066 // TrueVal = ...
12067 // cmpTY ccX, r1, r2
12068 // bCC copy1MBB
12069 // fallthrough --> copy0MBB
12070 MachineBasicBlock *thisMBB = BB;
12071 MachineFunction *F = BB->getParent();
12072 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
12073 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000012074 F->insert(It, copy0MBB);
12075 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000012076
Bill Wendling730c07e2010-06-25 20:48:10 +000012077 // If the EFLAGS register isn't dead in the terminator, then claim that it's
12078 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000012079 if (!MI->killsRegister(X86::EFLAGS)) {
12080 copy0MBB->addLiveIn(X86::EFLAGS);
12081 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000012082 }
12083
Dan Gohman14152b42010-07-06 20:24:04 +000012084 // Transfer the remainder of BB and its successor edges to sinkMBB.
12085 sinkMBB->splice(sinkMBB->begin(), BB,
12086 llvm::next(MachineBasicBlock::iterator(MI)),
12087 BB->end());
12088 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12089
12090 // Add the true and fallthrough blocks as its successors.
12091 BB->addSuccessor(copy0MBB);
12092 BB->addSuccessor(sinkMBB);
12093
12094 // Create the conditional branch instruction.
12095 unsigned Opc =
12096 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12097 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12098
Chris Lattner52600972009-09-02 05:57:00 +000012099 // copy0MBB:
12100 // %FalseValue = ...
12101 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012102 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012103
Chris Lattner52600972009-09-02 05:57:00 +000012104 // sinkMBB:
12105 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12106 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012107 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12108 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012109 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12110 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12111
Dan Gohman14152b42010-07-06 20:24:04 +000012112 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012113 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012114}
12115
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012116MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012117X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12118 bool Is64Bit) const {
12119 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12120 DebugLoc DL = MI->getDebugLoc();
12121 MachineFunction *MF = BB->getParent();
12122 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12123
12124 assert(EnableSegmentedStacks);
12125
12126 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12127 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12128
12129 // BB:
12130 // ... [Till the alloca]
12131 // If stacklet is not large enough, jump to mallocMBB
12132 //
12133 // bumpMBB:
12134 // Allocate by subtracting from RSP
12135 // Jump to continueMBB
12136 //
12137 // mallocMBB:
12138 // Allocate by call to runtime
12139 //
12140 // continueMBB:
12141 // ...
12142 // [rest of original BB]
12143 //
12144
12145 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12146 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12147 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12148
12149 MachineRegisterInfo &MRI = MF->getRegInfo();
12150 const TargetRegisterClass *AddrRegClass =
12151 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12152
12153 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12154 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12155 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012156 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012157 sizeVReg = MI->getOperand(1).getReg(),
12158 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12159
12160 MachineFunction::iterator MBBIter = BB;
12161 ++MBBIter;
12162
12163 MF->insert(MBBIter, bumpMBB);
12164 MF->insert(MBBIter, mallocMBB);
12165 MF->insert(MBBIter, continueMBB);
12166
12167 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12168 (MachineBasicBlock::iterator(MI)), BB->end());
12169 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12170
12171 // Add code to the main basic block to check if the stack limit has been hit,
12172 // and if so, jump to mallocMBB otherwise to bumpMBB.
12173 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012174 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012175 .addReg(tmpSPVReg).addReg(sizeVReg);
12176 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
12177 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012178 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012179 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12180
12181 // bumpMBB simply decreases the stack pointer, since we know the current
12182 // stacklet has enough space.
12183 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012184 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012185 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012186 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012187 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12188
12189 // Calls into a routine in libgcc to allocate more space from the heap.
12190 if (Is64Bit) {
12191 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12192 .addReg(sizeVReg);
12193 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
12194 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
12195 } else {
12196 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12197 .addImm(12);
12198 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12199 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
12200 .addExternalSymbol("__morestack_allocate_stack_space");
12201 }
12202
12203 if (!Is64Bit)
12204 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12205 .addImm(16);
12206
12207 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12208 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12209 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12210
12211 // Set up the CFG correctly.
12212 BB->addSuccessor(bumpMBB);
12213 BB->addSuccessor(mallocMBB);
12214 mallocMBB->addSuccessor(continueMBB);
12215 bumpMBB->addSuccessor(continueMBB);
12216
12217 // Take care of the PHI nodes.
12218 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12219 MI->getOperand(0).getReg())
12220 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12221 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12222
12223 // Delete the original pseudo instruction.
12224 MI->eraseFromParent();
12225
12226 // And we're done.
12227 return continueMBB;
12228}
12229
12230MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012231X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012232 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012233 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12234 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012235
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012236 assert(!Subtarget->isTargetEnvMacho());
12237
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012238 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12239 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012240
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012241 if (Subtarget->isTargetWin64()) {
12242 if (Subtarget->isTargetCygMing()) {
12243 // ___chkstk(Mingw64):
12244 // Clobbers R10, R11, RAX and EFLAGS.
12245 // Updates RSP.
12246 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12247 .addExternalSymbol("___chkstk")
12248 .addReg(X86::RAX, RegState::Implicit)
12249 .addReg(X86::RSP, RegState::Implicit)
12250 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12251 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12252 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12253 } else {
12254 // __chkstk(MSVCRT): does not update stack pointer.
12255 // Clobbers R10, R11 and EFLAGS.
12256 // FIXME: RAX(allocated size) might be reused and not killed.
12257 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12258 .addExternalSymbol("__chkstk")
12259 .addReg(X86::RAX, RegState::Implicit)
12260 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12261 // RAX has the offset to subtracted from RSP.
12262 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12263 .addReg(X86::RSP)
12264 .addReg(X86::RAX);
12265 }
12266 } else {
12267 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012268 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12269
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012270 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12271 .addExternalSymbol(StackProbeSymbol)
12272 .addReg(X86::EAX, RegState::Implicit)
12273 .addReg(X86::ESP, RegState::Implicit)
12274 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12275 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12276 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12277 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012278
Dan Gohman14152b42010-07-06 20:24:04 +000012279 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012280 return BB;
12281}
Chris Lattner52600972009-09-02 05:57:00 +000012282
12283MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012284X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12285 MachineBasicBlock *BB) const {
12286 // This is pretty easy. We're taking the value that we received from
12287 // our load from the relocation, sticking it in either RDI (x86-64)
12288 // or EAX and doing an indirect call. The return value will then
12289 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012290 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012291 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012292 DebugLoc DL = MI->getDebugLoc();
12293 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012294
12295 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012296 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012297
Eric Christopher30ef0e52010-06-03 04:07:48 +000012298 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012299 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12300 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012301 .addReg(X86::RIP)
12302 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012303 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012304 MI->getOperand(3).getTargetFlags())
12305 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012306 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012307 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000012308 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012309 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12310 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012311 .addReg(0)
12312 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012313 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012314 MI->getOperand(3).getTargetFlags())
12315 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012316 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012317 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012318 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012319 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12320 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012321 .addReg(TII->getGlobalBaseReg(F))
12322 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012323 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012324 MI->getOperand(3).getTargetFlags())
12325 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012326 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012327 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012328 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012329
Dan Gohman14152b42010-07-06 20:24:04 +000012330 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012331 return BB;
12332}
12333
12334MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012335X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012336 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012337 switch (MI->getOpcode()) {
Richard Trieu23946fc2011-09-21 03:09:09 +000012338 default: assert(0 && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012339 case X86::TAILJMPd64:
12340 case X86::TAILJMPr64:
12341 case X86::TAILJMPm64:
Richard Trieu23946fc2011-09-21 03:09:09 +000012342 assert(0 && "TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012343 case X86::TCRETURNdi64:
12344 case X86::TCRETURNri64:
12345 case X86::TCRETURNmi64:
12346 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
12347 // On AMD64, additional defs should be added before register allocation.
12348 if (!Subtarget->isTargetWin64()) {
12349 MI->addRegisterDefined(X86::RSI);
12350 MI->addRegisterDefined(X86::RDI);
12351 MI->addRegisterDefined(X86::XMM6);
12352 MI->addRegisterDefined(X86::XMM7);
12353 MI->addRegisterDefined(X86::XMM8);
12354 MI->addRegisterDefined(X86::XMM9);
12355 MI->addRegisterDefined(X86::XMM10);
12356 MI->addRegisterDefined(X86::XMM11);
12357 MI->addRegisterDefined(X86::XMM12);
12358 MI->addRegisterDefined(X86::XMM13);
12359 MI->addRegisterDefined(X86::XMM14);
12360 MI->addRegisterDefined(X86::XMM15);
12361 }
12362 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012363 case X86::WIN_ALLOCA:
12364 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012365 case X86::SEG_ALLOCA_32:
12366 return EmitLoweredSegAlloca(MI, BB, false);
12367 case X86::SEG_ALLOCA_64:
12368 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012369 case X86::TLSCall_32:
12370 case X86::TLSCall_64:
12371 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012372 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012373 case X86::CMOV_FR32:
12374 case X86::CMOV_FR64:
12375 case X86::CMOV_V4F32:
12376 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012377 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012378 case X86::CMOV_V8F32:
12379 case X86::CMOV_V4F64:
12380 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012381 case X86::CMOV_GR16:
12382 case X86::CMOV_GR32:
12383 case X86::CMOV_RFP32:
12384 case X86::CMOV_RFP64:
12385 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012386 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012387
Dale Johannesen849f2142007-07-03 00:53:03 +000012388 case X86::FP32_TO_INT16_IN_MEM:
12389 case X86::FP32_TO_INT32_IN_MEM:
12390 case X86::FP32_TO_INT64_IN_MEM:
12391 case X86::FP64_TO_INT16_IN_MEM:
12392 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012393 case X86::FP64_TO_INT64_IN_MEM:
12394 case X86::FP80_TO_INT16_IN_MEM:
12395 case X86::FP80_TO_INT32_IN_MEM:
12396 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012397 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12398 DebugLoc DL = MI->getDebugLoc();
12399
Evan Cheng60c07e12006-07-05 22:17:51 +000012400 // Change the floating point control register to use "round towards zero"
12401 // mode when truncating to an integer value.
12402 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012403 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012404 addFrameReference(BuildMI(*BB, MI, DL,
12405 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012406
12407 // Load the old value of the high byte of the control word...
12408 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012409 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012410 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012411 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012412
12413 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012414 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012415 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012416
12417 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012418 addFrameReference(BuildMI(*BB, MI, DL,
12419 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012420
12421 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012422 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012423 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012424
12425 // Get the X86 opcode to use.
12426 unsigned Opc;
12427 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012428 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012429 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12430 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12431 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12432 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12433 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12434 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012435 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12436 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12437 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012438 }
12439
12440 X86AddressMode AM;
12441 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012442 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012443 AM.BaseType = X86AddressMode::RegBase;
12444 AM.Base.Reg = Op.getReg();
12445 } else {
12446 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012447 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012448 }
12449 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012450 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012451 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012452 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012453 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012454 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012455 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012456 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012457 AM.GV = Op.getGlobal();
12458 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012459 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012460 }
Dan Gohman14152b42010-07-06 20:24:04 +000012461 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012462 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012463
12464 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012465 addFrameReference(BuildMI(*BB, MI, DL,
12466 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012467
Dan Gohman14152b42010-07-06 20:24:04 +000012468 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012469 return BB;
12470 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012471 // String/text processing lowering.
12472 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012473 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012474 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12475 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012476 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012477 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12478 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012479 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012480 return EmitPCMP(MI, BB, 5, false /* in mem */);
12481 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012482 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012483 return EmitPCMP(MI, BB, 5, true /* in mem */);
12484
Eric Christopher228232b2010-11-30 07:20:12 +000012485 // Thread synchronization.
12486 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012487 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012488 case X86::MWAIT:
12489 return EmitMwait(MI, BB);
12490
Eric Christopherb120ab42009-08-18 22:50:32 +000012491 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012492 case X86::ATOMAND32:
12493 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012494 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012495 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012496 X86::NOT32r, X86::EAX,
12497 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012498 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012499 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12500 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012501 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012502 X86::NOT32r, X86::EAX,
12503 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012504 case X86::ATOMXOR32:
12505 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012506 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012507 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012508 X86::NOT32r, X86::EAX,
12509 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012510 case X86::ATOMNAND32:
12511 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012512 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012513 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012514 X86::NOT32r, X86::EAX,
12515 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012516 case X86::ATOMMIN32:
12517 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12518 case X86::ATOMMAX32:
12519 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12520 case X86::ATOMUMIN32:
12521 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12522 case X86::ATOMUMAX32:
12523 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012524
12525 case X86::ATOMAND16:
12526 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12527 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012528 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012529 X86::NOT16r, X86::AX,
12530 X86::GR16RegisterClass);
12531 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012532 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012533 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012534 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012535 X86::NOT16r, X86::AX,
12536 X86::GR16RegisterClass);
12537 case X86::ATOMXOR16:
12538 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12539 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012540 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012541 X86::NOT16r, X86::AX,
12542 X86::GR16RegisterClass);
12543 case X86::ATOMNAND16:
12544 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12545 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012546 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012547 X86::NOT16r, X86::AX,
12548 X86::GR16RegisterClass, true);
12549 case X86::ATOMMIN16:
12550 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12551 case X86::ATOMMAX16:
12552 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12553 case X86::ATOMUMIN16:
12554 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12555 case X86::ATOMUMAX16:
12556 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12557
12558 case X86::ATOMAND8:
12559 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12560 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012561 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012562 X86::NOT8r, X86::AL,
12563 X86::GR8RegisterClass);
12564 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012565 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012566 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012567 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012568 X86::NOT8r, X86::AL,
12569 X86::GR8RegisterClass);
12570 case X86::ATOMXOR8:
12571 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12572 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012573 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012574 X86::NOT8r, X86::AL,
12575 X86::GR8RegisterClass);
12576 case X86::ATOMNAND8:
12577 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12578 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012579 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012580 X86::NOT8r, X86::AL,
12581 X86::GR8RegisterClass, true);
12582 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012583 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012584 case X86::ATOMAND64:
12585 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012586 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012587 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012588 X86::NOT64r, X86::RAX,
12589 X86::GR64RegisterClass);
12590 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012591 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12592 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012593 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012594 X86::NOT64r, X86::RAX,
12595 X86::GR64RegisterClass);
12596 case X86::ATOMXOR64:
12597 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012598 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012599 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012600 X86::NOT64r, X86::RAX,
12601 X86::GR64RegisterClass);
12602 case X86::ATOMNAND64:
12603 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12604 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012605 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012606 X86::NOT64r, X86::RAX,
12607 X86::GR64RegisterClass, true);
12608 case X86::ATOMMIN64:
12609 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12610 case X86::ATOMMAX64:
12611 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12612 case X86::ATOMUMIN64:
12613 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12614 case X86::ATOMUMAX64:
12615 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012616
12617 // This group does 64-bit operations on a 32-bit host.
12618 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012619 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012620 X86::AND32rr, X86::AND32rr,
12621 X86::AND32ri, X86::AND32ri,
12622 false);
12623 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012624 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012625 X86::OR32rr, X86::OR32rr,
12626 X86::OR32ri, X86::OR32ri,
12627 false);
12628 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012629 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012630 X86::XOR32rr, X86::XOR32rr,
12631 X86::XOR32ri, X86::XOR32ri,
12632 false);
12633 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012634 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012635 X86::AND32rr, X86::AND32rr,
12636 X86::AND32ri, X86::AND32ri,
12637 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012638 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012639 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012640 X86::ADD32rr, X86::ADC32rr,
12641 X86::ADD32ri, X86::ADC32ri,
12642 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012643 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012644 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012645 X86::SUB32rr, X86::SBB32rr,
12646 X86::SUB32ri, X86::SBB32ri,
12647 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012648 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012649 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012650 X86::MOV32rr, X86::MOV32rr,
12651 X86::MOV32ri, X86::MOV32ri,
12652 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012653 case X86::VASTART_SAVE_XMM_REGS:
12654 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012655
12656 case X86::VAARG_64:
12657 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012658 }
12659}
12660
12661//===----------------------------------------------------------------------===//
12662// X86 Optimization Hooks
12663//===----------------------------------------------------------------------===//
12664
Dan Gohman475871a2008-07-27 21:46:04 +000012665void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012666 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012667 APInt &KnownZero,
12668 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012669 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012670 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012671 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012672 assert((Opc >= ISD::BUILTIN_OP_END ||
12673 Opc == ISD::INTRINSIC_WO_CHAIN ||
12674 Opc == ISD::INTRINSIC_W_CHAIN ||
12675 Opc == ISD::INTRINSIC_VOID) &&
12676 "Should use MaskedValueIsZero if you don't know whether Op"
12677 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012678
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012679 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012680 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012681 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012682 case X86ISD::ADD:
12683 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012684 case X86ISD::ADC:
12685 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012686 case X86ISD::SMUL:
12687 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012688 case X86ISD::INC:
12689 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012690 case X86ISD::OR:
12691 case X86ISD::XOR:
12692 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012693 // These nodes' second result is a boolean.
12694 if (Op.getResNo() == 0)
12695 break;
12696 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012697 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012698 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12699 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012700 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012701 case ISD::INTRINSIC_WO_CHAIN: {
12702 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
12703 unsigned NumLoBits = 0;
12704 switch (IntId) {
12705 default: break;
12706 case Intrinsic::x86_sse_movmsk_ps:
12707 case Intrinsic::x86_avx_movmsk_ps_256:
12708 case Intrinsic::x86_sse2_movmsk_pd:
12709 case Intrinsic::x86_avx_movmsk_pd_256:
12710 case Intrinsic::x86_mmx_pmovmskb:
12711 case Intrinsic::x86_sse2_pmovmskb_128: {
12712 // High bits of movmskp{s|d}, pmovmskb are known zero.
12713 switch (IntId) {
12714 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
12715 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
12716 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
12717 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
12718 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
12719 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
12720 }
12721 KnownZero = APInt::getHighBitsSet(Mask.getBitWidth(),
12722 Mask.getBitWidth() - NumLoBits);
12723 break;
12724 }
12725 }
12726 break;
12727 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012728 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012729}
Chris Lattner259e97c2006-01-31 19:43:35 +000012730
Owen Andersonbc146b02010-09-21 20:42:50 +000012731unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12732 unsigned Depth) const {
12733 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12734 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12735 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012736
Owen Andersonbc146b02010-09-21 20:42:50 +000012737 // Fallback case.
12738 return 1;
12739}
12740
Evan Cheng206ee9d2006-07-07 08:33:52 +000012741/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012742/// node is a GlobalAddress + offset.
12743bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012744 const GlobalValue* &GA,
12745 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012746 if (N->getOpcode() == X86ISD::Wrapper) {
12747 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012748 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012749 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012750 return true;
12751 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012752 }
Evan Chengad4196b2008-05-12 19:56:52 +000012753 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012754}
12755
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012756/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12757/// same as extracting the high 128-bit part of 256-bit vector and then
12758/// inserting the result into the low part of a new 256-bit vector
12759static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12760 EVT VT = SVOp->getValueType(0);
12761 int NumElems = VT.getVectorNumElements();
12762
12763 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12764 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12765 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12766 SVOp->getMaskElt(j) >= 0)
12767 return false;
12768
12769 return true;
12770}
12771
12772/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12773/// same as extracting the low 128-bit part of 256-bit vector and then
12774/// inserting the result into the high part of a new 256-bit vector
12775static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12776 EVT VT = SVOp->getValueType(0);
12777 int NumElems = VT.getVectorNumElements();
12778
12779 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12780 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12781 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12782 SVOp->getMaskElt(j) >= 0)
12783 return false;
12784
12785 return true;
12786}
12787
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012788/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12789static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12790 TargetLowering::DAGCombinerInfo &DCI) {
12791 DebugLoc dl = N->getDebugLoc();
12792 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12793 SDValue V1 = SVOp->getOperand(0);
12794 SDValue V2 = SVOp->getOperand(1);
12795 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012796 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012797
12798 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12799 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12800 //
12801 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012802 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012803 // V UNDEF BUILD_VECTOR UNDEF
12804 // \ / \ /
12805 // CONCAT_VECTOR CONCAT_VECTOR
12806 // \ /
12807 // \ /
12808 // RESULT: V + zero extended
12809 //
12810 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12811 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12812 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12813 return SDValue();
12814
12815 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12816 return SDValue();
12817
12818 // To match the shuffle mask, the first half of the mask should
12819 // be exactly the first vector, and all the rest a splat with the
12820 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012821 for (int i = 0; i < NumElems/2; ++i)
12822 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12823 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12824 return SDValue();
12825
12826 // Emit a zeroed vector and insert the desired subvector on its
12827 // first half.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000012828 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012829 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12830 DAG.getConstant(0, MVT::i32), DAG, dl);
12831 return DCI.CombineTo(N, InsV);
12832 }
12833
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012834 //===--------------------------------------------------------------------===//
12835 // Combine some shuffles into subvector extracts and inserts:
12836 //
12837
12838 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12839 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12840 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12841 DAG, dl);
12842 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12843 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12844 return DCI.CombineTo(N, InsV);
12845 }
12846
12847 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12848 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12849 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12850 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12851 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12852 return DCI.CombineTo(N, InsV);
12853 }
12854
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012855 return SDValue();
12856}
12857
12858/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012859static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012860 TargetLowering::DAGCombinerInfo &DCI,
12861 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012862 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012863 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012864
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012865 // Don't create instructions with illegal types after legalize types has run.
12866 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12867 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12868 return SDValue();
12869
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012870 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12871 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12872 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012873 return PerformShuffleCombine256(N, DAG, DCI);
12874
12875 // Only handle 128 wide vector from here on.
12876 if (VT.getSizeInBits() != 128)
12877 return SDValue();
12878
12879 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12880 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12881 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012882 SmallVector<SDValue, 16> Elts;
12883 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012884 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012885
Nate Begemanfdea31a2010-03-24 20:49:50 +000012886 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012887}
Evan Chengd880b972008-05-09 21:53:03 +000012888
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012889/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12890/// generation and convert it from being a bunch of shuffles and extracts
12891/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012892static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12893 const TargetLowering &TLI) {
12894 SDValue InputVector = N->getOperand(0);
12895
12896 // Only operate on vectors of 4 elements, where the alternative shuffling
12897 // gets to be more expensive.
12898 if (InputVector.getValueType() != MVT::v4i32)
12899 return SDValue();
12900
12901 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12902 // single use which is a sign-extend or zero-extend, and all elements are
12903 // used.
12904 SmallVector<SDNode *, 4> Uses;
12905 unsigned ExtractedElements = 0;
12906 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12907 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12908 if (UI.getUse().getResNo() != InputVector.getResNo())
12909 return SDValue();
12910
12911 SDNode *Extract = *UI;
12912 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12913 return SDValue();
12914
12915 if (Extract->getValueType(0) != MVT::i32)
12916 return SDValue();
12917 if (!Extract->hasOneUse())
12918 return SDValue();
12919 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12920 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12921 return SDValue();
12922 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12923 return SDValue();
12924
12925 // Record which element was extracted.
12926 ExtractedElements |=
12927 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12928
12929 Uses.push_back(Extract);
12930 }
12931
12932 // If not all the elements were used, this may not be worthwhile.
12933 if (ExtractedElements != 15)
12934 return SDValue();
12935
12936 // Ok, we've now decided to do the transformation.
12937 DebugLoc dl = InputVector.getDebugLoc();
12938
12939 // Store the value to a temporary stack slot.
12940 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012941 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12942 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012943
12944 // Replace each use (extract) with a load of the appropriate element.
12945 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12946 UE = Uses.end(); UI != UE; ++UI) {
12947 SDNode *Extract = *UI;
12948
Nadav Rotem86694292011-05-17 08:31:57 +000012949 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012950 SDValue Idx = Extract->getOperand(1);
12951 unsigned EltSize =
12952 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12953 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12954 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12955
Nadav Rotem86694292011-05-17 08:31:57 +000012956 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012957 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012958
12959 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012960 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012961 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000012962 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012963
12964 // Replace the exact with the load.
12965 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12966 }
12967
12968 // The replacement was made in place; don't return anything.
12969 return SDValue();
12970}
12971
Duncan Sands6bcd2192011-09-17 16:49:39 +000012972/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
12973/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012974static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012975 const X86Subtarget *Subtarget) {
12976 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012977 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012978 // Get the LHS/RHS of the select.
12979 SDValue LHS = N->getOperand(1);
12980 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000012981 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000012982
Dan Gohman670e5392009-09-21 18:03:22 +000012983 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012984 // instructions match the semantics of the common C idiom x<y?x:y but not
12985 // x<=y?x:y, because of how they handle negative zero (which can be
12986 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000012987 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
12988 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
12989 (Subtarget->hasXMMInt() ||
12990 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012991 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012992
Chris Lattner47b4ce82009-03-11 05:48:52 +000012993 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000012994 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000012995 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12996 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012997 switch (CC) {
12998 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012999 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013000 // Converting this to a min would handle NaNs incorrectly, and swapping
13001 // the operands would cause it to handle comparisons between positive
13002 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013003 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000013004 if (!UnsafeFPMath &&
13005 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13006 break;
13007 std::swap(LHS, RHS);
13008 }
Dan Gohman670e5392009-09-21 18:03:22 +000013009 Opcode = X86ISD::FMIN;
13010 break;
13011 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013012 // Converting this to a min would handle comparisons between positive
13013 // and negative zero incorrectly.
13014 if (!UnsafeFPMath &&
13015 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
13016 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013017 Opcode = X86ISD::FMIN;
13018 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013019 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013020 // Converting this to a min would handle both negative zeros and NaNs
13021 // incorrectly, but we can swap the operands to fix both.
13022 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013023 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013024 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013025 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013026 Opcode = X86ISD::FMIN;
13027 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013028
Dan Gohman670e5392009-09-21 18:03:22 +000013029 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013030 // Converting this to a max would handle comparisons between positive
13031 // and negative zero incorrectly.
13032 if (!UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000013033 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013034 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013035 Opcode = X86ISD::FMAX;
13036 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013037 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013038 // Converting this to a max would handle NaNs incorrectly, and swapping
13039 // the operands would cause it to handle comparisons between positive
13040 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013041 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000013042 if (!UnsafeFPMath &&
13043 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13044 break;
13045 std::swap(LHS, RHS);
13046 }
Dan Gohman670e5392009-09-21 18:03:22 +000013047 Opcode = X86ISD::FMAX;
13048 break;
13049 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013050 // Converting this to a max would handle both negative zeros and NaNs
13051 // incorrectly, but we can swap the operands to fix both.
13052 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013053 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013054 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013055 case ISD::SETGE:
13056 Opcode = X86ISD::FMAX;
13057 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000013058 }
Dan Gohman670e5392009-09-21 18:03:22 +000013059 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000013060 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
13061 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013062 switch (CC) {
13063 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013064 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013065 // Converting this to a min would handle comparisons between positive
13066 // and negative zero incorrectly, and swapping the operands would
13067 // cause it to handle NaNs incorrectly.
13068 if (!UnsafeFPMath &&
13069 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000013070 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013071 break;
13072 std::swap(LHS, RHS);
13073 }
Dan Gohman670e5392009-09-21 18:03:22 +000013074 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000013075 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013076 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013077 // Converting this to a min would handle NaNs incorrectly.
13078 if (!UnsafeFPMath &&
13079 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
13080 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013081 Opcode = X86ISD::FMIN;
13082 break;
13083 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013084 // Converting this to a min would handle both negative zeros and NaNs
13085 // incorrectly, but we can swap the operands to fix both.
13086 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013087 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013088 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013089 case ISD::SETGE:
13090 Opcode = X86ISD::FMIN;
13091 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013092
Dan Gohman670e5392009-09-21 18:03:22 +000013093 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013094 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013095 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013096 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013097 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000013098 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013099 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013100 // Converting this to a max would handle comparisons between positive
13101 // and negative zero incorrectly, and swapping the operands would
13102 // cause it to handle NaNs incorrectly.
13103 if (!UnsafeFPMath &&
13104 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000013105 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013106 break;
13107 std::swap(LHS, RHS);
13108 }
Dan Gohman670e5392009-09-21 18:03:22 +000013109 Opcode = X86ISD::FMAX;
13110 break;
13111 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013112 // Converting this to a max would handle both negative zeros and NaNs
13113 // incorrectly, but we can swap the operands to fix both.
13114 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013115 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013116 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013117 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013118 Opcode = X86ISD::FMAX;
13119 break;
13120 }
Chris Lattner83e6c992006-10-04 06:57:07 +000013121 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013122
Chris Lattner47b4ce82009-03-11 05:48:52 +000013123 if (Opcode)
13124 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000013125 }
Eric Christopherfd179292009-08-27 18:07:15 +000013126
Chris Lattnerd1980a52009-03-12 06:52:53 +000013127 // If this is a select between two integer constants, try to do some
13128 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000013129 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
13130 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000013131 // Don't do this for crazy integer types.
13132 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
13133 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000013134 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013135 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000013136
Chris Lattnercee56e72009-03-13 05:53:31 +000013137 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000013138 // Efficiently invertible.
13139 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
13140 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
13141 isa<ConstantSDNode>(Cond.getOperand(1))))) {
13142 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000013143 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013144 }
Eric Christopherfd179292009-08-27 18:07:15 +000013145
Chris Lattnerd1980a52009-03-12 06:52:53 +000013146 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013147 if (FalseC->getAPIntValue() == 0 &&
13148 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013149 if (NeedsCondInvert) // Invert the condition if needed.
13150 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13151 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013152
Chris Lattnerd1980a52009-03-12 06:52:53 +000013153 // Zero extend the condition if needed.
13154 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013155
Chris Lattnercee56e72009-03-13 05:53:31 +000013156 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013157 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013158 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013159 }
Eric Christopherfd179292009-08-27 18:07:15 +000013160
Chris Lattner97a29a52009-03-13 05:22:11 +000013161 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013162 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013163 if (NeedsCondInvert) // Invert the condition if needed.
13164 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13165 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013166
Chris Lattner97a29a52009-03-13 05:22:11 +000013167 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013168 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13169 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013170 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013171 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013172 }
Eric Christopherfd179292009-08-27 18:07:15 +000013173
Chris Lattnercee56e72009-03-13 05:53:31 +000013174 // Optimize cases that will turn into an LEA instruction. This requires
13175 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013176 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013177 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013178 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013179
Chris Lattnercee56e72009-03-13 05:53:31 +000013180 bool isFastMultiplier = false;
13181 if (Diff < 10) {
13182 switch ((unsigned char)Diff) {
13183 default: break;
13184 case 1: // result = add base, cond
13185 case 2: // result = lea base( , cond*2)
13186 case 3: // result = lea base(cond, cond*2)
13187 case 4: // result = lea base( , cond*4)
13188 case 5: // result = lea base(cond, cond*4)
13189 case 8: // result = lea base( , cond*8)
13190 case 9: // result = lea base(cond, cond*8)
13191 isFastMultiplier = true;
13192 break;
13193 }
13194 }
Eric Christopherfd179292009-08-27 18:07:15 +000013195
Chris Lattnercee56e72009-03-13 05:53:31 +000013196 if (isFastMultiplier) {
13197 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
13198 if (NeedsCondInvert) // Invert the condition if needed.
13199 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13200 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013201
Chris Lattnercee56e72009-03-13 05:53:31 +000013202 // Zero extend the condition if needed.
13203 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13204 Cond);
13205 // Scale the condition by the difference.
13206 if (Diff != 1)
13207 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13208 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013209
Chris Lattnercee56e72009-03-13 05:53:31 +000013210 // Add the base if non-zero.
13211 if (FalseC->getAPIntValue() != 0)
13212 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13213 SDValue(FalseC, 0));
13214 return Cond;
13215 }
Eric Christopherfd179292009-08-27 18:07:15 +000013216 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013217 }
13218 }
Eric Christopherfd179292009-08-27 18:07:15 +000013219
Dan Gohman475871a2008-07-27 21:46:04 +000013220 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000013221}
13222
Chris Lattnerd1980a52009-03-12 06:52:53 +000013223/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
13224static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
13225 TargetLowering::DAGCombinerInfo &DCI) {
13226 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000013227
Chris Lattnerd1980a52009-03-12 06:52:53 +000013228 // If the flag operand isn't dead, don't touch this CMOV.
13229 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
13230 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000013231
Evan Chengb5a55d92011-05-24 01:48:22 +000013232 SDValue FalseOp = N->getOperand(0);
13233 SDValue TrueOp = N->getOperand(1);
13234 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
13235 SDValue Cond = N->getOperand(3);
13236 if (CC == X86::COND_E || CC == X86::COND_NE) {
13237 switch (Cond.getOpcode()) {
13238 default: break;
13239 case X86ISD::BSR:
13240 case X86ISD::BSF:
13241 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
13242 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
13243 return (CC == X86::COND_E) ? FalseOp : TrueOp;
13244 }
13245 }
13246
Chris Lattnerd1980a52009-03-12 06:52:53 +000013247 // If this is a select between two integer constants, try to do some
13248 // optimizations. Note that the operands are ordered the opposite of SELECT
13249 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000013250 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
13251 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013252 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
13253 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000013254 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
13255 CC = X86::GetOppositeBranchCondition(CC);
13256 std::swap(TrueC, FalseC);
13257 }
Eric Christopherfd179292009-08-27 18:07:15 +000013258
Chris Lattnerd1980a52009-03-12 06:52:53 +000013259 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013260 // This is efficient for any integer data type (including i8/i16) and
13261 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013262 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013263 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13264 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013265
Chris Lattnerd1980a52009-03-12 06:52:53 +000013266 // Zero extend the condition if needed.
13267 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013268
Chris Lattnerd1980a52009-03-12 06:52:53 +000013269 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
13270 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013271 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013272 if (N->getNumValues() == 2) // Dead flag value?
13273 return DCI.CombineTo(N, Cond, SDValue());
13274 return Cond;
13275 }
Eric Christopherfd179292009-08-27 18:07:15 +000013276
Chris Lattnercee56e72009-03-13 05:53:31 +000013277 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
13278 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000013279 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013280 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13281 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013282
Chris Lattner97a29a52009-03-13 05:22:11 +000013283 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013284 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13285 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013286 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13287 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000013288
Chris Lattner97a29a52009-03-13 05:22:11 +000013289 if (N->getNumValues() == 2) // Dead flag value?
13290 return DCI.CombineTo(N, Cond, SDValue());
13291 return Cond;
13292 }
Eric Christopherfd179292009-08-27 18:07:15 +000013293
Chris Lattnercee56e72009-03-13 05:53:31 +000013294 // Optimize cases that will turn into an LEA instruction. This requires
13295 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013296 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013297 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013298 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013299
Chris Lattnercee56e72009-03-13 05:53:31 +000013300 bool isFastMultiplier = false;
13301 if (Diff < 10) {
13302 switch ((unsigned char)Diff) {
13303 default: break;
13304 case 1: // result = add base, cond
13305 case 2: // result = lea base( , cond*2)
13306 case 3: // result = lea base(cond, cond*2)
13307 case 4: // result = lea base( , cond*4)
13308 case 5: // result = lea base(cond, cond*4)
13309 case 8: // result = lea base( , cond*8)
13310 case 9: // result = lea base(cond, cond*8)
13311 isFastMultiplier = true;
13312 break;
13313 }
13314 }
Eric Christopherfd179292009-08-27 18:07:15 +000013315
Chris Lattnercee56e72009-03-13 05:53:31 +000013316 if (isFastMultiplier) {
13317 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013318 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13319 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000013320 // Zero extend the condition if needed.
13321 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13322 Cond);
13323 // Scale the condition by the difference.
13324 if (Diff != 1)
13325 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13326 DAG.getConstant(Diff, Cond.getValueType()));
13327
13328 // Add the base if non-zero.
13329 if (FalseC->getAPIntValue() != 0)
13330 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13331 SDValue(FalseC, 0));
13332 if (N->getNumValues() == 2) // Dead flag value?
13333 return DCI.CombineTo(N, Cond, SDValue());
13334 return Cond;
13335 }
Eric Christopherfd179292009-08-27 18:07:15 +000013336 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013337 }
13338 }
13339 return SDValue();
13340}
13341
13342
Evan Cheng0b0cd912009-03-28 05:57:29 +000013343/// PerformMulCombine - Optimize a single multiply with constant into two
13344/// in order to implement it with two cheaper instructions, e.g.
13345/// LEA + SHL, LEA + LEA.
13346static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
13347 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000013348 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
13349 return SDValue();
13350
Owen Andersone50ed302009-08-10 22:56:29 +000013351 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000013352 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000013353 return SDValue();
13354
13355 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
13356 if (!C)
13357 return SDValue();
13358 uint64_t MulAmt = C->getZExtValue();
13359 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
13360 return SDValue();
13361
13362 uint64_t MulAmt1 = 0;
13363 uint64_t MulAmt2 = 0;
13364 if ((MulAmt % 9) == 0) {
13365 MulAmt1 = 9;
13366 MulAmt2 = MulAmt / 9;
13367 } else if ((MulAmt % 5) == 0) {
13368 MulAmt1 = 5;
13369 MulAmt2 = MulAmt / 5;
13370 } else if ((MulAmt % 3) == 0) {
13371 MulAmt1 = 3;
13372 MulAmt2 = MulAmt / 3;
13373 }
13374 if (MulAmt2 &&
13375 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
13376 DebugLoc DL = N->getDebugLoc();
13377
13378 if (isPowerOf2_64(MulAmt2) &&
13379 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
13380 // If second multiplifer is pow2, issue it first. We want the multiply by
13381 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
13382 // is an add.
13383 std::swap(MulAmt1, MulAmt2);
13384
13385 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000013386 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013387 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000013388 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000013389 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013390 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000013391 DAG.getConstant(MulAmt1, VT));
13392
Eric Christopherfd179292009-08-27 18:07:15 +000013393 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013394 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000013395 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000013396 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013397 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000013398 DAG.getConstant(MulAmt2, VT));
13399
13400 // Do not add new nodes to DAG combiner worklist.
13401 DCI.CombineTo(N, NewMul, false);
13402 }
13403 return SDValue();
13404}
13405
Evan Chengad9c0a32009-12-15 00:53:42 +000013406static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
13407 SDValue N0 = N->getOperand(0);
13408 SDValue N1 = N->getOperand(1);
13409 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13410 EVT VT = N0.getValueType();
13411
13412 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13413 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013414 if (VT.isInteger() && !VT.isVector() &&
13415 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000013416 N0.getOperand(1).getOpcode() == ISD::Constant) {
13417 SDValue N00 = N0.getOperand(0);
13418 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13419 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13420 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13421 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13422 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13423 APInt ShAmt = N1C->getAPIntValue();
13424 Mask = Mask.shl(ShAmt);
13425 if (Mask != 0)
13426 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13427 N00, DAG.getConstant(Mask, VT));
13428 }
13429 }
13430
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013431
13432 // Hardware support for vector shifts is sparse which makes us scalarize the
13433 // vector operations in many cases. Also, on sandybridge ADD is faster than
13434 // shl.
13435 // (shl V, 1) -> add V,V
13436 if (isSplatVector(N1.getNode())) {
13437 assert(N0.getValueType().isVector() && "Invalid vector shift type");
13438 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
13439 // We shift all of the values by one. In many cases we do not have
13440 // hardware support for this operation. This is better expressed as an ADD
13441 // of two values.
13442 if (N1C && (1 == N1C->getZExtValue())) {
13443 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
13444 }
13445 }
13446
Evan Chengad9c0a32009-12-15 00:53:42 +000013447 return SDValue();
13448}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013449
Nate Begeman740ab032009-01-26 00:52:55 +000013450/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13451/// when possible.
13452static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13453 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013454 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013455 if (N->getOpcode() == ISD::SHL) {
13456 SDValue V = PerformSHLCombine(N, DAG);
13457 if (V.getNode()) return V;
13458 }
Evan Chengad9c0a32009-12-15 00:53:42 +000013459
Nate Begeman740ab032009-01-26 00:52:55 +000013460 // On X86 with SSE2 support, we can transform this to a vector shift if
13461 // all elements are shifted by the same amount. We can't do this in legalize
13462 // because the a constant vector is typically transformed to a constant pool
13463 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013464 if (!Subtarget->hasXMMInt())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013465 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013466
Owen Anderson825b72b2009-08-11 20:47:22 +000013467 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013468 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013469
Mon P Wang3becd092009-01-28 08:12:05 +000013470 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013471 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013472 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013473 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013474 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13475 unsigned NumElts = VT.getVectorNumElements();
13476 unsigned i = 0;
13477 for (; i != NumElts; ++i) {
13478 SDValue Arg = ShAmtOp.getOperand(i);
13479 if (Arg.getOpcode() == ISD::UNDEF) continue;
13480 BaseShAmt = Arg;
13481 break;
13482 }
13483 for (; i != NumElts; ++i) {
13484 SDValue Arg = ShAmtOp.getOperand(i);
13485 if (Arg.getOpcode() == ISD::UNDEF) continue;
13486 if (Arg != BaseShAmt) {
13487 return SDValue();
13488 }
13489 }
13490 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013491 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013492 SDValue InVec = ShAmtOp.getOperand(0);
13493 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13494 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13495 unsigned i = 0;
13496 for (; i != NumElts; ++i) {
13497 SDValue Arg = InVec.getOperand(i);
13498 if (Arg.getOpcode() == ISD::UNDEF) continue;
13499 BaseShAmt = Arg;
13500 break;
13501 }
13502 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13503 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013504 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013505 if (C->getZExtValue() == SplatIdx)
13506 BaseShAmt = InVec.getOperand(1);
13507 }
13508 }
13509 if (BaseShAmt.getNode() == 0)
13510 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13511 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013512 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013513 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013514
Mon P Wangefa42202009-09-03 19:56:25 +000013515 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013516 if (EltVT.bitsGT(MVT::i32))
13517 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13518 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013519 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013520
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013521 // The shift amount is identical so we can do a vector shift.
13522 SDValue ValOp = N->getOperand(0);
13523 switch (N->getOpcode()) {
13524 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013525 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013526 break;
13527 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013528 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013529 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013530 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013531 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013532 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013533 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013534 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013535 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013536 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013537 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013538 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013539 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013540 break;
13541 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013542 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013543 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013544 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013545 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013546 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013547 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013548 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013549 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013550 break;
13551 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013552 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013553 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013554 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013555 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013556 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013557 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013558 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013559 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013560 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013561 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013562 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013563 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013564 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013565 }
13566 return SDValue();
13567}
13568
Nate Begemanb65c1752010-12-17 22:55:37 +000013569
Stuart Hastings865f0932011-06-03 23:53:54 +000013570// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13571// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13572// and friends. Likewise for OR -> CMPNEQSS.
13573static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13574 TargetLowering::DAGCombinerInfo &DCI,
13575 const X86Subtarget *Subtarget) {
13576 unsigned opcode;
13577
13578 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13579 // we're requiring SSE2 for both.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013580 if (Subtarget->hasXMMInt() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013581 SDValue N0 = N->getOperand(0);
13582 SDValue N1 = N->getOperand(1);
13583 SDValue CMP0 = N0->getOperand(1);
13584 SDValue CMP1 = N1->getOperand(1);
13585 DebugLoc DL = N->getDebugLoc();
13586
13587 // The SETCCs should both refer to the same CMP.
13588 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13589 return SDValue();
13590
13591 SDValue CMP00 = CMP0->getOperand(0);
13592 SDValue CMP01 = CMP0->getOperand(1);
13593 EVT VT = CMP00.getValueType();
13594
13595 if (VT == MVT::f32 || VT == MVT::f64) {
13596 bool ExpectingFlags = false;
13597 // Check for any users that want flags:
13598 for (SDNode::use_iterator UI = N->use_begin(),
13599 UE = N->use_end();
13600 !ExpectingFlags && UI != UE; ++UI)
13601 switch (UI->getOpcode()) {
13602 default:
13603 case ISD::BR_CC:
13604 case ISD::BRCOND:
13605 case ISD::SELECT:
13606 ExpectingFlags = true;
13607 break;
13608 case ISD::CopyToReg:
13609 case ISD::SIGN_EXTEND:
13610 case ISD::ZERO_EXTEND:
13611 case ISD::ANY_EXTEND:
13612 break;
13613 }
13614
13615 if (!ExpectingFlags) {
13616 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13617 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13618
13619 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13620 X86::CondCode tmp = cc0;
13621 cc0 = cc1;
13622 cc1 = tmp;
13623 }
13624
13625 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13626 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13627 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13628 X86ISD::NodeType NTOperator = is64BitFP ?
13629 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13630 // FIXME: need symbolic constants for these magic numbers.
13631 // See X86ATTInstPrinter.cpp:printSSECC().
13632 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13633 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13634 DAG.getConstant(x86cc, MVT::i8));
13635 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13636 OnesOrZeroesF);
13637 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13638 DAG.getConstant(1, MVT::i32));
13639 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13640 return OneBitOfTruth;
13641 }
13642 }
13643 }
13644 }
13645 return SDValue();
13646}
13647
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013648/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13649/// so it can be folded inside ANDNP.
13650static bool CanFoldXORWithAllOnes(const SDNode *N) {
13651 EVT VT = N->getValueType(0);
13652
13653 // Match direct AllOnes for 128 and 256-bit vectors
13654 if (ISD::isBuildVectorAllOnes(N))
13655 return true;
13656
13657 // Look through a bit convert.
13658 if (N->getOpcode() == ISD::BITCAST)
13659 N = N->getOperand(0).getNode();
13660
13661 // Sometimes the operand may come from a insert_subvector building a 256-bit
13662 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013663 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013664 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13665 SDValue V1 = N->getOperand(0);
13666 SDValue V2 = N->getOperand(1);
13667
13668 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13669 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13670 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13671 ISD::isBuildVectorAllOnes(V2.getNode()))
13672 return true;
13673 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013674
13675 return false;
13676}
13677
Nate Begemanb65c1752010-12-17 22:55:37 +000013678static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13679 TargetLowering::DAGCombinerInfo &DCI,
13680 const X86Subtarget *Subtarget) {
13681 if (DCI.isBeforeLegalizeOps())
13682 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013683
Stuart Hastings865f0932011-06-03 23:53:54 +000013684 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13685 if (R.getNode())
13686 return R;
13687
Craig Topper54a11172011-10-14 07:06:56 +000013688 EVT VT = N->getValueType(0);
13689
Craig Topperb4c94572011-10-21 06:55:01 +000013690 // Create ANDN, BLSI, and BLSR instructions
13691 // BLSI is X & (-X)
13692 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000013693 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
13694 SDValue N0 = N->getOperand(0);
13695 SDValue N1 = N->getOperand(1);
13696 DebugLoc DL = N->getDebugLoc();
13697
13698 // Check LHS for not
13699 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
13700 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
13701 // Check RHS for not
13702 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
13703 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
13704
Craig Topperb4c94572011-10-21 06:55:01 +000013705 // Check LHS for neg
13706 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
13707 isZero(N0.getOperand(0)))
13708 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
13709
13710 // Check RHS for neg
13711 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
13712 isZero(N1.getOperand(0)))
13713 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
13714
13715 // Check LHS for X-1
13716 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13717 isAllOnes(N0.getOperand(1)))
13718 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
13719
13720 // Check RHS for X-1
13721 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13722 isAllOnes(N1.getOperand(1)))
13723 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
13724
Craig Topper54a11172011-10-14 07:06:56 +000013725 return SDValue();
13726 }
13727
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013728 // Want to form ANDNP nodes:
13729 // 1) In the hopes of then easily combining them with OR and AND nodes
13730 // to form PBLEND/PSIGN.
13731 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013732 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013733 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013734
Nate Begemanb65c1752010-12-17 22:55:37 +000013735 SDValue N0 = N->getOperand(0);
13736 SDValue N1 = N->getOperand(1);
13737 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013738
Nate Begemanb65c1752010-12-17 22:55:37 +000013739 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013740 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013741 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13742 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013743 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013744
13745 // Check RHS for vnot
13746 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013747 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13748 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013749 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013750
Nate Begemanb65c1752010-12-17 22:55:37 +000013751 return SDValue();
13752}
13753
Evan Cheng760d1942010-01-04 21:22:48 +000013754static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013755 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013756 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013757 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013758 return SDValue();
13759
Stuart Hastings865f0932011-06-03 23:53:54 +000013760 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13761 if (R.getNode())
13762 return R;
13763
Evan Cheng760d1942010-01-04 21:22:48 +000013764 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000013765 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000013766 return SDValue();
13767
Evan Cheng760d1942010-01-04 21:22:48 +000013768 SDValue N0 = N->getOperand(0);
13769 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013770
Nate Begemanb65c1752010-12-17 22:55:37 +000013771 // look for psign/blend
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013772 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013773 if (VT == MVT::v2i64) {
13774 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013775 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000013776 std::swap(N0, N1);
13777 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013778 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013779 SDValue Mask = N1.getOperand(0);
13780 SDValue X = N1.getOperand(1);
13781 SDValue Y;
13782 if (N0.getOperand(0) == Mask)
13783 Y = N0.getOperand(1);
13784 if (N0.getOperand(1) == Mask)
13785 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013786
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013787 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000013788 if (!Y.getNode())
13789 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013790
Nate Begemanb65c1752010-12-17 22:55:37 +000013791 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13792 if (Mask.getOpcode() != ISD::BITCAST ||
13793 X.getOpcode() != ISD::BITCAST ||
13794 Y.getOpcode() != ISD::BITCAST)
13795 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013796
Nate Begemanb65c1752010-12-17 22:55:37 +000013797 // Look through mask bitcast.
13798 Mask = Mask.getOperand(0);
13799 EVT MaskVT = Mask.getValueType();
13800
13801 // Validate that the Mask operand is a vector sra node. The sra node
13802 // will be an intrinsic.
13803 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13804 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013805
Nate Begemanb65c1752010-12-17 22:55:37 +000013806 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13807 // there is no psrai.b
13808 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13809 case Intrinsic::x86_sse2_psrai_w:
13810 case Intrinsic::x86_sse2_psrai_d:
13811 break;
13812 default: return SDValue();
13813 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013814
Nate Begemanb65c1752010-12-17 22:55:37 +000013815 // Check that the SRA is all signbits.
13816 SDValue SraC = Mask.getOperand(2);
13817 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13818 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13819 if ((SraAmt + 1) != EltBits)
13820 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013821
Nate Begemanb65c1752010-12-17 22:55:37 +000013822 DebugLoc DL = N->getDebugLoc();
13823
13824 // Now we know we at least have a plendvb with the mask val. See if
13825 // we can form a psignb/w/d.
13826 // psign = x.type == y.type == mask.type && y = sub(0, x);
13827 X = X.getOperand(0);
13828 Y = Y.getOperand(0);
13829 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13830 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
13831 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
13832 unsigned Opc = 0;
13833 switch (EltBits) {
13834 case 8: Opc = X86ISD::PSIGNB; break;
13835 case 16: Opc = X86ISD::PSIGNW; break;
13836 case 32: Opc = X86ISD::PSIGND; break;
13837 default: break;
13838 }
13839 if (Opc) {
13840 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
13841 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
13842 }
13843 }
13844 // PBLENDVB only available on SSE 4.1
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013845 if (!(Subtarget->hasSSE41() || Subtarget->hasAVX()))
Nate Begemanb65c1752010-12-17 22:55:37 +000013846 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013847
Nate Begemanb65c1752010-12-17 22:55:37 +000013848 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
13849 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
13850 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000013851 Mask = DAG.getNode(ISD::VSELECT, DL, MVT::v16i8, Mask, X, Y);
Nate Begemanb65c1752010-12-17 22:55:37 +000013852 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
13853 }
13854 }
13855 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013856
Nate Begemanb65c1752010-12-17 22:55:37 +000013857 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013858 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13859 std::swap(N0, N1);
13860 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13861 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013862 if (!N0.hasOneUse() || !N1.hasOneUse())
13863 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013864
13865 SDValue ShAmt0 = N0.getOperand(1);
13866 if (ShAmt0.getValueType() != MVT::i8)
13867 return SDValue();
13868 SDValue ShAmt1 = N1.getOperand(1);
13869 if (ShAmt1.getValueType() != MVT::i8)
13870 return SDValue();
13871 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13872 ShAmt0 = ShAmt0.getOperand(0);
13873 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13874 ShAmt1 = ShAmt1.getOperand(0);
13875
13876 DebugLoc DL = N->getDebugLoc();
13877 unsigned Opc = X86ISD::SHLD;
13878 SDValue Op0 = N0.getOperand(0);
13879 SDValue Op1 = N1.getOperand(0);
13880 if (ShAmt0.getOpcode() == ISD::SUB) {
13881 Opc = X86ISD::SHRD;
13882 std::swap(Op0, Op1);
13883 std::swap(ShAmt0, ShAmt1);
13884 }
13885
Evan Cheng8b1190a2010-04-28 01:18:01 +000013886 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013887 if (ShAmt1.getOpcode() == ISD::SUB) {
13888 SDValue Sum = ShAmt1.getOperand(0);
13889 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013890 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13891 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13892 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13893 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013894 return DAG.getNode(Opc, DL, VT,
13895 Op0, Op1,
13896 DAG.getNode(ISD::TRUNCATE, DL,
13897 MVT::i8, ShAmt0));
13898 }
13899 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13900 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13901 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013902 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013903 return DAG.getNode(Opc, DL, VT,
13904 N0.getOperand(0), N1.getOperand(0),
13905 DAG.getNode(ISD::TRUNCATE, DL,
13906 MVT::i8, ShAmt0));
13907 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013908
Evan Cheng760d1942010-01-04 21:22:48 +000013909 return SDValue();
13910}
13911
Craig Topperb4c94572011-10-21 06:55:01 +000013912static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
13913 TargetLowering::DAGCombinerInfo &DCI,
13914 const X86Subtarget *Subtarget) {
13915 if (DCI.isBeforeLegalizeOps())
13916 return SDValue();
13917
13918 EVT VT = N->getValueType(0);
13919
13920 if (VT != MVT::i32 && VT != MVT::i64)
13921 return SDValue();
13922
13923 // Create BLSMSK instructions by finding X ^ (X-1)
13924 SDValue N0 = N->getOperand(0);
13925 SDValue N1 = N->getOperand(1);
13926 DebugLoc DL = N->getDebugLoc();
13927
13928 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13929 isAllOnes(N0.getOperand(1)))
13930 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
13931
13932 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13933 isAllOnes(N1.getOperand(1)))
13934 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
13935
13936 return SDValue();
13937}
13938
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013939/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
13940static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
13941 const X86Subtarget *Subtarget) {
13942 LoadSDNode *Ld = cast<LoadSDNode>(N);
13943 EVT RegVT = Ld->getValueType(0);
13944 EVT MemVT = Ld->getMemoryVT();
13945 DebugLoc dl = Ld->getDebugLoc();
13946 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13947
13948 ISD::LoadExtType Ext = Ld->getExtensionType();
13949
Nadav Rotemca6f2962011-09-18 19:00:23 +000013950 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013951 // shuffle. We need SSE4 for the shuffles.
13952 // TODO: It is possible to support ZExt by zeroing the undef values
13953 // during the shuffle phase or after the shuffle.
13954 if (RegVT.isVector() && Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
13955 assert(MemVT != RegVT && "Cannot extend to the same type");
13956 assert(MemVT.isVector() && "Must load a vector from memory");
13957
13958 unsigned NumElems = RegVT.getVectorNumElements();
13959 unsigned RegSz = RegVT.getSizeInBits();
13960 unsigned MemSz = MemVT.getSizeInBits();
13961 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000013962 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013963 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
13964
13965 // Attempt to load the original value using a single load op.
13966 // Find a scalar type which is equal to the loaded word size.
13967 MVT SclrLoadTy = MVT::i8;
13968 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13969 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13970 MVT Tp = (MVT::SimpleValueType)tp;
13971 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() == MemSz) {
13972 SclrLoadTy = Tp;
13973 break;
13974 }
13975 }
13976
13977 // Proceed if a load word is found.
13978 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
13979
13980 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
13981 RegSz/SclrLoadTy.getSizeInBits());
13982
13983 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
13984 RegSz/MemVT.getScalarType().getSizeInBits());
13985 // Can't shuffle using an illegal type.
13986 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13987
13988 // Perform a single load.
13989 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
13990 Ld->getBasePtr(),
13991 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013992 Ld->isNonTemporal(), Ld->isInvariant(),
13993 Ld->getAlignment());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013994
13995 // Insert the word loaded into a vector.
13996 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
13997 LoadUnitVecVT, ScalarLoad);
13998
13999 // Bitcast the loaded value to a vector of the original element type, in
14000 // the size of the target vector type.
14001 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, ScalarInVector);
14002 unsigned SizeRatio = RegSz/MemSz;
14003
14004 // Redistribute the loaded elements into the different locations.
14005 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
14006 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
14007
14008 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
14009 DAG.getUNDEF(SlicedVec.getValueType()),
14010 ShuffleVec.data());
14011
14012 // Bitcast to the requested type.
14013 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
14014 // Replace the original load with the new sequence
14015 // and return the new chain.
14016 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
14017 return SDValue(ScalarLoad.getNode(), 1);
14018 }
14019
14020 return SDValue();
14021}
14022
Chris Lattner149a4e52008-02-22 02:09:43 +000014023/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014024static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000014025 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000014026 StoreSDNode *St = cast<StoreSDNode>(N);
14027 EVT VT = St->getValue().getValueType();
14028 EVT StVT = St->getMemoryVT();
14029 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000014030 SDValue StoredVal = St->getOperand(1);
14031 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14032
14033 // If we are saving a concatination of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000014034 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
14035 // 128-bit ones. If in the future the cost becomes only one memory access the
14036 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000014037 if (VT.getSizeInBits() == 256 &&
14038 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
14039 StoredVal.getNumOperands() == 2) {
14040
14041 SDValue Value0 = StoredVal.getOperand(0);
14042 SDValue Value1 = StoredVal.getOperand(1);
14043
14044 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
14045 SDValue Ptr0 = St->getBasePtr();
14046 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
14047
14048 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
14049 St->getPointerInfo(), St->isVolatile(),
14050 St->isNonTemporal(), St->getAlignment());
14051 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
14052 St->getPointerInfo(), St->isVolatile(),
14053 St->isNonTemporal(), St->getAlignment());
14054 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
14055 }
Nadav Rotem614061b2011-08-10 19:30:14 +000014056
14057 // Optimize trunc store (of multiple scalars) to shuffle and store.
14058 // First, pack all of the elements in one place. Next, store to memory
14059 // in fewer chunks.
14060 if (St->isTruncatingStore() && VT.isVector()) {
14061 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14062 unsigned NumElems = VT.getVectorNumElements();
14063 assert(StVT != VT && "Cannot truncate to the same type");
14064 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
14065 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
14066
14067 // From, To sizes and ElemCount must be pow of two
14068 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014069 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000014070 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014071 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014072
Nadav Rotem614061b2011-08-10 19:30:14 +000014073 unsigned SizeRatio = FromSz / ToSz;
14074
14075 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
14076
14077 // Create a type on which we perform the shuffle
14078 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
14079 StVT.getScalarType(), NumElems*SizeRatio);
14080
14081 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
14082
14083 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
14084 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
14085 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
14086
14087 // Can't shuffle using an illegal type
14088 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
14089
14090 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
14091 DAG.getUNDEF(WideVec.getValueType()),
14092 ShuffleVec.data());
14093 // At this point all of the data is stored at the bottom of the
14094 // register. We now need to save it to mem.
14095
14096 // Find the largest store unit
14097 MVT StoreType = MVT::i8;
14098 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14099 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14100 MVT Tp = (MVT::SimpleValueType)tp;
14101 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
14102 StoreType = Tp;
14103 }
14104
14105 // Bitcast the original vector into a vector of store-size units
14106 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
14107 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
14108 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
14109 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
14110 SmallVector<SDValue, 8> Chains;
14111 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
14112 TLI.getPointerTy());
14113 SDValue Ptr = St->getBasePtr();
14114
14115 // Perform one or more big stores into memory.
14116 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
14117 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
14118 StoreType, ShuffWide,
14119 DAG.getIntPtrConstant(i));
14120 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
14121 St->getPointerInfo(), St->isVolatile(),
14122 St->isNonTemporal(), St->getAlignment());
14123 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
14124 Chains.push_back(Ch);
14125 }
14126
14127 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
14128 Chains.size());
14129 }
14130
14131
Chris Lattner149a4e52008-02-22 02:09:43 +000014132 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
14133 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000014134 // A preferable solution to the general problem is to figure out the right
14135 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000014136
14137 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000014138 if (VT.getSizeInBits() != 64)
14139 return SDValue();
14140
Devang Patel578efa92009-06-05 21:57:13 +000014141 const Function *F = DAG.getMachineFunction().getFunction();
14142 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000014143 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000014144 && Subtarget->hasXMMInt();
Evan Cheng536e6672009-03-12 05:59:15 +000014145 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000014146 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000014147 isa<LoadSDNode>(St->getValue()) &&
14148 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
14149 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014150 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014151 LoadSDNode *Ld = 0;
14152 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000014153 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000014154 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014155 // Must be a store of a load. We currently handle two cases: the load
14156 // is a direct child, and it's under an intervening TokenFactor. It is
14157 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000014158 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000014159 Ld = cast<LoadSDNode>(St->getChain());
14160 else if (St->getValue().hasOneUse() &&
14161 ChainVal->getOpcode() == ISD::TokenFactor) {
14162 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014163 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000014164 TokenFactorIndex = i;
14165 Ld = cast<LoadSDNode>(St->getValue());
14166 } else
14167 Ops.push_back(ChainVal->getOperand(i));
14168 }
14169 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000014170
Evan Cheng536e6672009-03-12 05:59:15 +000014171 if (!Ld || !ISD::isNormalLoad(Ld))
14172 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014173
Evan Cheng536e6672009-03-12 05:59:15 +000014174 // If this is not the MMX case, i.e. we are just turning i64 load/store
14175 // into f64 load/store, avoid the transformation if there are multiple
14176 // uses of the loaded value.
14177 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
14178 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014179
Evan Cheng536e6672009-03-12 05:59:15 +000014180 DebugLoc LdDL = Ld->getDebugLoc();
14181 DebugLoc StDL = N->getDebugLoc();
14182 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
14183 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
14184 // pair instead.
14185 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014186 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000014187 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
14188 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014189 Ld->isNonTemporal(), Ld->isInvariant(),
14190 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014191 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000014192 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000014193 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000014194 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000014195 Ops.size());
14196 }
Evan Cheng536e6672009-03-12 05:59:15 +000014197 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014198 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014199 St->isVolatile(), St->isNonTemporal(),
14200 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000014201 }
Evan Cheng536e6672009-03-12 05:59:15 +000014202
14203 // Otherwise, lower to two pairs of 32-bit loads / stores.
14204 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014205 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
14206 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014207
Owen Anderson825b72b2009-08-11 20:47:22 +000014208 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014209 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014210 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014211 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000014212 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014213 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000014214 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014215 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000014216 MinAlign(Ld->getAlignment(), 4));
14217
14218 SDValue NewChain = LoLd.getValue(1);
14219 if (TokenFactorIndex != -1) {
14220 Ops.push_back(LoLd);
14221 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000014222 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000014223 Ops.size());
14224 }
14225
14226 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014227 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
14228 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014229
14230 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014231 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014232 St->isVolatile(), St->isNonTemporal(),
14233 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014234 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014235 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000014236 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000014237 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000014238 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000014239 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000014240 }
Dan Gohman475871a2008-07-27 21:46:04 +000014241 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000014242}
14243
Duncan Sands17470be2011-09-22 20:15:48 +000014244/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
14245/// and return the operands for the horizontal operation in LHS and RHS. A
14246/// horizontal operation performs the binary operation on successive elements
14247/// of its first operand, then on successive elements of its second operand,
14248/// returning the resulting values in a vector. For example, if
14249/// A = < float a0, float a1, float a2, float a3 >
14250/// and
14251/// B = < float b0, float b1, float b2, float b3 >
14252/// then the result of doing a horizontal operation on A and B is
14253/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
14254/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
14255/// A horizontal-op B, for some already available A and B, and if so then LHS is
14256/// set to A, RHS to B, and the routine returns 'true'.
14257/// Note that the binary operation should have the property that if one of the
14258/// operands is UNDEF then the result is UNDEF.
14259static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool isCommutative) {
14260 // Look for the following pattern: if
14261 // A = < float a0, float a1, float a2, float a3 >
14262 // B = < float b0, float b1, float b2, float b3 >
14263 // and
14264 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
14265 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
14266 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
14267 // which is A horizontal-op B.
14268
14269 // At least one of the operands should be a vector shuffle.
14270 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
14271 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
14272 return false;
14273
14274 EVT VT = LHS.getValueType();
14275 unsigned N = VT.getVectorNumElements();
14276
14277 // View LHS in the form
14278 // LHS = VECTOR_SHUFFLE A, B, LMask
14279 // If LHS is not a shuffle then pretend it is the shuffle
14280 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
14281 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
14282 // type VT.
14283 SDValue A, B;
14284 SmallVector<int, 8> LMask(N);
14285 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14286 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
14287 A = LHS.getOperand(0);
14288 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
14289 B = LHS.getOperand(1);
14290 cast<ShuffleVectorSDNode>(LHS.getNode())->getMask(LMask);
14291 } else {
14292 if (LHS.getOpcode() != ISD::UNDEF)
14293 A = LHS;
14294 for (unsigned i = 0; i != N; ++i)
14295 LMask[i] = i;
14296 }
14297
14298 // Likewise, view RHS in the form
14299 // RHS = VECTOR_SHUFFLE C, D, RMask
14300 SDValue C, D;
14301 SmallVector<int, 8> RMask(N);
14302 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14303 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
14304 C = RHS.getOperand(0);
14305 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
14306 D = RHS.getOperand(1);
14307 cast<ShuffleVectorSDNode>(RHS.getNode())->getMask(RMask);
14308 } else {
14309 if (RHS.getOpcode() != ISD::UNDEF)
14310 C = RHS;
14311 for (unsigned i = 0; i != N; ++i)
14312 RMask[i] = i;
14313 }
14314
14315 // Check that the shuffles are both shuffling the same vectors.
14316 if (!(A == C && B == D) && !(A == D && B == C))
14317 return false;
14318
14319 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
14320 if (!A.getNode() && !B.getNode())
14321 return false;
14322
14323 // If A and B occur in reverse order in RHS, then "swap" them (which means
14324 // rewriting the mask).
14325 if (A != C)
14326 for (unsigned i = 0; i != N; ++i) {
14327 unsigned Idx = RMask[i];
14328 if (Idx < N)
14329 RMask[i] += N;
14330 else if (Idx < 2*N)
14331 RMask[i] -= N;
14332 }
14333
14334 // At this point LHS and RHS are equivalent to
14335 // LHS = VECTOR_SHUFFLE A, B, LMask
14336 // RHS = VECTOR_SHUFFLE A, B, RMask
14337 // Check that the masks correspond to performing a horizontal operation.
14338 for (unsigned i = 0; i != N; ++i) {
14339 unsigned LIdx = LMask[i], RIdx = RMask[i];
14340
14341 // Ignore any UNDEF components.
14342 if (LIdx >= 2*N || RIdx >= 2*N || (!A.getNode() && (LIdx < N || RIdx < N))
14343 || (!B.getNode() && (LIdx >= N || RIdx >= N)))
14344 continue;
14345
14346 // Check that successive elements are being operated on. If not, this is
14347 // not a horizontal operation.
14348 if (!(LIdx == 2*i && RIdx == 2*i + 1) &&
14349 !(isCommutative && LIdx == 2*i + 1 && RIdx == 2*i))
14350 return false;
14351 }
14352
14353 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
14354 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
14355 return true;
14356}
14357
14358/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
14359static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
14360 const X86Subtarget *Subtarget) {
14361 EVT VT = N->getValueType(0);
14362 SDValue LHS = N->getOperand(0);
14363 SDValue RHS = N->getOperand(1);
14364
14365 // Try to synthesize horizontal adds from adds of shuffles.
14366 if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
14367 (VT == MVT::v4f32 || VT == MVT::v2f64) &&
14368 isHorizontalBinOp(LHS, RHS, true))
14369 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
14370 return SDValue();
14371}
14372
14373/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
14374static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
14375 const X86Subtarget *Subtarget) {
14376 EVT VT = N->getValueType(0);
14377 SDValue LHS = N->getOperand(0);
14378 SDValue RHS = N->getOperand(1);
14379
14380 // Try to synthesize horizontal subs from subs of shuffles.
14381 if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
14382 (VT == MVT::v4f32 || VT == MVT::v2f64) &&
14383 isHorizontalBinOp(LHS, RHS, false))
14384 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
14385 return SDValue();
14386}
14387
Chris Lattner6cf73262008-01-25 06:14:17 +000014388/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
14389/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014390static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000014391 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
14392 // F[X]OR(0.0, x) -> x
14393 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000014394 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14395 if (C->getValueAPF().isPosZero())
14396 return N->getOperand(1);
14397 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14398 if (C->getValueAPF().isPosZero())
14399 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000014400 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014401}
14402
14403/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014404static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000014405 // FAND(0.0, x) -> 0.0
14406 // FAND(x, 0.0) -> 0.0
14407 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14408 if (C->getValueAPF().isPosZero())
14409 return N->getOperand(0);
14410 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14411 if (C->getValueAPF().isPosZero())
14412 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000014413 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014414}
14415
Dan Gohmane5af2d32009-01-29 01:59:02 +000014416static SDValue PerformBTCombine(SDNode *N,
14417 SelectionDAG &DAG,
14418 TargetLowering::DAGCombinerInfo &DCI) {
14419 // BT ignores high bits in the bit index operand.
14420 SDValue Op1 = N->getOperand(1);
14421 if (Op1.hasOneUse()) {
14422 unsigned BitWidth = Op1.getValueSizeInBits();
14423 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
14424 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014425 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
14426 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000014427 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000014428 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
14429 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
14430 DCI.CommitTargetLoweringOpt(TLO);
14431 }
14432 return SDValue();
14433}
Chris Lattner83e6c992006-10-04 06:57:07 +000014434
Eli Friedman7a5e5552009-06-07 06:52:44 +000014435static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
14436 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014437 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000014438 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000014439 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000014440 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000014441 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000014442 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014443 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014444 }
14445 return SDValue();
14446}
14447
Evan Cheng2e489c42009-12-16 00:53:11 +000014448static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
14449 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
14450 // (and (i32 x86isd::setcc_carry), 1)
14451 // This eliminates the zext. This transformation is necessary because
14452 // ISD::SETCC is always legalized to i8.
14453 DebugLoc dl = N->getDebugLoc();
14454 SDValue N0 = N->getOperand(0);
14455 EVT VT = N->getValueType(0);
14456 if (N0.getOpcode() == ISD::AND &&
14457 N0.hasOneUse() &&
14458 N0.getOperand(0).hasOneUse()) {
14459 SDValue N00 = N0.getOperand(0);
14460 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
14461 return SDValue();
14462 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
14463 if (!C || C->getZExtValue() != 1)
14464 return SDValue();
14465 return DAG.getNode(ISD::AND, dl, VT,
14466 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
14467 N00.getOperand(0), N00.getOperand(1)),
14468 DAG.getConstant(1, VT));
14469 }
14470
14471 return SDValue();
14472}
14473
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014474// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
14475static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
14476 unsigned X86CC = N->getConstantOperandVal(0);
14477 SDValue EFLAG = N->getOperand(1);
14478 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014479
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014480 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
14481 // a zext and produces an all-ones bit which is more useful than 0/1 in some
14482 // cases.
14483 if (X86CC == X86::COND_B)
14484 return DAG.getNode(ISD::AND, DL, MVT::i8,
14485 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
14486 DAG.getConstant(X86CC, MVT::i8), EFLAG),
14487 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014488
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014489 return SDValue();
14490}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014491
Benjamin Kramer1396c402011-06-18 11:09:41 +000014492static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
14493 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014494 SDValue Op0 = N->getOperand(0);
14495 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
14496 // a 32-bit target where SSE doesn't support i64->FP operations.
14497 if (Op0.getOpcode() == ISD::LOAD) {
14498 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
14499 EVT VT = Ld->getValueType(0);
14500 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
14501 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
14502 !XTLI->getSubtarget()->is64Bit() &&
14503 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000014504 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
14505 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014506 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
14507 return FILDChain;
14508 }
14509 }
14510 return SDValue();
14511}
14512
Chris Lattner23a01992010-12-20 01:37:09 +000014513// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
14514static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
14515 X86TargetLowering::DAGCombinerInfo &DCI) {
14516 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
14517 // the result is either zero or one (depending on the input carry bit).
14518 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
14519 if (X86::isZeroNode(N->getOperand(0)) &&
14520 X86::isZeroNode(N->getOperand(1)) &&
14521 // We don't have a good way to replace an EFLAGS use, so only do this when
14522 // dead right now.
14523 SDValue(N, 1).use_empty()) {
14524 DebugLoc DL = N->getDebugLoc();
14525 EVT VT = N->getValueType(0);
14526 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
14527 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
14528 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
14529 DAG.getConstant(X86::COND_B,MVT::i8),
14530 N->getOperand(2)),
14531 DAG.getConstant(1, VT));
14532 return DCI.CombineTo(N, Res1, CarryOut);
14533 }
14534
14535 return SDValue();
14536}
14537
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014538// fold (add Y, (sete X, 0)) -> adc 0, Y
14539// (add Y, (setne X, 0)) -> sbb -1, Y
14540// (sub (sete X, 0), Y) -> sbb 0, Y
14541// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014542static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014543 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014544
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014545 // Look through ZExts.
14546 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
14547 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
14548 return SDValue();
14549
14550 SDValue SetCC = Ext.getOperand(0);
14551 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
14552 return SDValue();
14553
14554 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
14555 if (CC != X86::COND_E && CC != X86::COND_NE)
14556 return SDValue();
14557
14558 SDValue Cmp = SetCC.getOperand(1);
14559 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000014560 !X86::isZeroNode(Cmp.getOperand(1)) ||
14561 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014562 return SDValue();
14563
14564 SDValue CmpOp0 = Cmp.getOperand(0);
14565 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
14566 DAG.getConstant(1, CmpOp0.getValueType()));
14567
14568 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
14569 if (CC == X86::COND_NE)
14570 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
14571 DL, OtherVal.getValueType(), OtherVal,
14572 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
14573 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
14574 DL, OtherVal.getValueType(), OtherVal,
14575 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
14576}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014577
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014578static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG) {
14579 SDValue Op0 = N->getOperand(0);
14580 SDValue Op1 = N->getOperand(1);
14581
14582 // X86 can't encode an immediate LHS of a sub. See if we can push the
14583 // negation into a preceding instruction.
14584 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014585 // If the RHS of the sub is a XOR with one use and a constant, invert the
14586 // immediate. Then add one to the LHS of the sub so we can turn
14587 // X-Y -> X+~Y+1, saving one register.
14588 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
14589 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000014590 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014591 EVT VT = Op0.getValueType();
14592 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
14593 Op1.getOperand(0),
14594 DAG.getConstant(~XorC, VT));
14595 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000014596 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014597 }
14598 }
14599
14600 return OptimizeConditionalInDecrement(N, DAG);
14601}
14602
Dan Gohman475871a2008-07-27 21:46:04 +000014603SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000014604 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014605 SelectionDAG &DAG = DCI.DAG;
14606 switch (N->getOpcode()) {
14607 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014608 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014609 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000014610 case ISD::VSELECT:
Chris Lattneraf723b92008-01-25 05:46:26 +000014611 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014612 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014613 case ISD::ADD: return OptimizeConditionalInDecrement(N, DAG);
14614 case ISD::SUB: return PerformSubCombine(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000014615 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000014616 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000014617 case ISD::SHL:
14618 case ISD::SRA:
14619 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000014620 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000014621 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000014622 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014623 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000014624 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014625 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000014626 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
14627 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000014628 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000014629 case X86ISD::FOR: return PerformFORCombine(N, DAG);
14630 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000014631 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014632 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000014633 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014634 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014635 case X86ISD::SHUFPS: // Handle all target specific shuffles
14636 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000014637 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014638 case X86ISD::PUNPCKHBW:
14639 case X86ISD::PUNPCKHWD:
14640 case X86ISD::PUNPCKHDQ:
14641 case X86ISD::PUNPCKHQDQ:
14642 case X86ISD::UNPCKHPS:
14643 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +000014644 case X86ISD::VUNPCKHPSY:
14645 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014646 case X86ISD::PUNPCKLBW:
14647 case X86ISD::PUNPCKLWD:
14648 case X86ISD::PUNPCKLDQ:
14649 case X86ISD::PUNPCKLQDQ:
14650 case X86ISD::UNPCKLPS:
14651 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000014652 case X86ISD::VUNPCKLPSY:
14653 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014654 case X86ISD::MOVHLPS:
14655 case X86ISD::MOVLHPS:
14656 case X86ISD::PSHUFD:
14657 case X86ISD::PSHUFHW:
14658 case X86ISD::PSHUFLW:
14659 case X86ISD::MOVSS:
14660 case X86ISD::MOVSD:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000014661 case X86ISD::VPERMILPS:
14662 case X86ISD::VPERMILPSY:
14663 case X86ISD::VPERMILPD:
14664 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000014665 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014666 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014667 }
14668
Dan Gohman475871a2008-07-27 21:46:04 +000014669 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014670}
14671
Evan Chenge5b51ac2010-04-17 06:13:15 +000014672/// isTypeDesirableForOp - Return true if the target has native support for
14673/// the specified value type and it is 'desirable' to use the type for the
14674/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14675/// instruction encodings are longer and some i16 instructions are slow.
14676bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14677 if (!isTypeLegal(VT))
14678 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014679 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014680 return true;
14681
14682 switch (Opc) {
14683 default:
14684 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014685 case ISD::LOAD:
14686 case ISD::SIGN_EXTEND:
14687 case ISD::ZERO_EXTEND:
14688 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014689 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014690 case ISD::SRL:
14691 case ISD::SUB:
14692 case ISD::ADD:
14693 case ISD::MUL:
14694 case ISD::AND:
14695 case ISD::OR:
14696 case ISD::XOR:
14697 return false;
14698 }
14699}
14700
14701/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014702/// beneficial for dag combiner to promote the specified node. If true, it
14703/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014704bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014705 EVT VT = Op.getValueType();
14706 if (VT != MVT::i16)
14707 return false;
14708
Evan Cheng4c26e932010-04-19 19:29:22 +000014709 bool Promote = false;
14710 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014711 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014712 default: break;
14713 case ISD::LOAD: {
14714 LoadSDNode *LD = cast<LoadSDNode>(Op);
14715 // If the non-extending load has a single use and it's not live out, then it
14716 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014717 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14718 Op.hasOneUse()*/) {
14719 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14720 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14721 // The only case where we'd want to promote LOAD (rather then it being
14722 // promoted as an operand is when it's only use is liveout.
14723 if (UI->getOpcode() != ISD::CopyToReg)
14724 return false;
14725 }
14726 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014727 Promote = true;
14728 break;
14729 }
14730 case ISD::SIGN_EXTEND:
14731 case ISD::ZERO_EXTEND:
14732 case ISD::ANY_EXTEND:
14733 Promote = true;
14734 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014735 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014736 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014737 SDValue N0 = Op.getOperand(0);
14738 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014739 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014740 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014741 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014742 break;
14743 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014744 case ISD::ADD:
14745 case ISD::MUL:
14746 case ISD::AND:
14747 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014748 case ISD::XOR:
14749 Commute = true;
14750 // fallthrough
14751 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014752 SDValue N0 = Op.getOperand(0);
14753 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014754 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014755 return false;
14756 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014757 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014758 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014759 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014760 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014761 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014762 }
14763 }
14764
14765 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014766 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014767}
14768
Evan Cheng60c07e12006-07-05 22:17:51 +000014769//===----------------------------------------------------------------------===//
14770// X86 Inline Assembly Support
14771//===----------------------------------------------------------------------===//
14772
Chris Lattnerb8105652009-07-20 17:51:36 +000014773bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14774 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014775
14776 std::string AsmStr = IA->getAsmString();
14777
14778 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014779 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014780 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014781
14782 switch (AsmPieces.size()) {
14783 default: return false;
14784 case 1:
14785 AsmStr = AsmPieces[0];
14786 AsmPieces.clear();
14787 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
14788
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014789 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000014790 // we will turn this bswap into something that will be lowered to logical ops
14791 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14792 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014793 // bswap $0
14794 if (AsmPieces.size() == 2 &&
14795 (AsmPieces[0] == "bswap" ||
14796 AsmPieces[0] == "bswapq" ||
14797 AsmPieces[0] == "bswapl") &&
14798 (AsmPieces[1] == "$0" ||
14799 AsmPieces[1] == "${0:q}")) {
14800 // No need to check constraints, nothing other than the equivalent of
14801 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014802 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014803 if (!Ty || Ty->getBitWidth() % 16 != 0)
14804 return false;
14805 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014806 }
14807 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014808 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014809 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014810 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014811 AsmPieces[1] == "$$8," &&
14812 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014813 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14814 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014815 const std::string &ConstraintsStr = IA->getConstraintString();
14816 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014817 std::sort(AsmPieces.begin(), AsmPieces.end());
14818 if (AsmPieces.size() == 4 &&
14819 AsmPieces[0] == "~{cc}" &&
14820 AsmPieces[1] == "~{dirflag}" &&
14821 AsmPieces[2] == "~{flags}" &&
14822 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014823 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014824 if (!Ty || Ty->getBitWidth() % 16 != 0)
14825 return false;
14826 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014827 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014828 }
14829 break;
14830 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014831 if (CI->getType()->isIntegerTy(32) &&
14832 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14833 SmallVector<StringRef, 4> Words;
14834 SplitString(AsmPieces[0], Words, " \t,");
14835 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14836 Words[2] == "${0:w}") {
14837 Words.clear();
14838 SplitString(AsmPieces[1], Words, " \t,");
14839 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14840 Words[2] == "$0") {
14841 Words.clear();
14842 SplitString(AsmPieces[2], Words, " \t,");
14843 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14844 Words[2] == "${0:w}") {
14845 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014846 const std::string &ConstraintsStr = IA->getConstraintString();
14847 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014848 std::sort(AsmPieces.begin(), AsmPieces.end());
14849 if (AsmPieces.size() == 4 &&
14850 AsmPieces[0] == "~{cc}" &&
14851 AsmPieces[1] == "~{dirflag}" &&
14852 AsmPieces[2] == "~{flags}" &&
14853 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014854 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014855 if (!Ty || Ty->getBitWidth() % 16 != 0)
14856 return false;
14857 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014858 }
14859 }
14860 }
14861 }
14862 }
Evan Cheng55d42002011-01-08 01:24:27 +000014863
14864 if (CI->getType()->isIntegerTy(64)) {
14865 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14866 if (Constraints.size() >= 2 &&
14867 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14868 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14869 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14870 SmallVector<StringRef, 4> Words;
14871 SplitString(AsmPieces[0], Words, " \t");
14872 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014873 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014874 SplitString(AsmPieces[1], Words, " \t");
14875 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14876 Words.clear();
14877 SplitString(AsmPieces[2], Words, " \t,");
14878 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14879 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014880 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014881 if (!Ty || Ty->getBitWidth() % 16 != 0)
14882 return false;
14883 return IntrinsicLowering::LowerToByteSwap(CI);
14884 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014885 }
14886 }
14887 }
14888 }
14889 break;
14890 }
14891 return false;
14892}
14893
14894
14895
Chris Lattnerf4dff842006-07-11 02:54:03 +000014896/// getConstraintType - Given a constraint letter, return the type of
14897/// constraint it is for this target.
14898X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014899X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14900 if (Constraint.size() == 1) {
14901 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014902 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014903 case 'q':
14904 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014905 case 'f':
14906 case 't':
14907 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014908 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014909 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014910 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014911 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014912 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014913 case 'a':
14914 case 'b':
14915 case 'c':
14916 case 'd':
14917 case 'S':
14918 case 'D':
14919 case 'A':
14920 return C_Register;
14921 case 'I':
14922 case 'J':
14923 case 'K':
14924 case 'L':
14925 case 'M':
14926 case 'N':
14927 case 'G':
14928 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014929 case 'e':
14930 case 'Z':
14931 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014932 default:
14933 break;
14934 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014935 }
Chris Lattner4234f572007-03-25 02:14:49 +000014936 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014937}
14938
John Thompson44ab89e2010-10-29 17:29:13 +000014939/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014940/// This object must already have been set up with the operand type
14941/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014942TargetLowering::ConstraintWeight
14943 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014944 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014945 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014946 Value *CallOperandVal = info.CallOperandVal;
14947 // If we don't have a value, we can't do a match,
14948 // but allow it at the lowest weight.
14949 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014950 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014951 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014952 // Look at the constraint type.
14953 switch (*constraint) {
14954 default:
John Thompson44ab89e2010-10-29 17:29:13 +000014955 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
14956 case 'R':
14957 case 'q':
14958 case 'Q':
14959 case 'a':
14960 case 'b':
14961 case 'c':
14962 case 'd':
14963 case 'S':
14964 case 'D':
14965 case 'A':
14966 if (CallOperandVal->getType()->isIntegerTy())
14967 weight = CW_SpecificReg;
14968 break;
14969 case 'f':
14970 case 't':
14971 case 'u':
14972 if (type->isFloatingPointTy())
14973 weight = CW_SpecificReg;
14974 break;
14975 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000014976 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000014977 weight = CW_SpecificReg;
14978 break;
14979 case 'x':
14980 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014981 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000014982 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014983 break;
14984 case 'I':
14985 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
14986 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000014987 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014988 }
14989 break;
John Thompson44ab89e2010-10-29 17:29:13 +000014990 case 'J':
14991 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14992 if (C->getZExtValue() <= 63)
14993 weight = CW_Constant;
14994 }
14995 break;
14996 case 'K':
14997 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14998 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
14999 weight = CW_Constant;
15000 }
15001 break;
15002 case 'L':
15003 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15004 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
15005 weight = CW_Constant;
15006 }
15007 break;
15008 case 'M':
15009 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15010 if (C->getZExtValue() <= 3)
15011 weight = CW_Constant;
15012 }
15013 break;
15014 case 'N':
15015 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15016 if (C->getZExtValue() <= 0xff)
15017 weight = CW_Constant;
15018 }
15019 break;
15020 case 'G':
15021 case 'C':
15022 if (dyn_cast<ConstantFP>(CallOperandVal)) {
15023 weight = CW_Constant;
15024 }
15025 break;
15026 case 'e':
15027 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15028 if ((C->getSExtValue() >= -0x80000000LL) &&
15029 (C->getSExtValue() <= 0x7fffffffLL))
15030 weight = CW_Constant;
15031 }
15032 break;
15033 case 'Z':
15034 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15035 if (C->getZExtValue() <= 0xffffffff)
15036 weight = CW_Constant;
15037 }
15038 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015039 }
15040 return weight;
15041}
15042
Dale Johannesenba2a0b92008-01-29 02:21:21 +000015043/// LowerXConstraint - try to replace an X constraint, which matches anything,
15044/// with another that has more specific requirements based on the type of the
15045/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000015046const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000015047LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000015048 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
15049 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000015050 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015051 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000015052 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015053 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000015054 return "x";
15055 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015056
Chris Lattner5e764232008-04-26 23:02:14 +000015057 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000015058}
15059
Chris Lattner48884cd2007-08-25 00:47:38 +000015060/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
15061/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000015062void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000015063 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000015064 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000015065 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000015066 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000015067
Eric Christopher100c8332011-06-02 23:16:42 +000015068 // Only support length 1 constraints for now.
15069 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000015070
Eric Christopher100c8332011-06-02 23:16:42 +000015071 char ConstraintLetter = Constraint[0];
15072 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015073 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000015074 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000015075 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015076 if (C->getZExtValue() <= 31) {
15077 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015078 break;
15079 }
Devang Patel84f7fd22007-03-17 00:13:28 +000015080 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015081 return;
Evan Cheng364091e2008-09-22 23:57:37 +000015082 case 'J':
15083 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015084 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000015085 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15086 break;
15087 }
15088 }
15089 return;
15090 case 'K':
15091 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015092 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000015093 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15094 break;
15095 }
15096 }
15097 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000015098 case 'N':
15099 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015100 if (C->getZExtValue() <= 255) {
15101 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015102 break;
15103 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000015104 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015105 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000015106 case 'e': {
15107 // 32-bit signed value
15108 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015109 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15110 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015111 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015112 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000015113 break;
15114 }
15115 // FIXME gcc accepts some relocatable values here too, but only in certain
15116 // memory models; it's complicated.
15117 }
15118 return;
15119 }
15120 case 'Z': {
15121 // 32-bit unsigned value
15122 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015123 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15124 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015125 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15126 break;
15127 }
15128 }
15129 // FIXME gcc accepts some relocatable values here too, but only in certain
15130 // memory models; it's complicated.
15131 return;
15132 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015133 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015134 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000015135 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015136 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015137 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000015138 break;
15139 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015140
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015141 // In any sort of PIC mode addresses need to be computed at runtime by
15142 // adding in a register or some sort of table lookup. These can't
15143 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000015144 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015145 return;
15146
Chris Lattnerdc43a882007-05-03 16:52:29 +000015147 // If we are in non-pic codegen mode, we allow the address of a global (with
15148 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000015149 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015150 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000015151
Chris Lattner49921962009-05-08 18:23:14 +000015152 // Match either (GA), (GA+C), (GA+C1+C2), etc.
15153 while (1) {
15154 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
15155 Offset += GA->getOffset();
15156 break;
15157 } else if (Op.getOpcode() == ISD::ADD) {
15158 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15159 Offset += C->getZExtValue();
15160 Op = Op.getOperand(0);
15161 continue;
15162 }
15163 } else if (Op.getOpcode() == ISD::SUB) {
15164 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15165 Offset += -C->getZExtValue();
15166 Op = Op.getOperand(0);
15167 continue;
15168 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015169 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015170
Chris Lattner49921962009-05-08 18:23:14 +000015171 // Otherwise, this isn't something we can handle, reject it.
15172 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015173 }
Eric Christopherfd179292009-08-27 18:07:15 +000015174
Dan Gohman46510a72010-04-15 01:51:59 +000015175 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015176 // If we require an extra load to get this address, as in PIC mode, we
15177 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000015178 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
15179 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015180 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000015181
Devang Patel0d881da2010-07-06 22:08:15 +000015182 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
15183 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000015184 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015185 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015186 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015187
Gabor Greifba36cb52008-08-28 21:40:38 +000015188 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000015189 Ops.push_back(Result);
15190 return;
15191 }
Dale Johannesen1784d162010-06-25 21:55:36 +000015192 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015193}
15194
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015195std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000015196X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000015197 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000015198 // First, see if this is a constraint that directly corresponds to an LLVM
15199 // register class.
15200 if (Constraint.size() == 1) {
15201 // GCC Constraint Letters
15202 switch (Constraint[0]) {
15203 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000015204 // TODO: Slight differences here in allocation order and leaving
15205 // RIP in the class. Do they matter any more here than they do
15206 // in the normal allocation?
15207 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
15208 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015209 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015210 return std::make_pair(0U, X86::GR32RegisterClass);
15211 else if (VT == MVT::i16)
15212 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015213 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015214 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015215 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000015216 return std::make_pair(0U, X86::GR64RegisterClass);
15217 break;
15218 }
15219 // 32-bit fallthrough
15220 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015221 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015222 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
15223 else if (VT == MVT::i16)
15224 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015225 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015226 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
15227 else if (VT == MVT::i64)
15228 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
15229 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015230 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000015231 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015232 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000015233 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015234 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000015235 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000015236 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000015237 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000015238 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015239 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015240 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015241 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
15242 if (VT == MVT::i16)
15243 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
15244 if (VT == MVT::i32 || !Subtarget->is64Bit())
15245 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
15246 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015247 case 'f': // FP Stack registers.
15248 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
15249 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000015250 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015251 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015252 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015253 return std::make_pair(0U, X86::RFP64RegisterClass);
15254 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000015255 case 'y': // MMX_REGS if MMX allowed.
15256 if (!Subtarget->hasMMX()) break;
15257 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015258 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015259 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015260 // FALL THROUGH.
15261 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015262 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000015263
Owen Anderson825b72b2009-08-11 20:47:22 +000015264 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000015265 default: break;
15266 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015267 case MVT::f32:
15268 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000015269 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015270 case MVT::f64:
15271 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000015272 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015273 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015274 case MVT::v16i8:
15275 case MVT::v8i16:
15276 case MVT::v4i32:
15277 case MVT::v2i64:
15278 case MVT::v4f32:
15279 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000015280 return std::make_pair(0U, X86::VR128RegisterClass);
15281 }
Chris Lattnerad043e82007-04-09 05:11:28 +000015282 break;
15283 }
15284 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015285
Chris Lattnerf76d1802006-07-31 23:26:50 +000015286 // Use the default implementation in TargetLowering to convert the register
15287 // constraint into a member of a register class.
15288 std::pair<unsigned, const TargetRegisterClass*> Res;
15289 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000015290
15291 // Not found as a standard register?
15292 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015293 // Map st(0) -> st(7) -> ST0
15294 if (Constraint.size() == 7 && Constraint[0] == '{' &&
15295 tolower(Constraint[1]) == 's' &&
15296 tolower(Constraint[2]) == 't' &&
15297 Constraint[3] == '(' &&
15298 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
15299 Constraint[5] == ')' &&
15300 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000015301
Chris Lattner56d77c72009-09-13 22:41:48 +000015302 Res.first = X86::ST0+Constraint[4]-'0';
15303 Res.second = X86::RFP80RegisterClass;
15304 return Res;
15305 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015306
Chris Lattner56d77c72009-09-13 22:41:48 +000015307 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015308 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000015309 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000015310 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015311 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000015312 }
Chris Lattner56d77c72009-09-13 22:41:48 +000015313
15314 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015315 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015316 Res.first = X86::EFLAGS;
15317 Res.second = X86::CCRRegisterClass;
15318 return Res;
15319 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015320
Dale Johannesen330169f2008-11-13 21:52:36 +000015321 // 'A' means EAX + EDX.
15322 if (Constraint == "A") {
15323 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000015324 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015325 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000015326 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000015327 return Res;
15328 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015329
Chris Lattnerf76d1802006-07-31 23:26:50 +000015330 // Otherwise, check to see if this is a register class of the wrong value
15331 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
15332 // turn into {ax},{dx}.
15333 if (Res.second->hasType(VT))
15334 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015335
Chris Lattnerf76d1802006-07-31 23:26:50 +000015336 // All of the single-register GCC register classes map their values onto
15337 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
15338 // really want an 8-bit or 32-bit register, map to the appropriate register
15339 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000015340 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015341 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015342 unsigned DestReg = 0;
15343 switch (Res.first) {
15344 default: break;
15345 case X86::AX: DestReg = X86::AL; break;
15346 case X86::DX: DestReg = X86::DL; break;
15347 case X86::CX: DestReg = X86::CL; break;
15348 case X86::BX: DestReg = X86::BL; break;
15349 }
15350 if (DestReg) {
15351 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015352 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015353 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015354 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015355 unsigned DestReg = 0;
15356 switch (Res.first) {
15357 default: break;
15358 case X86::AX: DestReg = X86::EAX; break;
15359 case X86::DX: DestReg = X86::EDX; break;
15360 case X86::CX: DestReg = X86::ECX; break;
15361 case X86::BX: DestReg = X86::EBX; break;
15362 case X86::SI: DestReg = X86::ESI; break;
15363 case X86::DI: DestReg = X86::EDI; break;
15364 case X86::BP: DestReg = X86::EBP; break;
15365 case X86::SP: DestReg = X86::ESP; break;
15366 }
15367 if (DestReg) {
15368 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015369 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015370 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015371 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015372 unsigned DestReg = 0;
15373 switch (Res.first) {
15374 default: break;
15375 case X86::AX: DestReg = X86::RAX; break;
15376 case X86::DX: DestReg = X86::RDX; break;
15377 case X86::CX: DestReg = X86::RCX; break;
15378 case X86::BX: DestReg = X86::RBX; break;
15379 case X86::SI: DestReg = X86::RSI; break;
15380 case X86::DI: DestReg = X86::RDI; break;
15381 case X86::BP: DestReg = X86::RBP; break;
15382 case X86::SP: DestReg = X86::RSP; break;
15383 }
15384 if (DestReg) {
15385 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015386 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015387 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000015388 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000015389 } else if (Res.second == X86::FR32RegisterClass ||
15390 Res.second == X86::FR64RegisterClass ||
15391 Res.second == X86::VR128RegisterClass) {
15392 // Handle references to XMM physical registers that got mapped into the
15393 // wrong class. This can happen with constraints like {xmm0} where the
15394 // target independent register mapper will just pick the first match it can
15395 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000015396 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015397 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000015398 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015399 Res.second = X86::FR64RegisterClass;
15400 else if (X86::VR128RegisterClass->hasType(VT))
15401 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000015402 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015403
Chris Lattnerf76d1802006-07-31 23:26:50 +000015404 return Res;
15405}