blob: aab7c73b67dcc336236f714922f31ebb2d1817fb [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
Nadav Rotemfbad25e2011-09-11 15:02:23 +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 Topper13894fa2011-08-24 06:14:18 +00001039 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1040 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1041 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1042 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1043
1044 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1045 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1046 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1047 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1048
1049 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1050 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1051 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1052 // Don't lower v32i8 because there is no 128-bit byte mul
1053
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001054 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001055 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001056 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1057 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1058 EVT VT = SVT;
1059
1060 // Extract subvector is special because the value type
1061 // (result) is 128-bit but the source is 256-bit wide.
1062 if (VT.is128BitVector())
1063 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1064
1065 // Do not attempt to custom lower other non-256-bit vectors
1066 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001067 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001068
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001069 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1070 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1071 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1072 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001073 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001074 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001075 }
1076
David Greene54d8eba2011-01-27 22:38:56 +00001077 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001078 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1079 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1080 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001081
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001082 // Do not attempt to promote non-256-bit vectors
1083 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001084 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001085
1086 setOperationAction(ISD::AND, SVT, Promote);
1087 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1088 setOperationAction(ISD::OR, SVT, Promote);
1089 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1090 setOperationAction(ISD::XOR, SVT, Promote);
1091 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1092 setOperationAction(ISD::LOAD, SVT, Promote);
1093 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1094 setOperationAction(ISD::SELECT, SVT, Promote);
1095 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001096 }
David Greene9b9838d2009-06-29 16:47:10 +00001097 }
1098
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001099 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1100 // of this type with custom code.
1101 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1102 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1103 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1104 }
1105
Evan Cheng6be2c582006-04-05 23:38:46 +00001106 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001107 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001108
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001109
Eli Friedman962f5492010-06-02 19:35:46 +00001110 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1111 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001112 //
Eli Friedman962f5492010-06-02 19:35:46 +00001113 // FIXME: We really should do custom legalization for addition and
1114 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1115 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001116 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1117 // Add/Sub/Mul with overflow operations are custom lowered.
1118 MVT VT = IntVTs[i];
1119 setOperationAction(ISD::SADDO, VT, Custom);
1120 setOperationAction(ISD::UADDO, VT, Custom);
1121 setOperationAction(ISD::SSUBO, VT, Custom);
1122 setOperationAction(ISD::USUBO, VT, Custom);
1123 setOperationAction(ISD::SMULO, VT, Custom);
1124 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001125 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001126
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001127 // There are no 8-bit 3-address imul/mul instructions
1128 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1129 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001130
Evan Chengd54f2d52009-03-31 19:38:51 +00001131 if (!Subtarget->is64Bit()) {
1132 // These libcalls are not available in 32-bit.
1133 setLibcallName(RTLIB::SHL_I128, 0);
1134 setLibcallName(RTLIB::SRL_I128, 0);
1135 setLibcallName(RTLIB::SRA_I128, 0);
1136 }
1137
Evan Cheng206ee9d2006-07-07 08:33:52 +00001138 // We have target-specific dag combine patterns for the following nodes:
1139 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001140 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001141 setTargetDAGCombine(ISD::BUILD_VECTOR);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001142 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001143 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001144 setTargetDAGCombine(ISD::SHL);
1145 setTargetDAGCombine(ISD::SRA);
1146 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001147 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001148 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001149 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001150 setTargetDAGCombine(ISD::FADD);
1151 setTargetDAGCombine(ISD::FSUB);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001152 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001153 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001154 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001155 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001156 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001157 if (Subtarget->is64Bit())
1158 setTargetDAGCombine(ISD::MUL);
Craig Topperb4c94572011-10-21 06:55:01 +00001159 if (Subtarget->hasBMI())
1160 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001161
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001162 computeRegisterProperties();
1163
Evan Cheng05219282011-01-06 06:52:41 +00001164 // On Darwin, -Os means optimize for size without hurting performance,
1165 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001166 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001167 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001168 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001169 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1170 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1171 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001172 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001173 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001174
1175 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001176}
1177
Scott Michel5b8f82e2008-03-10 15:42:14 +00001178
Duncan Sands28b77e92011-09-06 19:07:46 +00001179EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1180 if (!VT.isVector()) return MVT::i8;
1181 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001182}
1183
1184
Evan Cheng29286502008-01-23 23:17:41 +00001185/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1186/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001187static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001188 if (MaxAlign == 16)
1189 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001190 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001191 if (VTy->getBitWidth() == 128)
1192 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001193 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001194 unsigned EltAlign = 0;
1195 getMaxByValAlign(ATy->getElementType(), EltAlign);
1196 if (EltAlign > MaxAlign)
1197 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001198 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001199 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1200 unsigned EltAlign = 0;
1201 getMaxByValAlign(STy->getElementType(i), EltAlign);
1202 if (EltAlign > MaxAlign)
1203 MaxAlign = EltAlign;
1204 if (MaxAlign == 16)
1205 break;
1206 }
1207 }
1208 return;
1209}
1210
1211/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1212/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001213/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1214/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001215unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001216 if (Subtarget->is64Bit()) {
1217 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001218 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001219 if (TyAlign > 8)
1220 return TyAlign;
1221 return 8;
1222 }
1223
Evan Cheng29286502008-01-23 23:17:41 +00001224 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001225 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001226 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001227 return Align;
1228}
Chris Lattner2b02a442007-02-25 08:29:00 +00001229
Evan Chengf0df0312008-05-15 08:39:06 +00001230/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001231/// and store operations as a result of memset, memcpy, and memmove
1232/// lowering. If DstAlign is zero that means it's safe to destination
1233/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1234/// means there isn't a need to check it against alignment requirement,
1235/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001236/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001237/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1238/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1239/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001240/// It returns EVT::Other if the type should be determined using generic
1241/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001242EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001243X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1244 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001245 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001246 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001247 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001248 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1249 // linux. This is because the stack realignment code can't handle certain
1250 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001251 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001252 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001253 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001254 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001255 (Subtarget->isUnalignedMemAccessFast() ||
1256 ((DstAlign == 0 || DstAlign >= 16) &&
1257 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001258 Subtarget->getStackAlignment() >= 16) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001259 if (Subtarget->hasAVX() &&
1260 Subtarget->getStackAlignment() >= 32)
1261 return MVT::v8f32;
1262 if (Subtarget->hasXMMInt())
Evan Cheng255f20f2010-04-01 06:04:33 +00001263 return MVT::v4i32;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001264 if (Subtarget->hasXMM())
Evan Cheng255f20f2010-04-01 06:04:33 +00001265 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001266 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001267 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001268 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001269 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001270 // Do not use f64 to lower memcpy if source is string constant. It's
1271 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001272 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001273 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001274 }
Evan Chengf0df0312008-05-15 08:39:06 +00001275 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001276 return MVT::i64;
1277 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001278}
1279
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001280/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1281/// current function. The returned value is a member of the
1282/// MachineJumpTableInfo::JTEntryKind enum.
1283unsigned X86TargetLowering::getJumpTableEncoding() const {
1284 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1285 // symbol.
1286 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1287 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001288 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001289
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001290 // Otherwise, use the normal jump table encoding heuristics.
1291 return TargetLowering::getJumpTableEncoding();
1292}
1293
Chris Lattnerc64daab2010-01-26 05:02:42 +00001294const MCExpr *
1295X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1296 const MachineBasicBlock *MBB,
1297 unsigned uid,MCContext &Ctx) const{
1298 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1299 Subtarget->isPICStyleGOT());
1300 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1301 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001302 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1303 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001304}
1305
Evan Chengcc415862007-11-09 01:32:10 +00001306/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1307/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001308SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001309 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001310 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001311 // This doesn't have DebugLoc associated with it, but is not really the
1312 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001313 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001314 return Table;
1315}
1316
Chris Lattner589c6f62010-01-26 06:28:43 +00001317/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1318/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1319/// MCExpr.
1320const MCExpr *X86TargetLowering::
1321getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1322 MCContext &Ctx) const {
1323 // X86-64 uses RIP relative addressing based on the jump table label.
1324 if (Subtarget->isPICStyleRIPRel())
1325 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1326
1327 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001328 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001329}
1330
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001331// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001332std::pair<const TargetRegisterClass*, uint8_t>
1333X86TargetLowering::findRepresentativeClass(EVT VT) const{
1334 const TargetRegisterClass *RRC = 0;
1335 uint8_t Cost = 1;
1336 switch (VT.getSimpleVT().SimpleTy) {
1337 default:
1338 return TargetLowering::findRepresentativeClass(VT);
1339 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1340 RRC = (Subtarget->is64Bit()
1341 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1342 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001343 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001344 RRC = X86::VR64RegisterClass;
1345 break;
1346 case MVT::f32: case MVT::f64:
1347 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1348 case MVT::v4f32: case MVT::v2f64:
1349 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1350 case MVT::v4f64:
1351 RRC = X86::VR128RegisterClass;
1352 break;
1353 }
1354 return std::make_pair(RRC, Cost);
1355}
1356
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001357bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1358 unsigned &Offset) const {
1359 if (!Subtarget->isTargetLinux())
1360 return false;
1361
1362 if (Subtarget->is64Bit()) {
1363 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1364 Offset = 0x28;
1365 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1366 AddressSpace = 256;
1367 else
1368 AddressSpace = 257;
1369 } else {
1370 // %gs:0x14 on i386
1371 Offset = 0x14;
1372 AddressSpace = 256;
1373 }
1374 return true;
1375}
1376
1377
Chris Lattner2b02a442007-02-25 08:29:00 +00001378//===----------------------------------------------------------------------===//
1379// Return Value Calling Convention Implementation
1380//===----------------------------------------------------------------------===//
1381
Chris Lattner59ed56b2007-02-28 04:55:35 +00001382#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001383
Michael J. Spencerec38de22010-10-10 22:04:20 +00001384bool
Eric Christopher471e4222011-06-08 23:55:35 +00001385X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1386 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001387 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001388 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001389 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001390 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001391 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001392 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001393}
1394
Dan Gohman98ca4f22009-08-05 01:29:28 +00001395SDValue
1396X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001397 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001398 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001399 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001400 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001401 MachineFunction &MF = DAG.getMachineFunction();
1402 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001403
Chris Lattner9774c912007-02-27 05:28:59 +00001404 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001405 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001406 RVLocs, *DAG.getContext());
1407 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001408
Evan Chengdcea1632010-02-04 02:40:39 +00001409 // Add the regs to the liveout set for the function.
1410 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1411 for (unsigned i = 0; i != RVLocs.size(); ++i)
1412 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1413 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001414
Dan Gohman475871a2008-07-27 21:46:04 +00001415 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001416
Dan Gohman475871a2008-07-27 21:46:04 +00001417 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001418 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1419 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001420 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1421 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001422
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001423 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001424 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1425 CCValAssign &VA = RVLocs[i];
1426 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001427 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001428 EVT ValVT = ValToCopy.getValueType();
1429
Dale Johannesenc4510512010-09-24 19:05:48 +00001430 // If this is x86-64, and we disabled SSE, we can't return FP values,
1431 // or SSE or MMX vectors.
1432 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1433 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001434 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001435 report_fatal_error("SSE register return with SSE disabled");
1436 }
1437 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1438 // llvm-gcc has never done it right and no one has noticed, so this
1439 // should be OK for now.
1440 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001441 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001442 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001443
Chris Lattner447ff682008-03-11 03:23:40 +00001444 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1445 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001446 if (VA.getLocReg() == X86::ST0 ||
1447 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001448 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1449 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001450 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001451 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001452 RetOps.push_back(ValToCopy);
1453 // Don't emit a copytoreg.
1454 continue;
1455 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001456
Evan Cheng242b38b2009-02-23 09:03:22 +00001457 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1458 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001459 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001460 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001461 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001462 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001463 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1464 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001465 // If we don't have SSE2 available, convert to v4f32 so the generated
1466 // register is legal.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001467 if (!Subtarget->hasXMMInt())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001468 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001469 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001470 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001471 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001472
Dale Johannesendd64c412009-02-04 00:33:20 +00001473 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001474 Flag = Chain.getValue(1);
1475 }
Dan Gohman61a92132008-04-21 23:59:07 +00001476
1477 // The x86-64 ABI for returning structs by value requires that we copy
1478 // the sret argument into %rax for the return. We saved the argument into
1479 // a virtual register in the entry block, so now we copy the value out
1480 // and into %rax.
1481 if (Subtarget->is64Bit() &&
1482 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1483 MachineFunction &MF = DAG.getMachineFunction();
1484 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1485 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001486 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001487 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001488 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001489
Dale Johannesendd64c412009-02-04 00:33:20 +00001490 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001491 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001492
1493 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001494 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001495 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001496
Chris Lattner447ff682008-03-11 03:23:40 +00001497 RetOps[0] = Chain; // Update chain.
1498
1499 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001500 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001501 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001502
1503 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001504 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001505}
1506
Evan Cheng3d2125c2010-11-30 23:55:39 +00001507bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1508 if (N->getNumValues() != 1)
1509 return false;
1510 if (!N->hasNUsesOfValue(1, 0))
1511 return false;
1512
1513 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001514 if (Copy->getOpcode() != ISD::CopyToReg &&
1515 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001516 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001517
1518 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001519 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001520 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001521 if (UI->getOpcode() != X86ISD::RET_FLAG)
1522 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001523 HasRet = true;
1524 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001525
Evan Cheng1bf891a2010-12-01 22:59:46 +00001526 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001527}
1528
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001529EVT
1530X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001531 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001532 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001533 // TODO: Is this also valid on 32-bit?
1534 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001535 ReturnMVT = MVT::i8;
1536 else
1537 ReturnMVT = MVT::i32;
1538
1539 EVT MinVT = getRegisterType(Context, ReturnMVT);
1540 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001541}
1542
Dan Gohman98ca4f22009-08-05 01:29:28 +00001543/// LowerCallResult - Lower the result values of a call into the
1544/// appropriate copies out of appropriate physical registers.
1545///
1546SDValue
1547X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001548 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001549 const SmallVectorImpl<ISD::InputArg> &Ins,
1550 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001551 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001552
Chris Lattnere32bbf62007-02-28 07:09:55 +00001553 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001554 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001555 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001556 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1557 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001558 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001559
Chris Lattner3085e152007-02-25 08:59:22 +00001560 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001561 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001562 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001563 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001564
Torok Edwin3f142c32009-02-01 18:15:56 +00001565 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001566 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001567 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001568 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001569 }
1570
Evan Cheng79fb3b42009-02-20 20:43:02 +00001571 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001572
1573 // If this is a call to a function that returns an fp value on the floating
1574 // point stack, we must guarantee the the value is popped from the stack, so
1575 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001576 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001577 // instead.
1578 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1579 // If we prefer to use the value in xmm registers, copy it out as f80 and
1580 // use a truncate to move it from fp stack reg to xmm reg.
1581 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001582 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001583 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1584 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001585 Val = Chain.getValue(0);
1586
1587 // Round the f80 to the right size, which also moves it to the appropriate
1588 // xmm register.
1589 if (CopyVT != VA.getValVT())
1590 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1591 // This truncation won't change the value.
1592 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001593 } else {
1594 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1595 CopyVT, InFlag).getValue(1);
1596 Val = Chain.getValue(0);
1597 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001598 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001599 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001600 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001601
Dan Gohman98ca4f22009-08-05 01:29:28 +00001602 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001603}
1604
1605
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001606//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001607// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001608//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001609// StdCall calling convention seems to be standard for many Windows' API
1610// routines and around. It differs from C calling convention just a little:
1611// callee should clean up the stack, not caller. Symbols should be also
1612// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001613// For info on fast calling convention see Fast Calling Convention (tail call)
1614// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001615
Dan Gohman98ca4f22009-08-05 01:29:28 +00001616/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001617/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001618static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1619 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001620 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001621
Dan Gohman98ca4f22009-08-05 01:29:28 +00001622 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001623}
1624
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001625/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001626/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001627static bool
1628ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1629 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001630 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001631
Dan Gohman98ca4f22009-08-05 01:29:28 +00001632 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001633}
1634
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001635/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1636/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001637/// the specific parameter attribute. The copy will be passed as a byval
1638/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001639static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001640CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001641 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1642 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001643 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001644
Dale Johannesendd64c412009-02-04 00:33:20 +00001645 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001646 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001647 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001648}
1649
Chris Lattner29689432010-03-11 00:22:57 +00001650/// IsTailCallConvention - Return true if the calling convention is one that
1651/// supports tail call optimization.
1652static bool IsTailCallConvention(CallingConv::ID CC) {
1653 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1654}
1655
Evan Cheng485fafc2011-03-21 01:19:09 +00001656bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1657 if (!CI->isTailCall())
1658 return false;
1659
1660 CallSite CS(CI);
1661 CallingConv::ID CalleeCC = CS.getCallingConv();
1662 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1663 return false;
1664
1665 return true;
1666}
1667
Evan Cheng0c439eb2010-01-27 00:07:07 +00001668/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1669/// a tailcall target by changing its ABI.
1670static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001671 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001672}
1673
Dan Gohman98ca4f22009-08-05 01:29:28 +00001674SDValue
1675X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001676 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001677 const SmallVectorImpl<ISD::InputArg> &Ins,
1678 DebugLoc dl, SelectionDAG &DAG,
1679 const CCValAssign &VA,
1680 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001681 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001682 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001683 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001684 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001685 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001686 EVT ValVT;
1687
1688 // If value is passed by pointer we have address passed instead of the value
1689 // itself.
1690 if (VA.getLocInfo() == CCValAssign::Indirect)
1691 ValVT = VA.getLocVT();
1692 else
1693 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001694
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001695 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001696 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001697 // In case of tail call optimization mark all arguments mutable. Since they
1698 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001699 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001700 unsigned Bytes = Flags.getByValSize();
1701 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1702 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001703 return DAG.getFrameIndex(FI, getPointerTy());
1704 } else {
1705 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001706 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001707 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1708 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001709 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001710 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001711 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001712}
1713
Dan Gohman475871a2008-07-27 21:46:04 +00001714SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001715X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001716 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001717 bool isVarArg,
1718 const SmallVectorImpl<ISD::InputArg> &Ins,
1719 DebugLoc dl,
1720 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001721 SmallVectorImpl<SDValue> &InVals)
1722 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001723 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001724 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001725
Gordon Henriksen86737662008-01-05 16:56:59 +00001726 const Function* Fn = MF.getFunction();
1727 if (Fn->hasExternalLinkage() &&
1728 Subtarget->isTargetCygMing() &&
1729 Fn->getName() == "main")
1730 FuncInfo->setForceFramePointer(true);
1731
Evan Cheng1bc78042006-04-26 01:20:17 +00001732 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001733 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001734 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001735
Chris Lattner29689432010-03-11 00:22:57 +00001736 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1737 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001738
Chris Lattner638402b2007-02-28 07:00:42 +00001739 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001740 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001741 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001742 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001743
1744 // Allocate shadow area for Win64
1745 if (IsWin64) {
1746 CCInfo.AllocateStack(32, 8);
1747 }
1748
Duncan Sands45907662010-10-31 13:21:44 +00001749 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001750
Chris Lattnerf39f7712007-02-28 05:46:49 +00001751 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001752 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001753 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1754 CCValAssign &VA = ArgLocs[i];
1755 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1756 // places.
1757 assert(VA.getValNo() != LastVal &&
1758 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001759 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001760 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001761
Chris Lattnerf39f7712007-02-28 05:46:49 +00001762 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001763 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001764 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001765 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001766 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001767 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001768 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001769 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001770 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001771 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001772 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001773 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1774 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001775 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001776 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001777 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001778 RC = X86::VR64RegisterClass;
1779 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001780 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001781
Devang Patel68e6bee2011-02-21 23:21:26 +00001782 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001783 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001784
Chris Lattnerf39f7712007-02-28 05:46:49 +00001785 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1786 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1787 // right size.
1788 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001789 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001790 DAG.getValueType(VA.getValVT()));
1791 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001792 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001793 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001794 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001795 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001796
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001797 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001798 // Handle MMX values passed in XMM regs.
1799 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001800 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1801 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001802 } else
1803 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001804 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001805 } else {
1806 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001807 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001808 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001809
1810 // If value is passed via pointer - do a load.
1811 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001812 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1813 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001814
Dan Gohman98ca4f22009-08-05 01:29:28 +00001815 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001816 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001817
Dan Gohman61a92132008-04-21 23:59:07 +00001818 // The x86-64 ABI for returning structs by value requires that we copy
1819 // the sret argument into %rax for the return. Save the argument into
1820 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001821 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001822 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1823 unsigned Reg = FuncInfo->getSRetReturnReg();
1824 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001825 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001826 FuncInfo->setSRetReturnReg(Reg);
1827 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001828 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001829 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001830 }
1831
Chris Lattnerf39f7712007-02-28 05:46:49 +00001832 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001833 // Align stack specially for tail calls.
1834 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001835 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001836
Evan Cheng1bc78042006-04-26 01:20:17 +00001837 // If the function takes variable number of arguments, make a frame index for
1838 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001839 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001840 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1841 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001842 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001843 }
1844 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001845 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1846
1847 // FIXME: We should really autogenerate these arrays
1848 static const unsigned GPR64ArgRegsWin64[] = {
1849 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001850 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001851 static const unsigned GPR64ArgRegs64Bit[] = {
1852 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1853 };
1854 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001855 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1856 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1857 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001858 const unsigned *GPR64ArgRegs;
1859 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001860
1861 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001862 // The XMM registers which might contain var arg parameters are shadowed
1863 // in their paired GPR. So we only need to save the GPR to their home
1864 // slots.
1865 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001866 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001867 } else {
1868 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1869 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001870
1871 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001872 }
1873 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1874 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001875
Devang Patel578efa92009-06-05 21:57:13 +00001876 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001877 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001878 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001879 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001880 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001881 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001882 // Kernel mode asks for SSE to be disabled, so don't push them
1883 // on the stack.
1884 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001885
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001886 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001887 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001888 // Get to the caller-allocated home save location. Add 8 to account
1889 // for the return address.
1890 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001891 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001892 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001893 // Fixup to set vararg frame on shadow area (4 x i64).
1894 if (NumIntRegs < 4)
1895 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001896 } else {
1897 // For X86-64, if there are vararg parameters that are passed via
1898 // registers, then we must store them to their spots on the stack so they
1899 // may be loaded by deferencing the result of va_next.
1900 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1901 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1902 FuncInfo->setRegSaveFrameIndex(
1903 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001904 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001905 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001906
Gordon Henriksen86737662008-01-05 16:56:59 +00001907 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001908 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001909 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1910 getPointerTy());
1911 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001912 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001913 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1914 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001915 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001916 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001917 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001918 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001919 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001920 MachinePointerInfo::getFixedStack(
1921 FuncInfo->getRegSaveFrameIndex(), Offset),
1922 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001923 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001924 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001925 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001926
Dan Gohmanface41a2009-08-16 21:24:25 +00001927 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1928 // Now store the XMM (fp + vector) parameter registers.
1929 SmallVector<SDValue, 11> SaveXMMOps;
1930 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001931
Devang Patel68e6bee2011-02-21 23:21:26 +00001932 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001933 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1934 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001935
Dan Gohman1e93df62010-04-17 14:41:14 +00001936 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1937 FuncInfo->getRegSaveFrameIndex()));
1938 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1939 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001940
Dan Gohmanface41a2009-08-16 21:24:25 +00001941 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001942 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001943 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001944 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1945 SaveXMMOps.push_back(Val);
1946 }
1947 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1948 MVT::Other,
1949 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001950 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001951
1952 if (!MemOps.empty())
1953 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1954 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001955 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001956 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001957
Gordon Henriksen86737662008-01-05 16:56:59 +00001958 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001959 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001960 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001961 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001962 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001963 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001964 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001965 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001966 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001967
Gordon Henriksen86737662008-01-05 16:56:59 +00001968 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001969 // RegSaveFrameIndex is X86-64 only.
1970 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001971 if (CallConv == CallingConv::X86_FastCall ||
1972 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001973 // fastcc functions can't have varargs.
1974 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001975 }
Evan Cheng25caf632006-05-23 21:06:34 +00001976
Rafael Espindola76927d752011-08-30 19:39:58 +00001977 FuncInfo->setArgumentStackSize(StackSize);
1978
Dan Gohman98ca4f22009-08-05 01:29:28 +00001979 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001980}
1981
Dan Gohman475871a2008-07-27 21:46:04 +00001982SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001983X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1984 SDValue StackPtr, SDValue Arg,
1985 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001986 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001987 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001988 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001989 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001990 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001991 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001992 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001993
1994 return DAG.getStore(Chain, dl, Arg, PtrOff,
1995 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001996 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001997}
1998
Bill Wendling64e87322009-01-16 19:25:27 +00001999/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002000/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002001SDValue
2002X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002003 SDValue &OutRetAddr, SDValue Chain,
2004 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002005 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002006 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002007 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002008 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002009
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002010 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002011 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
2012 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002013 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002014}
2015
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002016/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002017/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002018static SDValue
2019EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002020 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002021 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002022 // Store the return address to the appropriate stack slot.
2023 if (!FPDiff) return Chain;
2024 // Calculate the new stack slot for the return address.
2025 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002026 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002027 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002028 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002029 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002030 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002031 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002032 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002033 return Chain;
2034}
2035
Dan Gohman98ca4f22009-08-05 01:29:28 +00002036SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002037X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002038 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002039 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002040 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002041 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002042 const SmallVectorImpl<ISD::InputArg> &Ins,
2043 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002044 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002045 MachineFunction &MF = DAG.getMachineFunction();
2046 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002047 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002048 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002049 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002050
Evan Cheng5f941932010-02-05 02:21:12 +00002051 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002052 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002053 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2054 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002055 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002056
2057 // Sibcalls are automatically detected tailcalls which do not require
2058 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002059 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002060 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002061
2062 if (isTailCall)
2063 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002064 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002065
Chris Lattner29689432010-03-11 00:22:57 +00002066 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2067 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002068
Chris Lattner638402b2007-02-28 07:00:42 +00002069 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002070 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002071 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002072 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002073
2074 // Allocate shadow area for Win64
2075 if (IsWin64) {
2076 CCInfo.AllocateStack(32, 8);
2077 }
2078
Duncan Sands45907662010-10-31 13:21:44 +00002079 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002080
Chris Lattner423c5f42007-02-28 05:31:48 +00002081 // Get a count of how many bytes are to be pushed on the stack.
2082 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002083 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002084 // This is a sibcall. The memory operands are available in caller's
2085 // own caller's stack.
2086 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002087 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002088 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002089
Gordon Henriksen86737662008-01-05 16:56:59 +00002090 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002091 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002092 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002093 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002094 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2095 FPDiff = NumBytesCallerPushed - NumBytes;
2096
2097 // Set the delta of movement of the returnaddr stackslot.
2098 // But only set if delta is greater than previous delta.
2099 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2100 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2101 }
2102
Evan Chengf22f9b32010-02-06 03:28:46 +00002103 if (!IsSibcall)
2104 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002105
Dan Gohman475871a2008-07-27 21:46:04 +00002106 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002107 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002108 if (isTailCall && FPDiff)
2109 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2110 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002111
Dan Gohman475871a2008-07-27 21:46:04 +00002112 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2113 SmallVector<SDValue, 8> MemOpChains;
2114 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002115
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002116 // Walk the register/memloc assignments, inserting copies/loads. In the case
2117 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002118 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2119 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002120 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002121 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002122 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002123 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002124
Chris Lattner423c5f42007-02-28 05:31:48 +00002125 // Promote the value if needed.
2126 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002127 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002128 case CCValAssign::Full: break;
2129 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002130 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002131 break;
2132 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002133 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002134 break;
2135 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002136 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2137 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002138 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002139 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2140 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002141 } else
2142 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2143 break;
2144 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002145 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002146 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002147 case CCValAssign::Indirect: {
2148 // Store the argument.
2149 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002150 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002151 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002152 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002153 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002154 Arg = SpillSlot;
2155 break;
2156 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002157 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002158
Chris Lattner423c5f42007-02-28 05:31:48 +00002159 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002160 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2161 if (isVarArg && IsWin64) {
2162 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2163 // shadow reg if callee is a varargs function.
2164 unsigned ShadowReg = 0;
2165 switch (VA.getLocReg()) {
2166 case X86::XMM0: ShadowReg = X86::RCX; break;
2167 case X86::XMM1: ShadowReg = X86::RDX; break;
2168 case X86::XMM2: ShadowReg = X86::R8; break;
2169 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002170 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002171 if (ShadowReg)
2172 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002173 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002174 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002175 assert(VA.isMemLoc());
2176 if (StackPtr.getNode() == 0)
2177 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2178 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2179 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002180 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002181 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002182
Evan Cheng32fe1032006-05-25 00:59:30 +00002183 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002184 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002185 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002186
Evan Cheng347d5f72006-04-28 21:29:37 +00002187 // Build a sequence of copy-to-reg nodes chained together with token chain
2188 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002189 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002190 // Tail call byval lowering might overwrite argument registers so in case of
2191 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002192 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002193 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002194 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002195 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002196 InFlag = Chain.getValue(1);
2197 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002198
Chris Lattner88e1fd52009-07-09 04:24:46 +00002199 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002200 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2201 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002202 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002203 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2204 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002205 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002206 InFlag);
2207 InFlag = Chain.getValue(1);
2208 } else {
2209 // If we are tail calling and generating PIC/GOT style code load the
2210 // address of the callee into ECX. The value in ecx is used as target of
2211 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2212 // for tail calls on PIC/GOT architectures. Normally we would just put the
2213 // address of GOT into ebx and then call target@PLT. But for tail calls
2214 // ebx would be restored (since ebx is callee saved) before jumping to the
2215 // target@PLT.
2216
2217 // Note: The actual moving to ECX is done further down.
2218 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2219 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2220 !G->getGlobal()->hasProtectedVisibility())
2221 Callee = LowerGlobalAddress(Callee, DAG);
2222 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002223 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002224 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002225 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002226
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002227 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002228 // From AMD64 ABI document:
2229 // For calls that may call functions that use varargs or stdargs
2230 // (prototype-less calls or calls to functions containing ellipsis (...) in
2231 // the declaration) %al is used as hidden argument to specify the number
2232 // of SSE registers used. The contents of %al do not need to match exactly
2233 // the number of registers, but must be an ubound on the number of SSE
2234 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002235
Gordon Henriksen86737662008-01-05 16:56:59 +00002236 // Count the number of XMM registers allocated.
2237 static const unsigned XMMArgRegs[] = {
2238 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2239 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2240 };
2241 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002242 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002243 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002244
Dale Johannesendd64c412009-02-04 00:33:20 +00002245 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002246 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002247 InFlag = Chain.getValue(1);
2248 }
2249
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002250
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002251 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002252 if (isTailCall) {
2253 // Force all the incoming stack arguments to be loaded from the stack
2254 // before any new outgoing arguments are stored to the stack, because the
2255 // outgoing stack slots may alias the incoming argument stack slots, and
2256 // the alias isn't otherwise explicit. This is slightly more conservative
2257 // than necessary, because it means that each store effectively depends
2258 // on every argument instead of just those arguments it would clobber.
2259 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2260
Dan Gohman475871a2008-07-27 21:46:04 +00002261 SmallVector<SDValue, 8> MemOpChains2;
2262 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002263 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002264 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002265 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002266 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002267 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2268 CCValAssign &VA = ArgLocs[i];
2269 if (VA.isRegLoc())
2270 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002271 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002272 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002273 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002274 // Create frame index.
2275 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002276 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002277 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002278 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002279
Duncan Sands276dcbd2008-03-21 09:14:45 +00002280 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002281 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002282 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002283 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002284 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002285 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002286 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002287
Dan Gohman98ca4f22009-08-05 01:29:28 +00002288 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2289 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002290 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002291 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002292 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002293 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002294 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002295 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002296 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002297 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002298 }
2299 }
2300
2301 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002302 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002303 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002304
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002305 // Copy arguments to their registers.
2306 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002307 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002308 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002309 InFlag = Chain.getValue(1);
2310 }
Dan Gohman475871a2008-07-27 21:46:04 +00002311 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002312
Gordon Henriksen86737662008-01-05 16:56:59 +00002313 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002314 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002315 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002316 }
2317
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002318 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2319 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2320 // In the 64-bit large code model, we have to make all calls
2321 // through a register, since the call instruction's 32-bit
2322 // pc-relative offset may not be large enough to hold the whole
2323 // address.
2324 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002325 // If the callee is a GlobalAddress node (quite common, every direct call
2326 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2327 // it.
2328
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002329 // We should use extra load for direct calls to dllimported functions in
2330 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002331 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002332 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002333 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002334 bool ExtraLoad = false;
2335 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002336
Chris Lattner48a7d022009-07-09 05:02:21 +00002337 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2338 // external symbols most go through the PLT in PIC mode. If the symbol
2339 // has hidden or protected visibility, or if it is static or local, then
2340 // we don't need to use the PLT - we can directly call it.
2341 if (Subtarget->isTargetELF() &&
2342 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002343 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002344 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002345 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002346 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002347 (!Subtarget->getTargetTriple().isMacOSX() ||
2348 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002349 // PC-relative references to external symbols should go through $stub,
2350 // unless we're building with the leopard linker or later, which
2351 // automatically synthesizes these stubs.
2352 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002353 } else if (Subtarget->isPICStyleRIPRel() &&
2354 isa<Function>(GV) &&
2355 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2356 // If the function is marked as non-lazy, generate an indirect call
2357 // which loads from the GOT directly. This avoids runtime overhead
2358 // at the cost of eager binding (and one extra byte of encoding).
2359 OpFlags = X86II::MO_GOTPCREL;
2360 WrapperKind = X86ISD::WrapperRIP;
2361 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002362 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002363
Devang Patel0d881da2010-07-06 22:08:15 +00002364 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002365 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002366
2367 // Add a wrapper if needed.
2368 if (WrapperKind != ISD::DELETED_NODE)
2369 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2370 // Add extra indirection if needed.
2371 if (ExtraLoad)
2372 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2373 MachinePointerInfo::getGOT(),
2374 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002375 }
Bill Wendling056292f2008-09-16 21:48:12 +00002376 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002377 unsigned char OpFlags = 0;
2378
Evan Cheng1bf891a2010-12-01 22:59:46 +00002379 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2380 // external symbols should go through the PLT.
2381 if (Subtarget->isTargetELF() &&
2382 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2383 OpFlags = X86II::MO_PLT;
2384 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002385 (!Subtarget->getTargetTriple().isMacOSX() ||
2386 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002387 // PC-relative references to external symbols should go through $stub,
2388 // unless we're building with the leopard linker or later, which
2389 // automatically synthesizes these stubs.
2390 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002391 }
Eric Christopherfd179292009-08-27 18:07:15 +00002392
Chris Lattner48a7d022009-07-09 05:02:21 +00002393 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2394 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002395 }
2396
Chris Lattnerd96d0722007-02-25 06:40:16 +00002397 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002398 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002399 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002400
Evan Chengf22f9b32010-02-06 03:28:46 +00002401 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002402 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2403 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002404 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002405 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002406
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002407 Ops.push_back(Chain);
2408 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002409
Dan Gohman98ca4f22009-08-05 01:29:28 +00002410 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002411 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002412
Gordon Henriksen86737662008-01-05 16:56:59 +00002413 // Add argument registers to the end of the list so that they are known live
2414 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002415 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2416 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2417 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002418
Evan Cheng586ccac2008-03-18 23:36:35 +00002419 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002420 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002421 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2422
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002423 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002424 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002425 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002426
Gabor Greifba36cb52008-08-28 21:40:38 +00002427 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002428 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002429
Dan Gohman98ca4f22009-08-05 01:29:28 +00002430 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002431 // We used to do:
2432 //// If this is the first return lowered for this function, add the regs
2433 //// to the liveout set for the function.
2434 // This isn't right, although it's probably harmless on x86; liveouts
2435 // should be computed from returns not tail calls. Consider a void
2436 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002437 return DAG.getNode(X86ISD::TC_RETURN, dl,
2438 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002439 }
2440
Dale Johannesenace16102009-02-03 19:33:06 +00002441 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002442 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002443
Chris Lattner2d297092006-05-23 18:50:38 +00002444 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002445 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002446 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002447 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002448 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002449 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002450 // pops the hidden struct pointer, so we have to push it back.
2451 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002452 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002453 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002454 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002455
Gordon Henriksenae636f82008-01-03 16:47:34 +00002456 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002457 if (!IsSibcall) {
2458 Chain = DAG.getCALLSEQ_END(Chain,
2459 DAG.getIntPtrConstant(NumBytes, true),
2460 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2461 true),
2462 InFlag);
2463 InFlag = Chain.getValue(1);
2464 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002465
Chris Lattner3085e152007-02-25 08:59:22 +00002466 // Handle result values, copying them out of physregs into vregs that we
2467 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002468 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2469 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002470}
2471
Evan Cheng25ab6902006-09-08 06:48:29 +00002472
2473//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002474// Fast Calling Convention (tail call) implementation
2475//===----------------------------------------------------------------------===//
2476
2477// Like std call, callee cleans arguments, convention except that ECX is
2478// reserved for storing the tail called function address. Only 2 registers are
2479// free for argument passing (inreg). Tail call optimization is performed
2480// provided:
2481// * tailcallopt is enabled
2482// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002483// On X86_64 architecture with GOT-style position independent code only local
2484// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002485// To keep the stack aligned according to platform abi the function
2486// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2487// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002488// If a tail called function callee has more arguments than the caller the
2489// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002490// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002491// original REtADDR, but before the saved framepointer or the spilled registers
2492// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2493// stack layout:
2494// arg1
2495// arg2
2496// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002497// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002498// move area ]
2499// (possible EBP)
2500// ESI
2501// EDI
2502// local1 ..
2503
2504/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2505/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002506unsigned
2507X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2508 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002509 MachineFunction &MF = DAG.getMachineFunction();
2510 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002511 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002512 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002513 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002514 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002515 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002516 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2517 // Number smaller than 12 so just add the difference.
2518 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2519 } else {
2520 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002521 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002522 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002523 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002524 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002525}
2526
Evan Cheng5f941932010-02-05 02:21:12 +00002527/// MatchingStackOffset - Return true if the given stack call argument is
2528/// already available in the same position (relatively) of the caller's
2529/// incoming argument stack.
2530static
2531bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2532 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2533 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002534 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2535 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002536 if (Arg.getOpcode() == ISD::CopyFromReg) {
2537 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002538 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002539 return false;
2540 MachineInstr *Def = MRI->getVRegDef(VR);
2541 if (!Def)
2542 return false;
2543 if (!Flags.isByVal()) {
2544 if (!TII->isLoadFromStackSlot(Def, FI))
2545 return false;
2546 } else {
2547 unsigned Opcode = Def->getOpcode();
2548 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2549 Def->getOperand(1).isFI()) {
2550 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002551 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002552 } else
2553 return false;
2554 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002555 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2556 if (Flags.isByVal())
2557 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002558 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002559 // define @foo(%struct.X* %A) {
2560 // tail call @bar(%struct.X* byval %A)
2561 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002562 return false;
2563 SDValue Ptr = Ld->getBasePtr();
2564 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2565 if (!FINode)
2566 return false;
2567 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002568 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002569 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002570 FI = FINode->getIndex();
2571 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002572 } else
2573 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002574
Evan Cheng4cae1332010-03-05 08:38:04 +00002575 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002576 if (!MFI->isFixedObjectIndex(FI))
2577 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002578 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002579}
2580
Dan Gohman98ca4f22009-08-05 01:29:28 +00002581/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2582/// for tail call optimization. Targets which want to do tail call
2583/// optimization should implement this function.
2584bool
2585X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002586 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002587 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002588 bool isCalleeStructRet,
2589 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002590 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002591 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002592 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002593 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002594 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002595 CalleeCC != CallingConv::C)
2596 return false;
2597
Evan Cheng7096ae42010-01-29 06:45:59 +00002598 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002599 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002600 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002601 CallingConv::ID CallerCC = CallerF->getCallingConv();
2602 bool CCMatch = CallerCC == CalleeCC;
2603
Dan Gohman1797ed52010-02-08 20:27:50 +00002604 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002605 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002606 return true;
2607 return false;
2608 }
2609
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002610 // Look for obvious safe cases to perform tail call optimization that do not
2611 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002612
Evan Cheng2c12cb42010-03-26 16:26:03 +00002613 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2614 // emit a special epilogue.
2615 if (RegInfo->needsStackRealignment(MF))
2616 return false;
2617
Evan Chenga375d472010-03-15 18:54:48 +00002618 // Also avoid sibcall optimization if either caller or callee uses struct
2619 // return semantics.
2620 if (isCalleeStructRet || isCallerStructRet)
2621 return false;
2622
Chad Rosier2416da32011-06-24 21:15:36 +00002623 // An stdcall caller is expected to clean up its arguments; the callee
2624 // isn't going to do that.
2625 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2626 return false;
2627
Chad Rosier871f6642011-05-18 19:59:50 +00002628 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002629 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002630 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002631
2632 // Optimizing for varargs on Win64 is unlikely to be safe without
2633 // additional testing.
2634 if (Subtarget->isTargetWin64())
2635 return false;
2636
Chad Rosier871f6642011-05-18 19:59:50 +00002637 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002638 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2639 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002640
Chad Rosier871f6642011-05-18 19:59:50 +00002641 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2642 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2643 if (!ArgLocs[i].isRegLoc())
2644 return false;
2645 }
2646
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002647 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2648 // Therefore if it's not used by the call it is not safe to optimize this into
2649 // a sibcall.
2650 bool Unused = false;
2651 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2652 if (!Ins[i].Used) {
2653 Unused = true;
2654 break;
2655 }
2656 }
2657 if (Unused) {
2658 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002659 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2660 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002661 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002662 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002663 CCValAssign &VA = RVLocs[i];
2664 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2665 return false;
2666 }
2667 }
2668
Evan Cheng13617962010-04-30 01:12:32 +00002669 // If the calling conventions do not match, then we'd better make sure the
2670 // results are returned in the same way as what the caller expects.
2671 if (!CCMatch) {
2672 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002673 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2674 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002675 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2676
2677 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002678 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2679 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002680 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2681
2682 if (RVLocs1.size() != RVLocs2.size())
2683 return false;
2684 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2685 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2686 return false;
2687 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2688 return false;
2689 if (RVLocs1[i].isRegLoc()) {
2690 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2691 return false;
2692 } else {
2693 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2694 return false;
2695 }
2696 }
2697 }
2698
Evan Chenga6bff982010-01-30 01:22:00 +00002699 // If the callee takes no arguments then go on to check the results of the
2700 // call.
2701 if (!Outs.empty()) {
2702 // Check if stack adjustment is needed. For now, do not do this if any
2703 // argument is passed on the stack.
2704 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002705 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2706 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002707
2708 // Allocate shadow area for Win64
2709 if (Subtarget->isTargetWin64()) {
2710 CCInfo.AllocateStack(32, 8);
2711 }
2712
Duncan Sands45907662010-10-31 13:21:44 +00002713 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002714 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002715 MachineFunction &MF = DAG.getMachineFunction();
2716 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2717 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002718
2719 // Check if the arguments are already laid out in the right way as
2720 // the caller's fixed stack objects.
2721 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002722 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2723 const X86InstrInfo *TII =
2724 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002725 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2726 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002727 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002728 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002729 if (VA.getLocInfo() == CCValAssign::Indirect)
2730 return false;
2731 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002732 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2733 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002734 return false;
2735 }
2736 }
2737 }
Evan Cheng9c044672010-05-29 01:35:22 +00002738
2739 // If the tailcall address may be in a register, then make sure it's
2740 // possible to register allocate for it. In 32-bit, the call address can
2741 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002742 // callee-saved registers are restored. These happen to be the same
2743 // registers used to pass 'inreg' arguments so watch out for those.
2744 if (!Subtarget->is64Bit() &&
2745 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002746 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002747 unsigned NumInRegs = 0;
2748 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2749 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002750 if (!VA.isRegLoc())
2751 continue;
2752 unsigned Reg = VA.getLocReg();
2753 switch (Reg) {
2754 default: break;
2755 case X86::EAX: case X86::EDX: case X86::ECX:
2756 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002757 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002758 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002759 }
2760 }
2761 }
Evan Chenga6bff982010-01-30 01:22:00 +00002762 }
Evan Chengb1712452010-01-27 06:25:16 +00002763
Evan Cheng86809cc2010-02-03 03:28:02 +00002764 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002765}
2766
Dan Gohman3df24e62008-09-03 23:12:08 +00002767FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002768X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2769 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002770}
2771
2772
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002773//===----------------------------------------------------------------------===//
2774// Other Lowering Hooks
2775//===----------------------------------------------------------------------===//
2776
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002777static bool MayFoldLoad(SDValue Op) {
2778 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2779}
2780
2781static bool MayFoldIntoStore(SDValue Op) {
2782 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2783}
2784
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002785static bool isTargetShuffle(unsigned Opcode) {
2786 switch(Opcode) {
2787 default: return false;
2788 case X86ISD::PSHUFD:
2789 case X86ISD::PSHUFHW:
2790 case X86ISD::PSHUFLW:
2791 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002792 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002793 case X86ISD::SHUFPS:
2794 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002795 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002796 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002797 case X86ISD::MOVLPS:
2798 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002799 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002800 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002801 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002802 case X86ISD::MOVSS:
2803 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002804 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002805 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002806 case X86ISD::VUNPCKLPSY:
2807 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002808 case X86ISD::PUNPCKLWD:
2809 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002810 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002811 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002812 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002813 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002814 case X86ISD::VUNPCKHPSY:
2815 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002816 case X86ISD::PUNPCKHWD:
2817 case X86ISD::PUNPCKHBW:
2818 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002819 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002820 case X86ISD::VPERMILPS:
2821 case X86ISD::VPERMILPSY:
2822 case X86ISD::VPERMILPD:
2823 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002824 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002825 return true;
2826 }
2827 return false;
2828}
2829
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002830static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002831 SDValue V1, SelectionDAG &DAG) {
2832 switch(Opc) {
2833 default: llvm_unreachable("Unknown x86 shuffle node");
2834 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002835 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002836 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002837 return DAG.getNode(Opc, dl, VT, V1);
2838 }
2839
2840 return SDValue();
2841}
2842
2843static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002844 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002845 switch(Opc) {
2846 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002847 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002848 case X86ISD::PSHUFHW:
2849 case X86ISD::PSHUFLW:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002850 case X86ISD::VPERMILPS:
2851 case X86ISD::VPERMILPSY:
2852 case X86ISD::VPERMILPD:
2853 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002854 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2855 }
2856
2857 return SDValue();
2858}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002859
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002860static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2861 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2862 switch(Opc) {
2863 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002864 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002865 case X86ISD::SHUFPD:
2866 case X86ISD::SHUFPS:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002867 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002868 return DAG.getNode(Opc, dl, VT, V1, V2,
2869 DAG.getConstant(TargetMask, MVT::i8));
2870 }
2871 return SDValue();
2872}
2873
2874static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2875 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2876 switch(Opc) {
2877 default: llvm_unreachable("Unknown x86 shuffle node");
2878 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002879 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002880 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002881 case X86ISD::MOVLPS:
2882 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002883 case X86ISD::MOVSS:
2884 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002885 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002886 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002887 case X86ISD::VUNPCKLPSY:
2888 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002889 case X86ISD::PUNPCKLWD:
2890 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002891 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002892 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002893 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002894 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002895 case X86ISD::VUNPCKHPSY:
2896 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002897 case X86ISD::PUNPCKHWD:
2898 case X86ISD::PUNPCKHBW:
2899 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002900 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002901 return DAG.getNode(Opc, dl, VT, V1, V2);
2902 }
2903 return SDValue();
2904}
2905
Dan Gohmand858e902010-04-17 15:26:15 +00002906SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002907 MachineFunction &MF = DAG.getMachineFunction();
2908 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2909 int ReturnAddrIndex = FuncInfo->getRAIndex();
2910
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002911 if (ReturnAddrIndex == 0) {
2912 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002913 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002914 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002915 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002916 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002917 }
2918
Evan Cheng25ab6902006-09-08 06:48:29 +00002919 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002920}
2921
2922
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002923bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2924 bool hasSymbolicDisplacement) {
2925 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002926 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002927 return false;
2928
2929 // If we don't have a symbolic displacement - we don't have any extra
2930 // restrictions.
2931 if (!hasSymbolicDisplacement)
2932 return true;
2933
2934 // FIXME: Some tweaks might be needed for medium code model.
2935 if (M != CodeModel::Small && M != CodeModel::Kernel)
2936 return false;
2937
2938 // For small code model we assume that latest object is 16MB before end of 31
2939 // bits boundary. We may also accept pretty large negative constants knowing
2940 // that all objects are in the positive half of address space.
2941 if (M == CodeModel::Small && Offset < 16*1024*1024)
2942 return true;
2943
2944 // For kernel code model we know that all object resist in the negative half
2945 // of 32bits address space. We may not accept negative offsets, since they may
2946 // be just off and we may accept pretty large positive ones.
2947 if (M == CodeModel::Kernel && Offset > 0)
2948 return true;
2949
2950 return false;
2951}
2952
Evan Chengef41ff62011-06-23 17:54:54 +00002953/// isCalleePop - Determines whether the callee is required to pop its
2954/// own arguments. Callee pop is necessary to support tail calls.
2955bool X86::isCalleePop(CallingConv::ID CallingConv,
2956 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2957 if (IsVarArg)
2958 return false;
2959
2960 switch (CallingConv) {
2961 default:
2962 return false;
2963 case CallingConv::X86_StdCall:
2964 return !is64Bit;
2965 case CallingConv::X86_FastCall:
2966 return !is64Bit;
2967 case CallingConv::X86_ThisCall:
2968 return !is64Bit;
2969 case CallingConv::Fast:
2970 return TailCallOpt;
2971 case CallingConv::GHC:
2972 return TailCallOpt;
2973 }
2974}
2975
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002976/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2977/// specific condition code, returning the condition code and the LHS/RHS of the
2978/// comparison to make.
2979static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2980 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002981 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002982 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2983 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2984 // X > -1 -> X == 0, jump !sign.
2985 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002986 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002987 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2988 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002989 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002990 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002991 // X < 1 -> X <= 0
2992 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002993 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002994 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002995 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002996
Evan Chengd9558e02006-01-06 00:43:03 +00002997 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002998 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002999 case ISD::SETEQ: return X86::COND_E;
3000 case ISD::SETGT: return X86::COND_G;
3001 case ISD::SETGE: return X86::COND_GE;
3002 case ISD::SETLT: return X86::COND_L;
3003 case ISD::SETLE: return X86::COND_LE;
3004 case ISD::SETNE: return X86::COND_NE;
3005 case ISD::SETULT: return X86::COND_B;
3006 case ISD::SETUGT: return X86::COND_A;
3007 case ISD::SETULE: return X86::COND_BE;
3008 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003009 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003010 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003011
Chris Lattner4c78e022008-12-23 23:42:27 +00003012 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003013
Chris Lattner4c78e022008-12-23 23:42:27 +00003014 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003015 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3016 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003017 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3018 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003019 }
3020
Chris Lattner4c78e022008-12-23 23:42:27 +00003021 switch (SetCCOpcode) {
3022 default: break;
3023 case ISD::SETOLT:
3024 case ISD::SETOLE:
3025 case ISD::SETUGT:
3026 case ISD::SETUGE:
3027 std::swap(LHS, RHS);
3028 break;
3029 }
3030
3031 // On a floating point condition, the flags are set as follows:
3032 // ZF PF CF op
3033 // 0 | 0 | 0 | X > Y
3034 // 0 | 0 | 1 | X < Y
3035 // 1 | 0 | 0 | X == Y
3036 // 1 | 1 | 1 | unordered
3037 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003038 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003039 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003040 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003041 case ISD::SETOLT: // flipped
3042 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003043 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003044 case ISD::SETOLE: // flipped
3045 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003046 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003047 case ISD::SETUGT: // flipped
3048 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003049 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003050 case ISD::SETUGE: // flipped
3051 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003052 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003053 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003054 case ISD::SETNE: return X86::COND_NE;
3055 case ISD::SETUO: return X86::COND_P;
3056 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003057 case ISD::SETOEQ:
3058 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003059 }
Evan Chengd9558e02006-01-06 00:43:03 +00003060}
3061
Evan Cheng4a460802006-01-11 00:33:36 +00003062/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3063/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003064/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003065static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003066 switch (X86CC) {
3067 default:
3068 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003069 case X86::COND_B:
3070 case X86::COND_BE:
3071 case X86::COND_E:
3072 case X86::COND_P:
3073 case X86::COND_A:
3074 case X86::COND_AE:
3075 case X86::COND_NE:
3076 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003077 return true;
3078 }
3079}
3080
Evan Chengeb2f9692009-10-27 19:56:55 +00003081/// isFPImmLegal - Returns true if the target can instruction select the
3082/// specified FP immediate natively. If false, the legalizer will
3083/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003084bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003085 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3086 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3087 return true;
3088 }
3089 return false;
3090}
3091
Nate Begeman9008ca62009-04-27 18:41:29 +00003092/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3093/// the specified range (L, H].
3094static bool isUndefOrInRange(int Val, int Low, int Hi) {
3095 return (Val < 0) || (Val >= Low && Val < Hi);
3096}
3097
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003098/// isUndefOrInRange - Return true if every element in Mask, begining
3099/// from position Pos and ending in Pos+Size, falls within the specified
3100/// range (L, L+Pos]. or is undef.
3101static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3102 int Pos, int Size, int Low, int Hi) {
3103 for (int i = Pos, e = Pos+Size; i != e; ++i)
3104 if (!isUndefOrInRange(Mask[i], Low, Hi))
3105 return false;
3106 return true;
3107}
3108
Nate Begeman9008ca62009-04-27 18:41:29 +00003109/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3110/// specified value.
3111static bool isUndefOrEqual(int Val, int CmpVal) {
3112 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003113 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003114 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003115}
3116
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003117/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3118/// from position Pos and ending in Pos+Size, falls within the specified
3119/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003120static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3121 int Pos, int Size, int Low) {
3122 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3123 if (!isUndefOrEqual(Mask[i], Low))
3124 return false;
3125 return true;
3126}
3127
Nate Begeman9008ca62009-04-27 18:41:29 +00003128/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3129/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3130/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003131static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003132 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003133 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003134 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003135 return (Mask[0] < 2 && Mask[1] < 2);
3136 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003137}
3138
Nate Begeman9008ca62009-04-27 18:41:29 +00003139bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003140 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003141 N->getMask(M);
3142 return ::isPSHUFDMask(M, N->getValueType(0));
3143}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003144
Nate Begeman9008ca62009-04-27 18:41:29 +00003145/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3146/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003147static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003148 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003149 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003150
Nate Begeman9008ca62009-04-27 18:41:29 +00003151 // Lower quadword copied in order or undef.
3152 for (int i = 0; i != 4; ++i)
3153 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003154 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003155
Evan Cheng506d3df2006-03-29 23:07:14 +00003156 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003157 for (int i = 4; i != 8; ++i)
3158 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003159 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003160
Evan Cheng506d3df2006-03-29 23:07:14 +00003161 return true;
3162}
3163
Nate Begeman9008ca62009-04-27 18:41:29 +00003164bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003165 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003166 N->getMask(M);
3167 return ::isPSHUFHWMask(M, N->getValueType(0));
3168}
Evan Cheng506d3df2006-03-29 23:07:14 +00003169
Nate Begeman9008ca62009-04-27 18:41:29 +00003170/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3171/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003172static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003173 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003174 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003175
Rafael Espindola15684b22009-04-24 12:40:33 +00003176 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003177 for (int i = 4; i != 8; ++i)
3178 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003179 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003180
Rafael Espindola15684b22009-04-24 12:40:33 +00003181 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003182 for (int i = 0; i != 4; ++i)
3183 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003184 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003185
Rafael Espindola15684b22009-04-24 12:40:33 +00003186 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003187}
3188
Nate Begeman9008ca62009-04-27 18:41:29 +00003189bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003190 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003191 N->getMask(M);
3192 return ::isPSHUFLWMask(M, N->getValueType(0));
3193}
3194
Nate Begemana09008b2009-10-19 02:17:23 +00003195/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3196/// is suitable for input to PALIGNR.
3197static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003198 bool hasSSSE3OrAVX) {
Nate Begemana09008b2009-10-19 02:17:23 +00003199 int i, e = VT.getVectorNumElements();
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003200 if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
3201 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003202
Nate Begemana09008b2009-10-19 02:17:23 +00003203 // Do not handle v2i64 / v2f64 shuffles with palignr.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003204 if (e < 4 || !hasSSSE3OrAVX)
Nate Begemana09008b2009-10-19 02:17:23 +00003205 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003206
Nate Begemana09008b2009-10-19 02:17:23 +00003207 for (i = 0; i != e; ++i)
3208 if (Mask[i] >= 0)
3209 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003210
Nate Begemana09008b2009-10-19 02:17:23 +00003211 // All undef, not a palignr.
3212 if (i == e)
3213 return false;
3214
Eli Friedman63f8dde2011-07-25 21:36:45 +00003215 // Make sure we're shifting in the right direction.
3216 if (Mask[i] <= i)
3217 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003218
3219 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003220
Nate Begemana09008b2009-10-19 02:17:23 +00003221 // Check the rest of the elements to see if they are consecutive.
3222 for (++i; i != e; ++i) {
3223 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003224 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003225 return false;
3226 }
3227 return true;
3228}
3229
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003230/// isVSHUFPSYMask - Return true if the specified VECTOR_SHUFFLE operand
3231/// specifies a shuffle of elements that is suitable for input to 256-bit
3232/// VSHUFPSY.
3233static bool isVSHUFPSYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3234 const X86Subtarget *Subtarget) {
3235 int NumElems = VT.getVectorNumElements();
3236
3237 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3238 return false;
3239
3240 if (NumElems != 8)
3241 return false;
3242
3243 // VSHUFPSY divides the resulting vector into 4 chunks.
3244 // The sources are also splitted into 4 chunks, and each destination
3245 // chunk must come from a different source chunk.
3246 //
3247 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3248 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3249 //
3250 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3251 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3252 //
3253 int QuarterSize = NumElems/4;
3254 int HalfSize = QuarterSize*2;
3255 for (int i = 0; i < QuarterSize; ++i)
3256 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3257 return false;
3258 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3259 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3260 return false;
3261
3262 // The mask of the second half must be the same as the first but with
3263 // the appropriate offsets. This works in the same way as VPERMILPS
3264 // works with masks.
3265 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3266 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3267 return false;
3268 int FstHalfIdx = i-HalfSize;
3269 if (Mask[FstHalfIdx] < 0)
3270 continue;
3271 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3272 return false;
3273 }
3274 for (int i = QuarterSize*3; i < NumElems; ++i) {
3275 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3276 return false;
3277 int FstHalfIdx = i-HalfSize;
3278 if (Mask[FstHalfIdx] < 0)
3279 continue;
3280 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3281 return false;
3282
3283 }
3284
3285 return true;
3286}
3287
3288/// getShuffleVSHUFPSYImmediate - Return the appropriate immediate to shuffle
3289/// the specified VECTOR_MASK mask with VSHUFPSY instruction.
3290static unsigned getShuffleVSHUFPSYImmediate(SDNode *N) {
3291 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3292 EVT VT = SVOp->getValueType(0);
3293 int NumElems = VT.getVectorNumElements();
3294
3295 assert(NumElems == 8 && VT.getSizeInBits() == 256 &&
3296 "Only supports v8i32 and v8f32 types");
3297
3298 int HalfSize = NumElems/2;
3299 unsigned Mask = 0;
3300 for (int i = 0; i != NumElems ; ++i) {
3301 if (SVOp->getMaskElt(i) < 0)
3302 continue;
3303 // The mask of the first half must be equal to the second one.
3304 unsigned Shamt = (i%HalfSize)*2;
3305 unsigned Elt = SVOp->getMaskElt(i) % HalfSize;
3306 Mask |= Elt << Shamt;
3307 }
3308
3309 return Mask;
3310}
3311
3312/// isVSHUFPDYMask - Return true if the specified VECTOR_SHUFFLE operand
3313/// specifies a shuffle of elements that is suitable for input to 256-bit
3314/// VSHUFPDY. This shuffle doesn't have the same restriction as the PS
3315/// version and the mask of the second half isn't binded with the first
3316/// one.
3317static bool isVSHUFPDYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3318 const X86Subtarget *Subtarget) {
3319 int NumElems = VT.getVectorNumElements();
3320
3321 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3322 return false;
3323
3324 if (NumElems != 4)
3325 return false;
3326
3327 // VSHUFPSY divides the resulting vector into 4 chunks.
3328 // The sources are also splitted into 4 chunks, and each destination
3329 // chunk must come from a different source chunk.
3330 //
3331 // SRC1 => X3 X2 X1 X0
3332 // SRC2 => Y3 Y2 Y1 Y0
3333 //
3334 // DST => Y2..Y3, X2..X3, Y1..Y0, X1..X0
3335 //
3336 int QuarterSize = NumElems/4;
3337 int HalfSize = QuarterSize*2;
3338 for (int i = 0; i < QuarterSize; ++i)
3339 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3340 return false;
3341 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3342 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3343 return false;
3344 for (int i = QuarterSize*2; i < QuarterSize*3; ++i)
3345 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3346 return false;
3347 for (int i = QuarterSize*3; i < NumElems; ++i)
3348 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3349 return false;
3350
3351 return true;
3352}
3353
3354/// getShuffleVSHUFPDYImmediate - Return the appropriate immediate to shuffle
3355/// the specified VECTOR_MASK mask with VSHUFPDY instruction.
3356static unsigned getShuffleVSHUFPDYImmediate(SDNode *N) {
3357 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3358 EVT VT = SVOp->getValueType(0);
3359 int NumElems = VT.getVectorNumElements();
3360
3361 assert(NumElems == 4 && VT.getSizeInBits() == 256 &&
3362 "Only supports v4i64 and v4f64 types");
3363
3364 int HalfSize = NumElems/2;
3365 unsigned Mask = 0;
3366 for (int i = 0; i != NumElems ; ++i) {
3367 if (SVOp->getMaskElt(i) < 0)
3368 continue;
3369 int Elt = SVOp->getMaskElt(i) % HalfSize;
3370 Mask |= Elt << i;
3371 }
3372
3373 return Mask;
3374}
3375
Evan Cheng14aed5e2006-03-24 01:18:28 +00003376/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003377/// specifies a shuffle of elements that is suitable for input to 128-bit
3378/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003379static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003380 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003381
3382 if (VT.getSizeInBits() != 128)
3383 return false;
3384
Nate Begeman9008ca62009-04-27 18:41:29 +00003385 if (NumElems != 2 && NumElems != 4)
3386 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003387
Nate Begeman9008ca62009-04-27 18:41:29 +00003388 int Half = NumElems / 2;
3389 for (int i = 0; i < Half; ++i)
3390 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003391 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003392 for (int i = Half; i < NumElems; ++i)
3393 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003394 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003395
Evan Cheng14aed5e2006-03-24 01:18:28 +00003396 return true;
3397}
3398
Nate Begeman9008ca62009-04-27 18:41:29 +00003399bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3400 SmallVector<int, 8> M;
3401 N->getMask(M);
3402 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003403}
3404
Evan Cheng213d2cf2007-05-17 18:45:50 +00003405/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003406/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3407/// half elements to come from vector 1 (which would equal the dest.) and
3408/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003409static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003410 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003411
3412 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003413 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003414
Nate Begeman9008ca62009-04-27 18:41:29 +00003415 int Half = NumElems / 2;
3416 for (int i = 0; i < Half; ++i)
3417 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003418 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003419 for (int i = Half; i < NumElems; ++i)
3420 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003421 return false;
3422 return true;
3423}
3424
Nate Begeman9008ca62009-04-27 18:41:29 +00003425static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3426 SmallVector<int, 8> M;
3427 N->getMask(M);
3428 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003429}
3430
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003431/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3432/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003433bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003434 EVT VT = N->getValueType(0);
3435 unsigned NumElems = VT.getVectorNumElements();
3436
3437 if (VT.getSizeInBits() != 128)
3438 return false;
3439
3440 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003441 return false;
3442
Evan Cheng2064a2b2006-03-28 06:50:32 +00003443 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003444 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3445 isUndefOrEqual(N->getMaskElt(1), 7) &&
3446 isUndefOrEqual(N->getMaskElt(2), 2) &&
3447 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003448}
3449
Nate Begeman0b10b912009-11-07 23:17:15 +00003450/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3451/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3452/// <2, 3, 2, 3>
3453bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003454 EVT VT = N->getValueType(0);
3455 unsigned NumElems = VT.getVectorNumElements();
3456
3457 if (VT.getSizeInBits() != 128)
3458 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003459
Nate Begeman0b10b912009-11-07 23:17:15 +00003460 if (NumElems != 4)
3461 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003462
Nate Begeman0b10b912009-11-07 23:17:15 +00003463 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003464 isUndefOrEqual(N->getMaskElt(1), 3) &&
3465 isUndefOrEqual(N->getMaskElt(2), 2) &&
3466 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003467}
3468
Evan Cheng5ced1d82006-04-06 23:23:56 +00003469/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3470/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003471bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3472 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003473
Evan Cheng5ced1d82006-04-06 23:23:56 +00003474 if (NumElems != 2 && NumElems != 4)
3475 return false;
3476
Evan Chengc5cdff22006-04-07 21:53:05 +00003477 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003478 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003479 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003480
Evan Chengc5cdff22006-04-07 21:53:05 +00003481 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003482 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003483 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003484
3485 return true;
3486}
3487
Nate Begeman0b10b912009-11-07 23:17:15 +00003488/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3489/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3490bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003491 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003492
David Greenea20244d2011-03-02 17:23:43 +00003493 if ((NumElems != 2 && NumElems != 4)
3494 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003495 return false;
3496
Evan Chengc5cdff22006-04-07 21:53:05 +00003497 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003498 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003499 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003500
Nate Begeman9008ca62009-04-27 18:41:29 +00003501 for (unsigned i = 0; i < NumElems/2; ++i)
3502 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003503 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003504
3505 return true;
3506}
3507
Evan Cheng0038e592006-03-28 00:39:58 +00003508/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3509/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003510static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003511 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003512 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003513
3514 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3515 "Unsupported vector type for unpckh");
3516
3517 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng0038e592006-03-28 00:39:58 +00003518 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003519
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003520 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3521 // independently on 128-bit lanes.
3522 unsigned NumLanes = VT.getSizeInBits()/128;
3523 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003524
3525 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003526 unsigned End = NumLaneElts;
3527 for (unsigned s = 0; s < NumLanes; ++s) {
3528 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003529 i != End;
3530 i += 2, ++j) {
3531 int BitI = Mask[i];
3532 int BitI1 = Mask[i+1];
3533 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003534 return false;
David Greenea20244d2011-03-02 17:23:43 +00003535 if (V2IsSplat) {
3536 if (!isUndefOrEqual(BitI1, NumElts))
3537 return false;
3538 } else {
3539 if (!isUndefOrEqual(BitI1, j + NumElts))
3540 return false;
3541 }
Evan Cheng39623da2006-04-20 08:58:49 +00003542 }
David Greenea20244d2011-03-02 17:23:43 +00003543 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003544 Start += NumLaneElts;
3545 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003546 }
David Greenea20244d2011-03-02 17:23:43 +00003547
Evan Cheng0038e592006-03-28 00:39:58 +00003548 return true;
3549}
3550
Nate Begeman9008ca62009-04-27 18:41:29 +00003551bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3552 SmallVector<int, 8> M;
3553 N->getMask(M);
3554 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003555}
3556
Evan Cheng4fcb9222006-03-28 02:43:26 +00003557/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3558/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003559static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003560 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003561 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003562
3563 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3564 "Unsupported vector type for unpckh");
3565
3566 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003567 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003568
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003569 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3570 // independently on 128-bit lanes.
3571 unsigned NumLanes = VT.getSizeInBits()/128;
3572 unsigned NumLaneElts = NumElts/NumLanes;
3573
3574 unsigned Start = 0;
3575 unsigned End = NumLaneElts;
3576 for (unsigned l = 0; l != NumLanes; ++l) {
3577 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3578 i != End; i += 2, ++j) {
3579 int BitI = Mask[i];
3580 int BitI1 = Mask[i+1];
3581 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003582 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003583 if (V2IsSplat) {
3584 if (isUndefOrEqual(BitI1, NumElts))
3585 return false;
3586 } else {
3587 if (!isUndefOrEqual(BitI1, j+NumElts))
3588 return false;
3589 }
Evan Cheng39623da2006-04-20 08:58:49 +00003590 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003591 // Process the next 128 bits.
3592 Start += NumLaneElts;
3593 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003594 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003595 return true;
3596}
3597
Nate Begeman9008ca62009-04-27 18:41:29 +00003598bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3599 SmallVector<int, 8> M;
3600 N->getMask(M);
3601 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003602}
3603
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003604/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3605/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3606/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003607static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003608 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003609 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003610 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003611
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003612 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3613 // FIXME: Need a better way to get rid of this, there's no latency difference
3614 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3615 // the former later. We should also remove the "_undef" special mask.
3616 if (NumElems == 4 && VT.getSizeInBits() == 256)
3617 return false;
3618
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003619 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3620 // independently on 128-bit lanes.
3621 unsigned NumLanes = VT.getSizeInBits() / 128;
3622 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003623
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003624 for (unsigned s = 0; s < NumLanes; ++s) {
3625 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3626 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003627 i += 2, ++j) {
3628 int BitI = Mask[i];
3629 int BitI1 = Mask[i+1];
3630
3631 if (!isUndefOrEqual(BitI, j))
3632 return false;
3633 if (!isUndefOrEqual(BitI1, j))
3634 return false;
3635 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003636 }
David Greenea20244d2011-03-02 17:23:43 +00003637
Rafael Espindola15684b22009-04-24 12:40:33 +00003638 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003639}
3640
Nate Begeman9008ca62009-04-27 18:41:29 +00003641bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3642 SmallVector<int, 8> M;
3643 N->getMask(M);
3644 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3645}
3646
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003647/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3648/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3649/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003650static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003651 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003652 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3653 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003654
Nate Begeman9008ca62009-04-27 18:41:29 +00003655 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3656 int BitI = Mask[i];
3657 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003658 if (!isUndefOrEqual(BitI, j))
3659 return false;
3660 if (!isUndefOrEqual(BitI1, j))
3661 return false;
3662 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003663 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003664}
3665
Nate Begeman9008ca62009-04-27 18:41:29 +00003666bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3667 SmallVector<int, 8> M;
3668 N->getMask(M);
3669 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3670}
3671
Evan Cheng017dcc62006-04-21 01:05:10 +00003672/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3673/// specifies a shuffle of elements that is suitable for input to MOVSS,
3674/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003675static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003676 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003677 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003678
3679 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003680
Nate Begeman9008ca62009-04-27 18:41:29 +00003681 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003682 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003683
Nate Begeman9008ca62009-04-27 18:41:29 +00003684 for (int i = 1; i < NumElts; ++i)
3685 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003686 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003687
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003688 return true;
3689}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003690
Nate Begeman9008ca62009-04-27 18:41:29 +00003691bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3692 SmallVector<int, 8> M;
3693 N->getMask(M);
3694 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003695}
3696
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003697/// isVPERM2F128Mask - Match 256-bit shuffles where the elements are considered
3698/// as permutations between 128-bit chunks or halves. As an example: this
3699/// shuffle bellow:
3700/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3701/// The first half comes from the second half of V1 and the second half from the
3702/// the second half of V2.
3703static bool isVPERM2F128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3704 const X86Subtarget *Subtarget) {
3705 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3706 return false;
3707
3708 // The shuffle result is divided into half A and half B. In total the two
3709 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3710 // B must come from C, D, E or F.
3711 int HalfSize = VT.getVectorNumElements()/2;
3712 bool MatchA = false, MatchB = false;
3713
3714 // Check if A comes from one of C, D, E, F.
3715 for (int Half = 0; Half < 4; ++Half) {
3716 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3717 MatchA = true;
3718 break;
3719 }
3720 }
3721
3722 // Check if B comes from one of C, D, E, F.
3723 for (int Half = 0; Half < 4; ++Half) {
3724 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3725 MatchB = true;
3726 break;
3727 }
3728 }
3729
3730 return MatchA && MatchB;
3731}
3732
3733/// getShuffleVPERM2F128Immediate - Return the appropriate immediate to shuffle
3734/// the specified VECTOR_MASK mask with VPERM2F128 instructions.
3735static unsigned getShuffleVPERM2F128Immediate(SDNode *N) {
3736 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3737 EVT VT = SVOp->getValueType(0);
3738
3739 int HalfSize = VT.getVectorNumElements()/2;
3740
3741 int FstHalf = 0, SndHalf = 0;
3742 for (int i = 0; i < HalfSize; ++i) {
3743 if (SVOp->getMaskElt(i) > 0) {
3744 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3745 break;
3746 }
3747 }
3748 for (int i = HalfSize; i < HalfSize*2; ++i) {
3749 if (SVOp->getMaskElt(i) > 0) {
3750 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3751 break;
3752 }
3753 }
3754
3755 return (FstHalf | (SndHalf << 4));
3756}
3757
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003758/// isVPERMILPDMask - Return true if the specified VECTOR_SHUFFLE operand
3759/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3760/// Note that VPERMIL mask matching is different depending whether theunderlying
3761/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3762/// to the same elements of the low, but to the higher half of the source.
3763/// In VPERMILPD the two lanes could be shuffled independently of each other
3764/// with the same restriction that lanes can't be crossed.
3765static bool isVPERMILPDMask(const SmallVectorImpl<int> &Mask, EVT VT,
3766 const X86Subtarget *Subtarget) {
3767 int NumElts = VT.getVectorNumElements();
3768 int NumLanes = VT.getSizeInBits()/128;
3769
3770 if (!Subtarget->hasAVX())
3771 return false;
3772
Eli Friedmandca62d52011-10-10 22:28:47 +00003773 // Only match 256-bit with 64-bit types
3774 if (VT.getSizeInBits() != 256 || NumElts != 4)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003775 return false;
3776
3777 // The mask on the high lane is independent of the low. Both can match
3778 // any element in inside its own lane, but can't cross.
3779 int LaneSize = NumElts/NumLanes;
3780 for (int l = 0; l < NumLanes; ++l)
3781 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3782 int LaneStart = l*LaneSize;
3783 if (!isUndefOrInRange(Mask[i], LaneStart, LaneStart+LaneSize))
3784 return false;
3785 }
3786
3787 return true;
3788}
3789
3790/// isVPERMILPSMask - Return true if the specified VECTOR_SHUFFLE operand
3791/// specifies a shuffle of elements that is suitable for input to VPERMILPS*.
3792/// Note that VPERMIL mask matching is different depending whether theunderlying
3793/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3794/// to the same elements of the low, but to the higher half of the source.
3795/// In VPERMILPD the two lanes could be shuffled independently of each other
3796/// with the same restriction that lanes can't be crossed.
3797static bool isVPERMILPSMask(const SmallVectorImpl<int> &Mask, EVT VT,
3798 const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003799 unsigned NumElts = VT.getVectorNumElements();
3800 unsigned NumLanes = VT.getSizeInBits()/128;
3801
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003802 if (!Subtarget->hasAVX())
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003803 return false;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003804
Eli Friedmandca62d52011-10-10 22:28:47 +00003805 // Only match 256-bit with 32-bit types
3806 if (VT.getSizeInBits() != 256 || NumElts != 8)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003807 return false;
3808
3809 // The mask on the high lane should be the same as the low. Actually,
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003810 // they can differ if any of the corresponding index in a lane is undef
3811 // and the other stays in range.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003812 int LaneSize = NumElts/NumLanes;
3813 for (int i = 0; i < LaneSize; ++i) {
3814 int HighElt = i+LaneSize;
Bruno Cardoso Lopes155a92a2011-08-10 01:54:17 +00003815 bool HighValid = isUndefOrInRange(Mask[HighElt], LaneSize, NumElts);
3816 bool LowValid = isUndefOrInRange(Mask[i], 0, LaneSize);
3817
3818 if (!HighValid || !LowValid)
3819 return false;
3820 if (Mask[i] < 0 || Mask[HighElt] < 0)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003821 continue;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003822 if (Mask[HighElt]-Mask[i] != LaneSize)
3823 return false;
3824 }
3825
3826 return true;
3827}
3828
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003829/// getShuffleVPERMILPSImmediate - Return the appropriate immediate to shuffle
3830/// the specified VECTOR_MASK mask with VPERMILPS* instructions.
3831static unsigned getShuffleVPERMILPSImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003832 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3833 EVT VT = SVOp->getValueType(0);
3834
3835 int NumElts = VT.getVectorNumElements();
3836 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003837 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003838
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003839 // Although the mask is equal for both lanes do it twice to get the cases
3840 // where a mask will match because the same mask element is undef on the
3841 // first half but valid on the second. This would get pathological cases
3842 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003843 unsigned Mask = 0;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003844 for (int l = 0; l < NumLanes; ++l) {
3845 for (int i = 0; i < LaneSize; ++i) {
3846 int MaskElt = SVOp->getMaskElt(i+(l*LaneSize));
3847 if (MaskElt < 0)
3848 continue;
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003849 if (MaskElt >= LaneSize)
3850 MaskElt -= LaneSize;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003851 Mask |= MaskElt << (i*2);
3852 }
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003853 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003854
3855 return Mask;
3856}
3857
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003858/// getShuffleVPERMILPDImmediate - Return the appropriate immediate to shuffle
3859/// the specified VECTOR_MASK mask with VPERMILPD* instructions.
3860static unsigned getShuffleVPERMILPDImmediate(SDNode *N) {
3861 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3862 EVT VT = SVOp->getValueType(0);
3863
3864 int NumElts = VT.getVectorNumElements();
3865 int NumLanes = VT.getSizeInBits()/128;
3866
3867 unsigned Mask = 0;
3868 int LaneSize = NumElts/NumLanes;
3869 for (int l = 0; l < NumLanes; ++l)
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003870 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3871 int MaskElt = SVOp->getMaskElt(i);
3872 if (MaskElt < 0)
3873 continue;
3874 Mask |= (MaskElt-l*LaneSize) << i;
3875 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003876
3877 return Mask;
3878}
3879
Evan Cheng017dcc62006-04-21 01:05:10 +00003880/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3881/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003882/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003883static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003884 bool V2IsSplat = false, bool V2IsUndef = false) {
3885 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003886 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003887 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003888
Nate Begeman9008ca62009-04-27 18:41:29 +00003889 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003890 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003891
Nate Begeman9008ca62009-04-27 18:41:29 +00003892 for (int i = 1; i < NumOps; ++i)
3893 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3894 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3895 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003896 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003897
Evan Cheng39623da2006-04-20 08:58:49 +00003898 return true;
3899}
3900
Nate Begeman9008ca62009-04-27 18:41:29 +00003901static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003902 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003903 SmallVector<int, 8> M;
3904 N->getMask(M);
3905 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003906}
3907
Evan Chengd9539472006-04-14 21:59:03 +00003908/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3909/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003910/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3911bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3912 const X86Subtarget *Subtarget) {
3913 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003914 return false;
3915
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003916 // The second vector must be undef
3917 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3918 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003919
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003920 EVT VT = N->getValueType(0);
3921 unsigned NumElems = VT.getVectorNumElements();
3922
3923 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3924 (VT.getSizeInBits() == 256 && NumElems != 8))
3925 return false;
3926
3927 // "i+1" is the value the indexed mask element must have
3928 for (unsigned i = 0; i < NumElems; i += 2)
3929 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3930 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003931 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003932
3933 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003934}
3935
3936/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3937/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003938/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3939bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3940 const X86Subtarget *Subtarget) {
3941 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003942 return false;
3943
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003944 // The second vector must be undef
3945 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3946 return false;
3947
3948 EVT VT = N->getValueType(0);
3949 unsigned NumElems = VT.getVectorNumElements();
3950
3951 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3952 (VT.getSizeInBits() == 256 && NumElems != 8))
3953 return false;
3954
3955 // "i" is the value the indexed mask element must have
3956 for (unsigned i = 0; i < NumElems; i += 2)
3957 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3958 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003959 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003960
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003961 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003962}
3963
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003964/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3965/// specifies a shuffle of elements that is suitable for input to 256-bit
3966/// version of MOVDDUP.
3967static bool isMOVDDUPYMask(ShuffleVectorSDNode *N,
3968 const X86Subtarget *Subtarget) {
3969 EVT VT = N->getValueType(0);
3970 int NumElts = VT.getVectorNumElements();
3971 bool V2IsUndef = N->getOperand(1).getOpcode() == ISD::UNDEF;
3972
3973 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256 ||
3974 !V2IsUndef || NumElts != 4)
3975 return false;
3976
3977 for (int i = 0; i != NumElts/2; ++i)
3978 if (!isUndefOrEqual(N->getMaskElt(i), 0))
3979 return false;
3980 for (int i = NumElts/2; i != NumElts; ++i)
3981 if (!isUndefOrEqual(N->getMaskElt(i), NumElts/2))
3982 return false;
3983 return true;
3984}
3985
Evan Cheng0b457f02008-09-25 20:50:48 +00003986/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003987/// specifies a shuffle of elements that is suitable for input to 128-bit
3988/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003989bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003990 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003991
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003992 if (VT.getSizeInBits() != 128)
3993 return false;
3994
3995 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003996 for (int i = 0; i < e; ++i)
3997 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003998 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003999 for (int i = 0; i < e; ++i)
4000 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004001 return false;
4002 return true;
4003}
4004
David Greenec38a03e2011-02-03 15:50:00 +00004005/// isVEXTRACTF128Index - Return true if the specified
4006/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4007/// suitable for input to VEXTRACTF128.
4008bool X86::isVEXTRACTF128Index(SDNode *N) {
4009 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4010 return false;
4011
4012 // The index should be aligned on a 128-bit boundary.
4013 uint64_t Index =
4014 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4015
4016 unsigned VL = N->getValueType(0).getVectorNumElements();
4017 unsigned VBits = N->getValueType(0).getSizeInBits();
4018 unsigned ElSize = VBits / VL;
4019 bool Result = (Index * ElSize) % 128 == 0;
4020
4021 return Result;
4022}
4023
David Greeneccacdc12011-02-04 16:08:29 +00004024/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4025/// operand specifies a subvector insert that is suitable for input to
4026/// VINSERTF128.
4027bool X86::isVINSERTF128Index(SDNode *N) {
4028 if (!isa<ConstantSDNode>(N->getOperand(2).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(2).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
Evan Cheng63d33002006-03-22 08:01:21 +00004043/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004044/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00004045unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004046 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4047 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4048
Evan Chengb9df0ca2006-03-22 02:53:00 +00004049 unsigned Shift = (NumOperands == 4) ? 2 : 1;
4050 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00004051 for (int i = 0; i < NumOperands; ++i) {
4052 int Val = SVOp->getMaskElt(NumOperands-i-1);
4053 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00004054 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00004055 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00004056 if (i != NumOperands - 1)
4057 Mask <<= Shift;
4058 }
Evan Cheng63d33002006-03-22 08:01:21 +00004059 return Mask;
4060}
4061
Evan Cheng506d3df2006-03-29 23:07:14 +00004062/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004063/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004064unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004065 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004066 unsigned Mask = 0;
4067 // 8 nodes, but we only care about the last 4.
4068 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004069 int Val = SVOp->getMaskElt(i);
4070 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004071 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00004072 if (i != 4)
4073 Mask <<= 2;
4074 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004075 return Mask;
4076}
4077
4078/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004079/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004080unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004081 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004082 unsigned Mask = 0;
4083 // 8 nodes, but we only care about the first 4.
4084 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004085 int Val = SVOp->getMaskElt(i);
4086 if (Val >= 0)
4087 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004088 if (i != 0)
4089 Mask <<= 2;
4090 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004091 return Mask;
4092}
4093
Nate Begemana09008b2009-10-19 02:17:23 +00004094/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4095/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4096unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4097 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4098 EVT VVT = N->getValueType(0);
4099 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4100 int Val = 0;
4101
4102 unsigned i, e;
4103 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4104 Val = SVOp->getMaskElt(i);
4105 if (Val >= 0)
4106 break;
4107 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004108 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004109 return (Val - i) * EltSize;
4110}
4111
David Greenec38a03e2011-02-03 15:50:00 +00004112/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4113/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4114/// instructions.
4115unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4116 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4117 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4118
4119 uint64_t Index =
4120 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4121
4122 EVT VecVT = N->getOperand(0).getValueType();
4123 EVT ElVT = VecVT.getVectorElementType();
4124
4125 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004126 return Index / NumElemsPerChunk;
4127}
4128
David Greeneccacdc12011-02-04 16:08:29 +00004129/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4130/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4131/// instructions.
4132unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4133 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4134 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4135
4136 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004137 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004138
4139 EVT VecVT = N->getValueType(0);
4140 EVT ElVT = VecVT.getVectorElementType();
4141
4142 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004143 return Index / NumElemsPerChunk;
4144}
4145
Evan Cheng37b73872009-07-30 08:33:02 +00004146/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4147/// constant +0.0.
4148bool X86::isZeroNode(SDValue Elt) {
4149 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004150 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004151 (isa<ConstantFPSDNode>(Elt) &&
4152 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4153}
4154
Nate Begeman9008ca62009-04-27 18:41:29 +00004155/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4156/// their permute mask.
4157static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4158 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004159 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004160 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004161 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004162
Nate Begeman5a5ca152009-04-29 05:20:52 +00004163 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004164 int idx = SVOp->getMaskElt(i);
4165 if (idx < 0)
4166 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004167 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004168 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004169 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004170 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004171 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004172 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4173 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004174}
4175
Evan Cheng779ccea2007-12-07 21:30:01 +00004176/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
4177/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00004178static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004179 unsigned NumElems = VT.getVectorNumElements();
4180 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004181 int idx = Mask[i];
4182 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004183 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004184 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004185 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004186 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004187 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004188 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004189}
4190
Evan Cheng533a0aa2006-04-19 20:35:22 +00004191/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4192/// match movhlps. The lower half elements should come from upper half of
4193/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004194/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004195static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004196 EVT VT = Op->getValueType(0);
4197 if (VT.getSizeInBits() != 128)
4198 return false;
4199 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004200 return false;
4201 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004202 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004203 return false;
4204 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004205 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004206 return false;
4207 return true;
4208}
4209
Evan Cheng5ced1d82006-04-06 23:23:56 +00004210/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004211/// is promoted to a vector. It also returns the LoadSDNode by reference if
4212/// required.
4213static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004214 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4215 return false;
4216 N = N->getOperand(0).getNode();
4217 if (!ISD::isNON_EXTLoad(N))
4218 return false;
4219 if (LD)
4220 *LD = cast<LoadSDNode>(N);
4221 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004222}
4223
Dan Gohman65fd6562011-11-03 21:49:52 +00004224// Test whether the given value is a vector value which will be legalized
4225// into a load.
4226static bool WillBeConstantPoolLoad(SDNode *N) {
4227 if (N->getOpcode() != ISD::BUILD_VECTOR)
4228 return false;
4229
4230 // Check for any non-constant elements.
4231 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4232 switch (N->getOperand(i).getNode()->getOpcode()) {
4233 case ISD::UNDEF:
4234 case ISD::ConstantFP:
4235 case ISD::Constant:
4236 break;
4237 default:
4238 return false;
4239 }
4240
4241 // Vectors of all-zeros and all-ones are materialized with special
4242 // instructions rather than being loaded.
4243 return !ISD::isBuildVectorAllZeros(N) &&
4244 !ISD::isBuildVectorAllOnes(N);
4245}
4246
Evan Cheng533a0aa2006-04-19 20:35:22 +00004247/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4248/// match movlp{s|d}. The lower half elements should come from lower half of
4249/// V1 (and in order), and the upper half elements should come from the upper
4250/// half of V2 (and in order). And since V1 will become the source of the
4251/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004252static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4253 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004254 EVT VT = Op->getValueType(0);
4255 if (VT.getSizeInBits() != 128)
4256 return false;
4257
Evan Cheng466685d2006-10-09 20:57:25 +00004258 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004259 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004260 // Is V2 is a vector load, don't do this transformation. We will try to use
4261 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004262 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004263 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004264
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004265 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004266
Evan Cheng533a0aa2006-04-19 20:35:22 +00004267 if (NumElems != 2 && NumElems != 4)
4268 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004269 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004270 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004271 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004272 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004273 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004274 return false;
4275 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004276}
4277
Evan Cheng39623da2006-04-20 08:58:49 +00004278/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4279/// all the same.
4280static bool isSplatVector(SDNode *N) {
4281 if (N->getOpcode() != ISD::BUILD_VECTOR)
4282 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004283
Dan Gohman475871a2008-07-27 21:46:04 +00004284 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004285 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4286 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004287 return false;
4288 return true;
4289}
4290
Evan Cheng213d2cf2007-05-17 18:45:50 +00004291/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004292/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004293/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004294static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004295 SDValue V1 = N->getOperand(0);
4296 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004297 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4298 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004299 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004300 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004301 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004302 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4303 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004304 if (Opc != ISD::BUILD_VECTOR ||
4305 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004306 return false;
4307 } else if (Idx >= 0) {
4308 unsigned Opc = V1.getOpcode();
4309 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4310 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004311 if (Opc != ISD::BUILD_VECTOR ||
4312 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004313 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004314 }
4315 }
4316 return true;
4317}
4318
4319/// getZeroVector - Returns a vector of specified type with all zero elements.
4320///
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004321static SDValue getZeroVector(EVT VT, bool HasXMMInt, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004322 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004323 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004324
Dale Johannesen0488fb62010-09-30 23:57:10 +00004325 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004326 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004327 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004328 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004329 if (HasXMMInt) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004330 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4331 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4332 } else { // SSE1
4333 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4334 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4335 }
4336 } else if (VT.getSizeInBits() == 256) { // AVX
4337 // 256-bit logic and arithmetic instructions in AVX are
4338 // all floating-point, no support for integer ops. Default
4339 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004340 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004341 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4342 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004343 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004344 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004345}
4346
Chris Lattner8a594482007-11-25 00:24:49 +00004347/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004348/// Always build ones vectors as <4 x i32>. For 256-bit types, use two
4349/// <4 x i32> inserted in a <8 x i32> appropriately. Then bitcast to their
4350/// original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00004351static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004352 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004353 assert((VT.is128BitVector() || VT.is256BitVector())
4354 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004355
Owen Anderson825b72b2009-08-11 20:47:22 +00004356 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004357 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4358 Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004359
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004360 if (VT.is256BitVector()) {
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004361 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4362 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4363 Vec = Insert128BitVector(InsV, Vec,
4364 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4365 }
4366
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004367 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004368}
4369
Evan Cheng39623da2006-04-20 08:58:49 +00004370/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4371/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004372static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004373 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004374 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004375
Evan Cheng39623da2006-04-20 08:58:49 +00004376 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004377 SmallVector<int, 8> MaskVec;
4378 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004379
Nate Begeman5a5ca152009-04-29 05:20:52 +00004380 for (unsigned i = 0; i != NumElems; ++i) {
4381 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004382 MaskVec[i] = NumElems;
4383 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004384 }
Evan Cheng39623da2006-04-20 08:58:49 +00004385 }
Evan Cheng39623da2006-04-20 08:58:49 +00004386 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004387 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4388 SVOp->getOperand(1), &MaskVec[0]);
4389 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004390}
4391
Evan Cheng017dcc62006-04-21 01:05:10 +00004392/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4393/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004394static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004395 SDValue V2) {
4396 unsigned NumElems = VT.getVectorNumElements();
4397 SmallVector<int, 8> Mask;
4398 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004399 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004400 Mask.push_back(i);
4401 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004402}
4403
Nate Begeman9008ca62009-04-27 18:41:29 +00004404/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004405static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004406 SDValue V2) {
4407 unsigned NumElems = VT.getVectorNumElements();
4408 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004409 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004410 Mask.push_back(i);
4411 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004412 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004413 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004414}
4415
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004416/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004417static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004418 SDValue V2) {
4419 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004420 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004421 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004422 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004423 Mask.push_back(i + Half);
4424 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004425 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004426 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004427}
4428
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004429// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004430// a generic shuffle instruction because the target has no such instructions.
4431// Generate shuffles which repeat i16 and i8 several times until they can be
4432// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004433static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004434 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004435 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004436 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004437
Nate Begeman9008ca62009-04-27 18:41:29 +00004438 while (NumElems > 4) {
4439 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004440 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004441 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004442 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004443 EltNo -= NumElems/2;
4444 }
4445 NumElems >>= 1;
4446 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004447 return V;
4448}
Eric Christopherfd179292009-08-27 18:07:15 +00004449
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004450/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4451static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4452 EVT VT = V.getValueType();
4453 DebugLoc dl = V.getDebugLoc();
4454 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4455 && "Vector size not supported");
4456
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004457 if (VT.getSizeInBits() == 128) {
4458 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004459 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004460 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4461 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004462 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004463 // To use VPERMILPS to splat scalars, the second half of indicies must
4464 // refer to the higher part, which is a duplication of the lower one,
4465 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004466 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4467 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004468
4469 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4470 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4471 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004472 }
4473
4474 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4475}
4476
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004477/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004478static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4479 EVT SrcVT = SV->getValueType(0);
4480 SDValue V1 = SV->getOperand(0);
4481 DebugLoc dl = SV->getDebugLoc();
4482
4483 int EltNo = SV->getSplatIndex();
4484 int NumElems = SrcVT.getVectorNumElements();
4485 unsigned Size = SrcVT.getSizeInBits();
4486
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004487 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4488 "Unknown how to promote splat for type");
4489
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004490 // Extract the 128-bit part containing the splat element and update
4491 // the splat element index when it refers to the higher register.
4492 if (Size == 256) {
4493 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4494 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4495 if (Idx > 0)
4496 EltNo -= NumElems/2;
4497 }
4498
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004499 // All i16 and i8 vector types can't be used directly by a generic shuffle
4500 // instruction because the target has no such instruction. Generate shuffles
4501 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004502 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004503 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004504 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004505 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004506
4507 // Recreate the 256-bit vector and place the same 128-bit vector
4508 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004509 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004510 if (Size == 256) {
4511 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4512 DAG.getConstant(0, MVT::i32), DAG, dl);
4513 V1 = Insert128BitVector(InsV, V1,
4514 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4515 }
4516
4517 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004518}
4519
Evan Chengba05f722006-04-21 23:03:30 +00004520/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004521/// vector of zero or undef vector. This produces a shuffle where the low
4522/// element of V2 is swizzled into the zero/undef vector, landing at element
4523/// 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 +00004524static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004525 bool isZero, bool HasXMMInt,
4526 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004527 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004528 SDValue V1 = isZero
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004529 ? getZeroVector(VT, HasXMMInt, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004530 unsigned NumElems = VT.getVectorNumElements();
4531 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004532 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004533 // If this is the insertion idx, put the low elt of V2 here.
4534 MaskVec.push_back(i == Idx ? NumElems : i);
4535 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004536}
4537
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004538/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4539/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004540static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4541 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004542 if (Depth == 6)
4543 return SDValue(); // Limit search depth.
4544
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004545 SDValue V = SDValue(N, 0);
4546 EVT VT = V.getValueType();
4547 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004548
4549 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4550 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4551 Index = SV->getMaskElt(Index);
4552
4553 if (Index < 0)
4554 return DAG.getUNDEF(VT.getVectorElementType());
4555
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004556 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004557 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004558 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004559 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004560
4561 // Recurse into target specific vector shuffles to find scalars.
4562 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004563 int NumElems = VT.getVectorNumElements();
4564 SmallVector<unsigned, 16> ShuffleMask;
4565 SDValue ImmN;
4566
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004567 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004568 case X86ISD::SHUFPS:
4569 case X86ISD::SHUFPD:
4570 ImmN = N->getOperand(N->getNumOperands()-1);
4571 DecodeSHUFPSMask(NumElems,
4572 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4573 ShuffleMask);
4574 break;
4575 case X86ISD::PUNPCKHBW:
4576 case X86ISD::PUNPCKHWD:
4577 case X86ISD::PUNPCKHDQ:
4578 case X86ISD::PUNPCKHQDQ:
4579 DecodePUNPCKHMask(NumElems, ShuffleMask);
4580 break;
4581 case X86ISD::UNPCKHPS:
4582 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00004583 case X86ISD::VUNPCKHPSY:
4584 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004585 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4586 break;
4587 case X86ISD::PUNPCKLBW:
4588 case X86ISD::PUNPCKLWD:
4589 case X86ISD::PUNPCKLDQ:
4590 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004591 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004592 break;
4593 case X86ISD::UNPCKLPS:
4594 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004595 case X86ISD::VUNPCKLPSY:
4596 case X86ISD::VUNPCKLPDY:
4597 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004598 break;
4599 case X86ISD::MOVHLPS:
4600 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4601 break;
4602 case X86ISD::MOVLHPS:
4603 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4604 break;
4605 case X86ISD::PSHUFD:
4606 ImmN = N->getOperand(N->getNumOperands()-1);
4607 DecodePSHUFMask(NumElems,
4608 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4609 ShuffleMask);
4610 break;
4611 case X86ISD::PSHUFHW:
4612 ImmN = N->getOperand(N->getNumOperands()-1);
4613 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4614 ShuffleMask);
4615 break;
4616 case X86ISD::PSHUFLW:
4617 ImmN = N->getOperand(N->getNumOperands()-1);
4618 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4619 ShuffleMask);
4620 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004621 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004622 case X86ISD::MOVSD: {
4623 // The index 0 always comes from the first element of the second source,
4624 // this is why MOVSS and MOVSD are used in the first place. The other
4625 // elements come from the other positions of the first source vector.
4626 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004627 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4628 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004629 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004630 case X86ISD::VPERMILPS:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004631 ImmN = N->getOperand(N->getNumOperands()-1);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004632 DecodeVPERMILPSMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004633 ShuffleMask);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004634 break;
4635 case X86ISD::VPERMILPSY:
4636 ImmN = N->getOperand(N->getNumOperands()-1);
4637 DecodeVPERMILPSMask(8, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4638 ShuffleMask);
4639 break;
4640 case X86ISD::VPERMILPD:
4641 ImmN = N->getOperand(N->getNumOperands()-1);
4642 DecodeVPERMILPDMask(2, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4643 ShuffleMask);
4644 break;
4645 case X86ISD::VPERMILPDY:
4646 ImmN = N->getOperand(N->getNumOperands()-1);
4647 DecodeVPERMILPDMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4648 ShuffleMask);
4649 break;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004650 case X86ISD::VPERM2F128:
4651 ImmN = N->getOperand(N->getNumOperands()-1);
4652 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4653 ShuffleMask);
4654 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004655 case X86ISD::MOVDDUP:
4656 case X86ISD::MOVLHPD:
4657 case X86ISD::MOVLPD:
4658 case X86ISD::MOVLPS:
4659 case X86ISD::MOVSHDUP:
4660 case X86ISD::MOVSLDUP:
4661 case X86ISD::PALIGN:
4662 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004663 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004664 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004665 return SDValue();
4666 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004667
4668 Index = ShuffleMask[Index];
4669 if (Index < 0)
4670 return DAG.getUNDEF(VT.getVectorElementType());
4671
4672 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4673 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4674 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004675 }
4676
4677 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004678 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004679 V = V.getOperand(0);
4680 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004681 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004682
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004683 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004684 return SDValue();
4685 }
4686
4687 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4688 return (Index == 0) ? V.getOperand(0)
4689 : DAG.getUNDEF(VT.getVectorElementType());
4690
4691 if (V.getOpcode() == ISD::BUILD_VECTOR)
4692 return V.getOperand(Index);
4693
4694 return SDValue();
4695}
4696
4697/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4698/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004699/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004700static
4701unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4702 bool ZerosFromLeft, SelectionDAG &DAG) {
4703 int i = 0;
4704
4705 while (i < NumElems) {
4706 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004707 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004708 if (!(Elt.getNode() &&
4709 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4710 break;
4711 ++i;
4712 }
4713
4714 return i;
4715}
4716
4717/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4718/// MaskE correspond consecutively to elements from one of the vector operands,
4719/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4720static
4721bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4722 int OpIdx, int NumElems, unsigned &OpNum) {
4723 bool SeenV1 = false;
4724 bool SeenV2 = false;
4725
4726 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4727 int Idx = SVOp->getMaskElt(i);
4728 // Ignore undef indicies
4729 if (Idx < 0)
4730 continue;
4731
4732 if (Idx < NumElems)
4733 SeenV1 = true;
4734 else
4735 SeenV2 = true;
4736
4737 // Only accept consecutive elements from the same vector
4738 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4739 return false;
4740 }
4741
4742 OpNum = SeenV1 ? 0 : 1;
4743 return true;
4744}
4745
4746/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4747/// logical left shift of a vector.
4748static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4749 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4750 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4751 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4752 false /* check zeros from right */, DAG);
4753 unsigned OpSrc;
4754
4755 if (!NumZeros)
4756 return false;
4757
4758 // Considering the elements in the mask that are not consecutive zeros,
4759 // check if they consecutively come from only one of the source vectors.
4760 //
4761 // V1 = {X, A, B, C} 0
4762 // \ \ \ /
4763 // vector_shuffle V1, V2 <1, 2, 3, X>
4764 //
4765 if (!isShuffleMaskConsecutive(SVOp,
4766 0, // Mask Start Index
4767 NumElems-NumZeros-1, // Mask End Index
4768 NumZeros, // Where to start looking in the src vector
4769 NumElems, // Number of elements in vector
4770 OpSrc)) // Which source operand ?
4771 return false;
4772
4773 isLeft = false;
4774 ShAmt = NumZeros;
4775 ShVal = SVOp->getOperand(OpSrc);
4776 return true;
4777}
4778
4779/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4780/// logical left shift of a vector.
4781static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4782 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4783 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4784 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4785 true /* check zeros from left */, DAG);
4786 unsigned OpSrc;
4787
4788 if (!NumZeros)
4789 return false;
4790
4791 // Considering the elements in the mask that are not consecutive zeros,
4792 // check if they consecutively come from only one of the source vectors.
4793 //
4794 // 0 { A, B, X, X } = V2
4795 // / \ / /
4796 // vector_shuffle V1, V2 <X, X, 4, 5>
4797 //
4798 if (!isShuffleMaskConsecutive(SVOp,
4799 NumZeros, // Mask Start Index
4800 NumElems-1, // Mask End Index
4801 0, // Where to start looking in the src vector
4802 NumElems, // Number of elements in vector
4803 OpSrc)) // Which source operand ?
4804 return false;
4805
4806 isLeft = true;
4807 ShAmt = NumZeros;
4808 ShVal = SVOp->getOperand(OpSrc);
4809 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004810}
4811
4812/// isVectorShift - Returns true if the shuffle can be implemented as a
4813/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004814static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004815 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004816 // Although the logic below support any bitwidth size, there are no
4817 // shift instructions which handle more than 128-bit vectors.
4818 if (SVOp->getValueType(0).getSizeInBits() > 128)
4819 return false;
4820
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004821 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4822 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4823 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004824
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004825 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004826}
4827
Evan Chengc78d3b42006-04-24 18:01:45 +00004828/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4829///
Dan Gohman475871a2008-07-27 21:46:04 +00004830static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004831 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004832 SelectionDAG &DAG,
4833 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004834 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004835 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004836
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004837 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004838 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004839 bool First = true;
4840 for (unsigned i = 0; i < 16; ++i) {
4841 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4842 if (ThisIsNonZero && First) {
4843 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004844 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004845 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004846 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004847 First = false;
4848 }
4849
4850 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004851 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004852 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4853 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004854 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004855 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004856 }
4857 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004858 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4859 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4860 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004861 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004862 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004863 } else
4864 ThisElt = LastElt;
4865
Gabor Greifba36cb52008-08-28 21:40:38 +00004866 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004867 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004868 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004869 }
4870 }
4871
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004872 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004873}
4874
Bill Wendlinga348c562007-03-22 18:42:45 +00004875/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004876///
Dan Gohman475871a2008-07-27 21:46:04 +00004877static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004878 unsigned NumNonZero, unsigned NumZero,
4879 SelectionDAG &DAG,
4880 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004881 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004882 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004883
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004884 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004885 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004886 bool First = true;
4887 for (unsigned i = 0; i < 8; ++i) {
4888 bool isNonZero = (NonZeros & (1 << i)) != 0;
4889 if (isNonZero) {
4890 if (First) {
4891 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004892 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004893 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004894 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004895 First = false;
4896 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004897 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004898 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004899 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004900 }
4901 }
4902
4903 return V;
4904}
4905
Evan Chengf26ffe92008-05-29 08:22:04 +00004906/// getVShift - Return a vector logical shift node.
4907///
Owen Andersone50ed302009-08-10 22:56:29 +00004908static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004909 unsigned NumBits, SelectionDAG &DAG,
4910 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004911 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004912 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004913 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004914 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4915 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004916 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004917 DAG.getConstant(NumBits,
4918 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004919}
4920
Dan Gohman475871a2008-07-27 21:46:04 +00004921SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004922X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004923 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004924
Evan Chengc3630942009-12-09 21:00:30 +00004925 // Check if the scalar load can be widened into a vector load. And if
4926 // the address is "base + cst" see if the cst can be "absorbed" into
4927 // the shuffle mask.
4928 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4929 SDValue Ptr = LD->getBasePtr();
4930 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4931 return SDValue();
4932 EVT PVT = LD->getValueType(0);
4933 if (PVT != MVT::i32 && PVT != MVT::f32)
4934 return SDValue();
4935
4936 int FI = -1;
4937 int64_t Offset = 0;
4938 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4939 FI = FINode->getIndex();
4940 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004941 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004942 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4943 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4944 Offset = Ptr.getConstantOperandVal(1);
4945 Ptr = Ptr.getOperand(0);
4946 } else {
4947 return SDValue();
4948 }
4949
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004950 // FIXME: 256-bit vector instructions don't require a strict alignment,
4951 // improve this code to support it better.
4952 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004953 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004954 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004955 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004956 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004957 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004958 // Can't change the alignment. FIXME: It's possible to compute
4959 // the exact stack offset and reference FI + adjust offset instead.
4960 // If someone *really* cares about this. That's the way to implement it.
4961 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004962 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004963 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004964 }
4965 }
4966
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004967 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004968 // Ptr + (Offset & ~15).
4969 if (Offset < 0)
4970 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004971 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004972 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004973 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004974 if (StartOffset)
4975 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4976 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4977
4978 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004979 int NumElems = VT.getVectorNumElements();
4980
4981 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4982 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4983 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004984 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004985 false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004986
4987 // Canonicalize it to a v4i32 or v8i32 shuffle.
4988 SmallVector<int, 8> Mask;
4989 for (int i = 0; i < NumElems; ++i)
4990 Mask.push_back(EltNo);
4991
4992 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4993 return DAG.getNode(ISD::BITCAST, dl, NVT,
4994 DAG.getVectorShuffle(CanonVT, dl, V1,
4995 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004996 }
4997
4998 return SDValue();
4999}
5000
Michael J. Spencerec38de22010-10-10 22:04:20 +00005001/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5002/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005003/// load which has the same value as a build_vector whose operands are 'elts'.
5004///
5005/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005006///
Nate Begeman1449f292010-03-24 22:19:06 +00005007/// FIXME: we'd also like to handle the case where the last elements are zero
5008/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5009/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005010static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005011 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005012 EVT EltVT = VT.getVectorElementType();
5013 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005014
Nate Begemanfdea31a2010-03-24 20:49:50 +00005015 LoadSDNode *LDBase = NULL;
5016 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005017
Nate Begeman1449f292010-03-24 22:19:06 +00005018 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005019 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005020 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005021 for (unsigned i = 0; i < NumElems; ++i) {
5022 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005023
Nate Begemanfdea31a2010-03-24 20:49:50 +00005024 if (!Elt.getNode() ||
5025 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5026 return SDValue();
5027 if (!LDBase) {
5028 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5029 return SDValue();
5030 LDBase = cast<LoadSDNode>(Elt.getNode());
5031 LastLoadedElt = i;
5032 continue;
5033 }
5034 if (Elt.getOpcode() == ISD::UNDEF)
5035 continue;
5036
5037 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5038 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5039 return SDValue();
5040 LastLoadedElt = i;
5041 }
Nate Begeman1449f292010-03-24 22:19:06 +00005042
5043 // If we have found an entire vector of loads and undefs, then return a large
5044 // load of the entire vector width starting at the base pointer. If we found
5045 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005046 if (LastLoadedElt == NumElems - 1) {
5047 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005048 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005049 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005050 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005051 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005052 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005053 LDBase->isVolatile(), LDBase->isNonTemporal(),
5054 LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00005055 } else if (NumElems == 4 && LastLoadedElt == 1 &&
5056 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005057 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5058 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005059 SDValue ResNode =
5060 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5061 LDBase->getPointerInfo(),
5062 LDBase->getAlignment(),
5063 false/*isVolatile*/, true/*ReadMem*/,
5064 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005065 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005066 }
5067 return SDValue();
5068}
5069
Evan Chengc3630942009-12-09 21:00:30 +00005070SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005071X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005072 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005073
David Greenef125a292011-02-08 19:04:41 +00005074 EVT VT = Op.getValueType();
5075 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005076 unsigned NumElems = Op.getNumOperands();
5077
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005078 // Vectors containing all zeros can be matched by pxor and xorps later
5079 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5080 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5081 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005082 if (Op.getValueType() == MVT::v4i32 ||
5083 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005084 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005085
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005086 return getZeroVector(Op.getValueType(), Subtarget->hasXMMInt(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005087 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005088
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005089 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
5090 // vectors or broken into v4i32 operations on 256-bit vectors.
5091 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
5092 if (Op.getValueType() == MVT::v4i32)
5093 return Op;
5094
5095 return getOnesVector(Op.getValueType(), DAG, dl);
5096 }
5097
Owen Andersone50ed302009-08-10 22:56:29 +00005098 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005099
Evan Cheng0db9fe62006-04-25 20:13:52 +00005100 unsigned NumZero = 0;
5101 unsigned NumNonZero = 0;
5102 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005103 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005104 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005105 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005106 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005107 if (Elt.getOpcode() == ISD::UNDEF)
5108 continue;
5109 Values.insert(Elt);
5110 if (Elt.getOpcode() != ISD::Constant &&
5111 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005112 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005113 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005114 NumZero++;
5115 else {
5116 NonZeros |= (1 << i);
5117 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005118 }
5119 }
5120
Chris Lattner97a2a562010-08-26 05:24:29 +00005121 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5122 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005123 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005124
Chris Lattner67f453a2008-03-09 05:42:06 +00005125 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005126 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005127 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005128 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005129
Chris Lattner62098042008-03-09 01:05:04 +00005130 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5131 // the value are obviously zero, truncate the value to i32 and do the
5132 // insertion that way. Only do this if the value is non-constant or if the
5133 // value is a constant being inserted into element 0. It is cheaper to do
5134 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005135 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005136 (!IsAllConstants || Idx == 0)) {
5137 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005138 // Handle SSE only.
5139 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5140 EVT VecVT = MVT::v4i32;
5141 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005142
Chris Lattner62098042008-03-09 01:05:04 +00005143 // Truncate the value (which may itself be a constant) to i32, and
5144 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005145 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005146 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005147 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005148 Subtarget->hasXMMInt(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005149
Chris Lattner62098042008-03-09 01:05:04 +00005150 // Now we have our 32-bit value zero extended in the low element of
5151 // a vector. If Idx != 0, swizzle it into place.
5152 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005153 SmallVector<int, 4> Mask;
5154 Mask.push_back(Idx);
5155 for (unsigned i = 1; i != VecElts; ++i)
5156 Mask.push_back(i);
5157 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005158 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005159 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005160 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005161 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005162 }
5163 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005164
Chris Lattner19f79692008-03-08 22:59:52 +00005165 // If we have a constant or non-constant insertion into the low element of
5166 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5167 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005168 // depending on what the source datatype is.
5169 if (Idx == 0) {
5170 if (NumZero == 0) {
5171 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005172 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5173 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005174 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5175 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005176 return getShuffleVectorZeroOrUndef(Item, 0, true,Subtarget->hasXMMInt(),
Eli Friedman10415532009-06-06 06:05:10 +00005177 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005178 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5179 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005180 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5181 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005182 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5183 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005184 Subtarget->hasXMMInt(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005185 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005186 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005187 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005188
5189 // Is it a vector logical left shift?
5190 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005191 X86::isZeroNode(Op.getOperand(0)) &&
5192 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005193 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005194 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005195 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005196 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005197 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005198 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005199
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005200 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005201 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005202
Chris Lattner19f79692008-03-08 22:59:52 +00005203 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5204 // is a non-constant being inserted into an element other than the low one,
5205 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5206 // movd/movss) to move this into the low element, then shuffle it into
5207 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005208 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005209 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005210
Evan Cheng0db9fe62006-04-25 20:13:52 +00005211 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005212 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005213 Subtarget->hasXMMInt(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005214 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005215 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005216 MaskVec.push_back(i == Idx ? 0 : 1);
5217 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005218 }
5219 }
5220
Chris Lattner67f453a2008-03-09 05:42:06 +00005221 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005222 if (Values.size() == 1) {
5223 if (EVTBits == 32) {
5224 // Instead of a shuffle like this:
5225 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5226 // Check if it's possible to issue this instead.
5227 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5228 unsigned Idx = CountTrailingZeros_32(NonZeros);
5229 SDValue Item = Op.getOperand(Idx);
5230 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5231 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5232 }
Dan Gohman475871a2008-07-27 21:46:04 +00005233 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005234 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005235
Dan Gohmana3941172007-07-24 22:55:08 +00005236 // A vector full of immediates; various special cases are already
5237 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005238 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005239 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005240
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005241 // For AVX-length vectors, build the individual 128-bit pieces and use
5242 // shuffles to put them in place.
5243 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5244 SmallVector<SDValue, 32> V;
5245 for (unsigned i = 0; i < NumElems; ++i)
5246 V.push_back(Op.getOperand(i));
5247
5248 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5249
5250 // Build both the lower and upper subvector.
5251 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5252 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5253 NumElems/2);
5254
5255 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005256 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5257 DAG.getConstant(0, MVT::i32), DAG, dl);
5258 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005259 DAG, dl);
5260 }
5261
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005262 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005263 if (EVTBits == 64) {
5264 if (NumNonZero == 1) {
5265 // One half is zero or undef.
5266 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005267 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005268 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005269 return getShuffleVectorZeroOrUndef(V2, Idx, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005270 Subtarget->hasXMMInt(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005271 }
Dan Gohman475871a2008-07-27 21:46:04 +00005272 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005273 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005274
5275 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005276 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005277 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005278 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005279 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005280 }
5281
Bill Wendling826f36f2007-03-28 00:57:11 +00005282 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005283 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005284 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005285 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005286 }
5287
5288 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005289 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005290 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005291 if (NumElems == 4 && NumZero > 0) {
5292 for (unsigned i = 0; i < 4; ++i) {
5293 bool isZero = !(NonZeros & (1 << i));
5294 if (isZero)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005295 V[i] = getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005296 else
Dale Johannesenace16102009-02-03 19:33:06 +00005297 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005298 }
5299
5300 for (unsigned i = 0; i < 2; ++i) {
5301 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5302 default: break;
5303 case 0:
5304 V[i] = V[i*2]; // Must be a zero vector.
5305 break;
5306 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005307 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005308 break;
5309 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005310 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005311 break;
5312 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005313 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005314 break;
5315 }
5316 }
5317
Nate Begeman9008ca62009-04-27 18:41:29 +00005318 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005319 bool Reverse = (NonZeros & 0x3) == 2;
5320 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005321 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005322 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5323 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005324 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5325 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005326 }
5327
Nate Begemanfdea31a2010-03-24 20:49:50 +00005328 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5329 // Check for a build vector of consecutive loads.
5330 for (unsigned i = 0; i < NumElems; ++i)
5331 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005332
Nate Begemanfdea31a2010-03-24 20:49:50 +00005333 // Check for elements which are consecutive loads.
5334 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5335 if (LD.getNode())
5336 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005337
5338 // For SSE 4.1, use insertps to put the high elements into the low element.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005339 if (getSubtarget()->hasSSE41() || getSubtarget()->hasAVX()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005340 SDValue Result;
5341 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5342 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5343 else
5344 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005345
Chris Lattner24faf612010-08-28 17:59:08 +00005346 for (unsigned i = 1; i < NumElems; ++i) {
5347 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5348 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005349 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005350 }
5351 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005352 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005353
Chris Lattner6e80e442010-08-28 17:15:43 +00005354 // Otherwise, expand into a number of unpckl*, start by extending each of
5355 // our (non-undef) elements to the full vector width with the element in the
5356 // bottom slot of the vector (which generates no code for SSE).
5357 for (unsigned i = 0; i < NumElems; ++i) {
5358 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5359 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5360 else
5361 V[i] = DAG.getUNDEF(VT);
5362 }
5363
5364 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005365 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5366 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5367 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005368 unsigned EltStride = NumElems >> 1;
5369 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005370 for (unsigned i = 0; i < EltStride; ++i) {
5371 // If V[i+EltStride] is undef and this is the first round of mixing,
5372 // then it is safe to just drop this shuffle: V[i] is already in the
5373 // right place, the one element (since it's the first round) being
5374 // inserted as undef can be dropped. This isn't safe for successive
5375 // rounds because they will permute elements within both vectors.
5376 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5377 EltStride == NumElems/2)
5378 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005379
Chris Lattner6e80e442010-08-28 17:15:43 +00005380 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005381 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005382 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005383 }
5384 return V[0];
5385 }
Dan Gohman475871a2008-07-27 21:46:04 +00005386 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005387}
5388
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005389// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5390// them in a MMX register. This is better than doing a stack convert.
5391static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005392 DebugLoc dl = Op.getDebugLoc();
5393 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005394
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005395 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5396 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5397 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005398 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005399 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5400 InVec = Op.getOperand(1);
5401 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5402 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005403 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005404 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5405 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5406 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005407 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005408 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5409 Mask[0] = 0; Mask[1] = 2;
5410 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5411 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005412 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005413}
5414
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005415// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5416// to create 256-bit vectors from two other 128-bit ones.
5417static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5418 DebugLoc dl = Op.getDebugLoc();
5419 EVT ResVT = Op.getValueType();
5420
5421 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5422
5423 SDValue V1 = Op.getOperand(0);
5424 SDValue V2 = Op.getOperand(1);
5425 unsigned NumElems = ResVT.getVectorNumElements();
5426
5427 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5428 DAG.getConstant(0, MVT::i32), DAG, dl);
5429 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5430 DAG, dl);
5431}
5432
5433SDValue
5434X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005435 EVT ResVT = Op.getValueType();
5436
5437 assert(Op.getNumOperands() == 2);
5438 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5439 "Unsupported CONCAT_VECTORS for value type");
5440
5441 // We support concatenate two MMX registers and place them in a MMX register.
5442 // This is better than doing a stack convert.
5443 if (ResVT.is128BitVector())
5444 return LowerMMXCONCAT_VECTORS(Op, DAG);
5445
5446 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5447 // from two other 128-bit ones.
5448 return LowerAVXCONCAT_VECTORS(Op, DAG);
5449}
5450
Nate Begemanb9a47b82009-02-23 08:49:38 +00005451// v8i16 shuffles - Prefer shuffles in the following order:
5452// 1. [all] pshuflw, pshufhw, optional move
5453// 2. [ssse3] 1 x pshufb
5454// 3. [ssse3] 2 x pshufb + 1 x por
5455// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005456SDValue
5457X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5458 SelectionDAG &DAG) const {
5459 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005460 SDValue V1 = SVOp->getOperand(0);
5461 SDValue V2 = SVOp->getOperand(1);
5462 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005463 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005464
Nate Begemanb9a47b82009-02-23 08:49:38 +00005465 // Determine if more than 1 of the words in each of the low and high quadwords
5466 // of the result come from the same quadword of one of the two inputs. Undef
5467 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005468 unsigned LoQuad[] = { 0, 0, 0, 0 };
5469 unsigned HiQuad[] = { 0, 0, 0, 0 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005470 BitVector InputQuads(4);
5471 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005472 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005473 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005474 MaskVals.push_back(EltIdx);
5475 if (EltIdx < 0) {
5476 ++Quad[0];
5477 ++Quad[1];
5478 ++Quad[2];
5479 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005480 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005481 }
5482 ++Quad[EltIdx / 4];
5483 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005484 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005485
Nate Begemanb9a47b82009-02-23 08:49:38 +00005486 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005487 unsigned MaxQuad = 1;
5488 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005489 if (LoQuad[i] > MaxQuad) {
5490 BestLoQuad = i;
5491 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005492 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005493 }
5494
Nate Begemanb9a47b82009-02-23 08:49:38 +00005495 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005496 MaxQuad = 1;
5497 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005498 if (HiQuad[i] > MaxQuad) {
5499 BestHiQuad = i;
5500 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005501 }
5502 }
5503
Nate Begemanb9a47b82009-02-23 08:49:38 +00005504 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005505 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005506 // single pshufb instruction is necessary. If There are more than 2 input
5507 // quads, disable the next transformation since it does not help SSSE3.
5508 bool V1Used = InputQuads[0] || InputQuads[1];
5509 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005510 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005511 if (InputQuads.count() == 2 && V1Used && V2Used) {
5512 BestLoQuad = InputQuads.find_first();
5513 BestHiQuad = InputQuads.find_next(BestLoQuad);
5514 }
5515 if (InputQuads.count() > 2) {
5516 BestLoQuad = -1;
5517 BestHiQuad = -1;
5518 }
5519 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005520
Nate Begemanb9a47b82009-02-23 08:49:38 +00005521 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5522 // the shuffle mask. If a quad is scored as -1, that means that it contains
5523 // words from all 4 input quadwords.
5524 SDValue NewV;
5525 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005526 SmallVector<int, 8> MaskV;
5527 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5528 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005529 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005530 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5531 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5532 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005533
Nate Begemanb9a47b82009-02-23 08:49:38 +00005534 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5535 // source words for the shuffle, to aid later transformations.
5536 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005537 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005538 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005539 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005540 if (idx != (int)i)
5541 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005542 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005543 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005544 AllWordsInNewV = false;
5545 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005546 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005547
Nate Begemanb9a47b82009-02-23 08:49:38 +00005548 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5549 if (AllWordsInNewV) {
5550 for (int i = 0; i != 8; ++i) {
5551 int idx = MaskVals[i];
5552 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005553 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005554 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005555 if ((idx != i) && idx < 4)
5556 pshufhw = false;
5557 if ((idx != i) && idx > 3)
5558 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005559 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005560 V1 = NewV;
5561 V2Used = false;
5562 BestLoQuad = 0;
5563 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005564 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005565
Nate Begemanb9a47b82009-02-23 08:49:38 +00005566 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5567 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005568 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005569 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5570 unsigned TargetMask = 0;
5571 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005572 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005573 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5574 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5575 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005576 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005577 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005578 }
Eric Christopherfd179292009-08-27 18:07:15 +00005579
Nate Begemanb9a47b82009-02-23 08:49:38 +00005580 // If we have SSSE3, and all words of the result are from 1 input vector,
5581 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5582 // is present, fall back to case 4.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005583 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005584 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005585
Nate Begemanb9a47b82009-02-23 08:49:38 +00005586 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005587 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005588 // mask, and elements that come from V1 in the V2 mask, so that the two
5589 // results can be OR'd together.
5590 bool TwoInputs = V1Used && V2Used;
5591 for (unsigned i = 0; i != 8; ++i) {
5592 int EltIdx = MaskVals[i] * 2;
5593 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005594 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5595 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005596 continue;
5597 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005598 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5599 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005600 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005601 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005602 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005603 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005604 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005605 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005606 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005607
Nate Begemanb9a47b82009-02-23 08:49:38 +00005608 // Calculate the shuffle mask for the second input, shuffle it, and
5609 // OR it with the first shuffled input.
5610 pshufbMask.clear();
5611 for (unsigned i = 0; i != 8; ++i) {
5612 int EltIdx = MaskVals[i] * 2;
5613 if (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 - 16, MVT::i8));
5619 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005620 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005621 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005622 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
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));
5625 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005626 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005627 }
5628
5629 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5630 // and update MaskVals with new element order.
5631 BitVector InOrder(8);
5632 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005633 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005634 for (int i = 0; i != 4; ++i) {
5635 int idx = MaskVals[i];
5636 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005637 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005638 InOrder.set(i);
5639 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005640 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005641 InOrder.set(i);
5642 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005643 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005644 }
5645 }
5646 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005647 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005648 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005649 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005650
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005651 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5652 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005653 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5654 NewV.getOperand(0),
5655 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5656 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005657 }
Eric Christopherfd179292009-08-27 18:07:15 +00005658
Nate Begemanb9a47b82009-02-23 08:49:38 +00005659 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5660 // and update MaskVals with the new element order.
5661 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005662 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005663 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005664 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005665 for (unsigned i = 4; i != 8; ++i) {
5666 int idx = MaskVals[i];
5667 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005668 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005669 InOrder.set(i);
5670 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005671 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005672 InOrder.set(i);
5673 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005674 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005675 }
5676 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005677 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005678 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005679
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005680 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5681 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005682 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5683 NewV.getOperand(0),
5684 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5685 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005686 }
Eric Christopherfd179292009-08-27 18:07:15 +00005687
Nate Begemanb9a47b82009-02-23 08:49:38 +00005688 // In case BestHi & BestLo were both -1, which means each quadword has a word
5689 // from each of the four input quadwords, calculate the InOrder bitvector now
5690 // before falling through to the insert/extract cleanup.
5691 if (BestLoQuad == -1 && BestHiQuad == -1) {
5692 NewV = V1;
5693 for (int i = 0; i != 8; ++i)
5694 if (MaskVals[i] < 0 || MaskVals[i] == i)
5695 InOrder.set(i);
5696 }
Eric Christopherfd179292009-08-27 18:07:15 +00005697
Nate Begemanb9a47b82009-02-23 08:49:38 +00005698 // The other elements are put in the right place using pextrw and pinsrw.
5699 for (unsigned i = 0; i != 8; ++i) {
5700 if (InOrder[i])
5701 continue;
5702 int EltIdx = MaskVals[i];
5703 if (EltIdx < 0)
5704 continue;
5705 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005706 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005707 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005708 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005709 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005710 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005711 DAG.getIntPtrConstant(i));
5712 }
5713 return NewV;
5714}
5715
5716// v16i8 shuffles - Prefer shuffles in the following order:
5717// 1. [ssse3] 1 x pshufb
5718// 2. [ssse3] 2 x pshufb + 1 x por
5719// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5720static
Nate Begeman9008ca62009-04-27 18:41:29 +00005721SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005722 SelectionDAG &DAG,
5723 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005724 SDValue V1 = SVOp->getOperand(0);
5725 SDValue V2 = SVOp->getOperand(1);
5726 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005727 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005728 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005729
Nate Begemanb9a47b82009-02-23 08:49:38 +00005730 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005731 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005732 // present, fall back to case 3.
5733 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5734 bool V1Only = true;
5735 bool V2Only = true;
5736 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005737 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005738 if (EltIdx < 0)
5739 continue;
5740 if (EltIdx < 16)
5741 V2Only = false;
5742 else
5743 V1Only = false;
5744 }
Eric Christopherfd179292009-08-27 18:07:15 +00005745
Nate Begemanb9a47b82009-02-23 08:49:38 +00005746 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005747 if (TLI.getSubtarget()->hasSSSE3() || TLI.getSubtarget()->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005748 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005749
Nate Begemanb9a47b82009-02-23 08:49:38 +00005750 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005751 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005752 //
5753 // Otherwise, we have elements from both input vectors, and must zero out
5754 // elements that come from V2 in the first mask, and V1 in the second mask
5755 // so that we can OR them together.
5756 bool TwoInputs = !(V1Only || V2Only);
5757 for (unsigned i = 0; i != 16; ++i) {
5758 int EltIdx = MaskVals[i];
5759 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005760 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005761 continue;
5762 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005763 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005764 }
5765 // If all the elements are from V2, assign it to V1 and return after
5766 // building the first pshufb.
5767 if (V2Only)
5768 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005769 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005770 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005771 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005772 if (!TwoInputs)
5773 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005774
Nate Begemanb9a47b82009-02-23 08:49:38 +00005775 // Calculate the shuffle mask for the second input, shuffle it, and
5776 // OR it with the first shuffled input.
5777 pshufbMask.clear();
5778 for (unsigned i = 0; i != 16; ++i) {
5779 int EltIdx = MaskVals[i];
5780 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005781 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005782 continue;
5783 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005784 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005785 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005786 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005787 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005788 MVT::v16i8, &pshufbMask[0], 16));
5789 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005790 }
Eric Christopherfd179292009-08-27 18:07:15 +00005791
Nate Begemanb9a47b82009-02-23 08:49:38 +00005792 // No SSSE3 - Calculate in place words and then fix all out of place words
5793 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5794 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005795 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5796 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005797 SDValue NewV = V2Only ? V2 : V1;
5798 for (int i = 0; i != 8; ++i) {
5799 int Elt0 = MaskVals[i*2];
5800 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005801
Nate Begemanb9a47b82009-02-23 08:49:38 +00005802 // This word of the result is all undef, skip it.
5803 if (Elt0 < 0 && Elt1 < 0)
5804 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005805
Nate Begemanb9a47b82009-02-23 08:49:38 +00005806 // This word of the result is already in the correct place, skip it.
5807 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5808 continue;
5809 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5810 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005811
Nate Begemanb9a47b82009-02-23 08:49:38 +00005812 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5813 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5814 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005815
5816 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5817 // using a single extract together, load it and store it.
5818 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005819 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005820 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005821 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005822 DAG.getIntPtrConstant(i));
5823 continue;
5824 }
5825
Nate Begemanb9a47b82009-02-23 08:49:38 +00005826 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005827 // source byte is not also odd, shift the extracted word left 8 bits
5828 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005829 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005830 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005831 DAG.getIntPtrConstant(Elt1 / 2));
5832 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005833 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005834 DAG.getConstant(8,
5835 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005836 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005837 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5838 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005839 }
5840 // If Elt0 is defined, extract it from the appropriate source. If the
5841 // source byte is not also even, shift the extracted word right 8 bits. If
5842 // Elt1 was also defined, OR the extracted values together before
5843 // inserting them in the result.
5844 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005845 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005846 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5847 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005848 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005849 DAG.getConstant(8,
5850 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005851 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005852 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5853 DAG.getConstant(0x00FF, MVT::i16));
5854 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005855 : InsElt0;
5856 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005857 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005858 DAG.getIntPtrConstant(i));
5859 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005860 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005861}
5862
Evan Cheng7a831ce2007-12-15 03:00:47 +00005863/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005864/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005865/// done when every pair / quad of shuffle mask elements point to elements in
5866/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005867/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005868static
Nate Begeman9008ca62009-04-27 18:41:29 +00005869SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005870 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005871 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005872 SDValue V1 = SVOp->getOperand(0);
5873 SDValue V2 = SVOp->getOperand(1);
5874 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005875 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005876 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005877 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005878 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005879 case MVT::v4f32: NewVT = MVT::v2f64; break;
5880 case MVT::v4i32: NewVT = MVT::v2i64; break;
5881 case MVT::v8i16: NewVT = MVT::v4i32; break;
5882 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005883 }
5884
Nate Begeman9008ca62009-04-27 18:41:29 +00005885 int Scale = NumElems / NewWidth;
5886 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005887 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005888 int StartIdx = -1;
5889 for (int j = 0; j < Scale; ++j) {
5890 int EltIdx = SVOp->getMaskElt(i+j);
5891 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005892 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005893 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005894 StartIdx = EltIdx - (EltIdx % Scale);
5895 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005896 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005897 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005898 if (StartIdx == -1)
5899 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005900 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005901 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005902 }
5903
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005904 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5905 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005906 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005907}
5908
Evan Chengd880b972008-05-09 21:53:03 +00005909/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005910///
Owen Andersone50ed302009-08-10 22:56:29 +00005911static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005912 SDValue SrcOp, SelectionDAG &DAG,
5913 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005914 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005915 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005916 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005917 LD = dyn_cast<LoadSDNode>(SrcOp);
5918 if (!LD) {
5919 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5920 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005921 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005922 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005923 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005924 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005925 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005926 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005927 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005928 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005929 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5930 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5931 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005932 SrcOp.getOperand(0)
5933 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005934 }
5935 }
5936 }
5937
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005938 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005939 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005940 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005941 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005942}
5943
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005944/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5945/// shuffle node referes to only one lane in the sources.
5946static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5947 EVT VT = SVOp->getValueType(0);
5948 int NumElems = VT.getVectorNumElements();
5949 int HalfSize = NumElems/2;
5950 SmallVector<int, 16> M;
5951 SVOp->getMask(M);
5952 bool MatchA = false, MatchB = false;
5953
5954 for (int l = 0; l < NumElems*2; l += HalfSize) {
5955 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5956 MatchA = true;
5957 break;
5958 }
5959 }
5960
5961 for (int l = 0; l < NumElems*2; l += HalfSize) {
5962 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
5963 MatchB = true;
5964 break;
5965 }
5966 }
5967
5968 return MatchA && MatchB;
5969}
5970
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005971/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5972/// which could not be matched by any known target speficic shuffle
5973static SDValue
5974LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005975 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
5976 // If each half of a vector shuffle node referes to only one lane in the
5977 // source vectors, extract each used 128-bit lane and shuffle them using
5978 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
5979 // the work to the legalizer.
5980 DebugLoc dl = SVOp->getDebugLoc();
5981 EVT VT = SVOp->getValueType(0);
5982 int NumElems = VT.getVectorNumElements();
5983 int HalfSize = NumElems/2;
5984
5985 // Extract the reference for each half
5986 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
5987 int FstVecOpNum = 0, SndVecOpNum = 0;
5988 for (int i = 0; i < HalfSize; ++i) {
5989 int Elt = SVOp->getMaskElt(i);
5990 if (SVOp->getMaskElt(i) < 0)
5991 continue;
5992 FstVecOpNum = Elt/NumElems;
5993 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5994 break;
5995 }
5996 for (int i = HalfSize; i < NumElems; ++i) {
5997 int Elt = SVOp->getMaskElt(i);
5998 if (SVOp->getMaskElt(i) < 0)
5999 continue;
6000 SndVecOpNum = Elt/NumElems;
6001 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
6002 break;
6003 }
6004
6005 // Extract the subvectors
6006 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
6007 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
6008 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
6009 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
6010
6011 // Generate 128-bit shuffles
6012 SmallVector<int, 16> MaskV1, MaskV2;
6013 for (int i = 0; i < HalfSize; ++i) {
6014 int Elt = SVOp->getMaskElt(i);
6015 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
6016 }
6017 for (int i = HalfSize; i < NumElems; ++i) {
6018 int Elt = SVOp->getMaskElt(i);
6019 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
6020 }
6021
6022 EVT NVT = V1.getValueType();
6023 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
6024 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
6025
6026 // Concatenate the result back
6027 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
6028 DAG.getConstant(0, MVT::i32), DAG, dl);
6029 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
6030 DAG, dl);
6031 }
6032
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006033 return SDValue();
6034}
6035
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006036/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6037/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006038static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006039LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006040 SDValue V1 = SVOp->getOperand(0);
6041 SDValue V2 = SVOp->getOperand(1);
6042 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006043 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006044
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006045 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
6046
Evan Chengace3c172008-07-22 21:13:36 +00006047 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00006048 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006049 SmallVector<int, 8> Mask1(4U, -1);
6050 SmallVector<int, 8> PermMask;
6051 SVOp->getMask(PermMask);
6052
Evan Chengace3c172008-07-22 21:13:36 +00006053 unsigned NumHi = 0;
6054 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006055 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006056 int Idx = PermMask[i];
6057 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006058 Locs[i] = std::make_pair(-1, -1);
6059 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006060 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6061 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006062 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006063 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006064 NumLo++;
6065 } else {
6066 Locs[i] = std::make_pair(1, NumHi);
6067 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006068 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006069 NumHi++;
6070 }
6071 }
6072 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006073
Evan Chengace3c172008-07-22 21:13:36 +00006074 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006075 // If no more than two elements come from either vector. This can be
6076 // implemented with two shuffles. First shuffle gather the elements.
6077 // The second shuffle, which takes the first shuffle as both of its
6078 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006079 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006080
Nate Begeman9008ca62009-04-27 18:41:29 +00006081 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006082
Evan Chengace3c172008-07-22 21:13:36 +00006083 for (unsigned i = 0; i != 4; ++i) {
6084 if (Locs[i].first == -1)
6085 continue;
6086 else {
6087 unsigned Idx = (i < 2) ? 0 : 4;
6088 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006089 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006090 }
6091 }
6092
Nate Begeman9008ca62009-04-27 18:41:29 +00006093 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006094 } else if (NumLo == 3 || NumHi == 3) {
6095 // Otherwise, we must have three elements from one vector, call it X, and
6096 // one element from the other, call it Y. First, use a shufps to build an
6097 // intermediate vector with the one element from Y and the element from X
6098 // that will be in the same half in the final destination (the indexes don't
6099 // matter). Then, use a shufps to build the final vector, taking the half
6100 // containing the element from Y from the intermediate, and the other half
6101 // from X.
6102 if (NumHi == 3) {
6103 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00006104 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006105 std::swap(V1, V2);
6106 }
6107
6108 // Find the element from V2.
6109 unsigned HiIndex;
6110 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006111 int Val = PermMask[HiIndex];
6112 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006113 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006114 if (Val >= 4)
6115 break;
6116 }
6117
Nate Begeman9008ca62009-04-27 18:41:29 +00006118 Mask1[0] = PermMask[HiIndex];
6119 Mask1[1] = -1;
6120 Mask1[2] = PermMask[HiIndex^1];
6121 Mask1[3] = -1;
6122 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006123
6124 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006125 Mask1[0] = PermMask[0];
6126 Mask1[1] = PermMask[1];
6127 Mask1[2] = HiIndex & 1 ? 6 : 4;
6128 Mask1[3] = HiIndex & 1 ? 4 : 6;
6129 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006130 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006131 Mask1[0] = HiIndex & 1 ? 2 : 0;
6132 Mask1[1] = HiIndex & 1 ? 0 : 2;
6133 Mask1[2] = PermMask[2];
6134 Mask1[3] = PermMask[3];
6135 if (Mask1[2] >= 0)
6136 Mask1[2] += 4;
6137 if (Mask1[3] >= 0)
6138 Mask1[3] += 4;
6139 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006140 }
Evan Chengace3c172008-07-22 21:13:36 +00006141 }
6142
6143 // Break it into (shuffle shuffle_hi, shuffle_lo).
6144 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006145 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006146 SmallVector<int,8> LoMask(4U, -1);
6147 SmallVector<int,8> HiMask(4U, -1);
6148
6149 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006150 unsigned MaskIdx = 0;
6151 unsigned LoIdx = 0;
6152 unsigned HiIdx = 2;
6153 for (unsigned i = 0; i != 4; ++i) {
6154 if (i == 2) {
6155 MaskPtr = &HiMask;
6156 MaskIdx = 1;
6157 LoIdx = 0;
6158 HiIdx = 2;
6159 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006160 int Idx = PermMask[i];
6161 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006162 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006163 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006164 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006165 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006166 LoIdx++;
6167 } else {
6168 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006169 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006170 HiIdx++;
6171 }
6172 }
6173
Nate Begeman9008ca62009-04-27 18:41:29 +00006174 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6175 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6176 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006177 for (unsigned i = 0; i != 4; ++i) {
6178 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006179 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006180 } else {
6181 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006182 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006183 }
6184 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006185 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006186}
6187
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006188static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006189 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006190 V = V.getOperand(0);
6191 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6192 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006193 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6194 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6195 // BUILD_VECTOR (load), undef
6196 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006197 if (MayFoldLoad(V))
6198 return true;
6199 return false;
6200}
6201
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006202// FIXME: the version above should always be used. Since there's
6203// a bug where several vector shuffles can't be folded because the
6204// DAG is not updated during lowering and a node claims to have two
6205// uses while it only has one, use this version, and let isel match
6206// another instruction if the load really happens to have more than
6207// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006208// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006209static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006210 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006211 V = V.getOperand(0);
6212 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6213 V = V.getOperand(0);
6214 if (ISD::isNormalLoad(V.getNode()))
6215 return true;
6216 return false;
6217}
6218
6219/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6220/// a vector extract, and if both can be later optimized into a single load.
6221/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6222/// here because otherwise a target specific shuffle node is going to be
6223/// emitted for this shuffle, and the optimization not done.
6224/// FIXME: This is probably not the best approach, but fix the problem
6225/// until the right path is decided.
6226static
6227bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6228 const TargetLowering &TLI) {
6229 EVT VT = V.getValueType();
6230 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6231
6232 // Be sure that the vector shuffle is present in a pattern like this:
6233 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6234 if (!V.hasOneUse())
6235 return false;
6236
6237 SDNode *N = *V.getNode()->use_begin();
6238 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6239 return false;
6240
6241 SDValue EltNo = N->getOperand(1);
6242 if (!isa<ConstantSDNode>(EltNo))
6243 return false;
6244
6245 // If the bit convert changed the number of elements, it is unsafe
6246 // to examine the mask.
6247 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006248 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006249 EVT SrcVT = V.getOperand(0).getValueType();
6250 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6251 return false;
6252 V = V.getOperand(0);
6253 HasShuffleIntoBitcast = true;
6254 }
6255
6256 // Select the input vector, guarding against out of range extract vector.
6257 unsigned NumElems = VT.getVectorNumElements();
6258 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6259 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6260 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6261
6262 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006263 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006264 V = V.getOperand(0);
6265
6266 if (ISD::isNormalLoad(V.getNode())) {
6267 // Is the original load suitable?
6268 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6269
6270 // FIXME: avoid the multi-use bug that is preventing lots of
6271 // of foldings to be detected, this is still wrong of course, but
6272 // give the temporary desired behavior, and if it happens that
6273 // the load has real more uses, during isel it will not fold, and
6274 // will generate poor code.
6275 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6276 return false;
6277
6278 if (!HasShuffleIntoBitcast)
6279 return true;
6280
6281 // If there's a bitcast before the shuffle, check if the load type and
6282 // alignment is valid.
6283 unsigned Align = LN0->getAlignment();
6284 unsigned NewAlign =
6285 TLI.getTargetData()->getABITypeAlignment(
6286 VT.getTypeForEVT(*DAG.getContext()));
6287
6288 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6289 return false;
6290 }
6291
6292 return true;
6293}
6294
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006295static
Evan Cheng835580f2010-10-07 20:50:20 +00006296SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6297 EVT VT = Op.getValueType();
6298
6299 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006300 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6301 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006302 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6303 V1, DAG));
6304}
6305
6306static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006307SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006308 bool HasXMMInt) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006309 SDValue V1 = Op.getOperand(0);
6310 SDValue V2 = Op.getOperand(1);
6311 EVT VT = Op.getValueType();
6312
6313 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6314
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006315 if (HasXMMInt && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006316 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6317
Evan Cheng0899f5c2011-08-31 02:05:24 +00006318 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6319 return DAG.getNode(ISD::BITCAST, dl, VT,
6320 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6321 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6322 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006323}
6324
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006325static
6326SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6327 SDValue V1 = Op.getOperand(0);
6328 SDValue V2 = Op.getOperand(1);
6329 EVT VT = Op.getValueType();
6330
6331 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6332 "unsupported shuffle type");
6333
6334 if (V2.getOpcode() == ISD::UNDEF)
6335 V2 = V1;
6336
6337 // v4i32 or v4f32
6338 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6339}
6340
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006341static inline unsigned getSHUFPOpcode(EVT VT) {
6342 switch(VT.getSimpleVT().SimpleTy) {
6343 case MVT::v8i32: // Use fp unit for int unpack.
6344 case MVT::v8f32:
6345 case MVT::v4i32: // Use fp unit for int unpack.
6346 case MVT::v4f32: return X86ISD::SHUFPS;
6347 case MVT::v4i64: // Use fp unit for int unpack.
6348 case MVT::v4f64:
6349 case MVT::v2i64: // Use fp unit for int unpack.
6350 case MVT::v2f64: return X86ISD::SHUFPD;
6351 default:
6352 llvm_unreachable("Unknown type for shufp*");
6353 }
6354 return 0;
6355}
6356
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006357static
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006358SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasXMMInt) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006359 SDValue V1 = Op.getOperand(0);
6360 SDValue V2 = Op.getOperand(1);
6361 EVT VT = Op.getValueType();
6362 unsigned NumElems = VT.getVectorNumElements();
6363
6364 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6365 // operand of these instructions is only memory, so check if there's a
6366 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6367 // same masks.
6368 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006369
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006370 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006371 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006372 CanFoldLoad = true;
6373
6374 // When V1 is a load, it can be folded later into a store in isel, example:
6375 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6376 // turns into:
6377 // (MOVLPSmr addr:$src1, VR128:$src2)
6378 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006379 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006380 CanFoldLoad = true;
6381
Dan Gohman65fd6562011-11-03 21:49:52 +00006382 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006383 if (CanFoldLoad) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006384 if (HasXMMInt && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006385 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6386
6387 if (NumElems == 4)
Dan Gohman65fd6562011-11-03 21:49:52 +00006388 // If we don't care about the second element, procede to use movss.
6389 if (SVOp->getMaskElt(1) != -1)
6390 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006391 }
6392
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006393 // movl and movlp will both match v2i64, but v2i64 is never matched by
6394 // movl earlier because we make it strict to avoid messing with the movlp load
6395 // folding logic (see the code above getMOVLP call). Match it here then,
6396 // this is horrible, but will stay like this until we move all shuffle
6397 // matching to x86 specific nodes. Note that for the 1st condition all
6398 // types are matched with movsd.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006399 if (HasXMMInt) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006400 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6401 // as to remove this logic from here, as much as possible
6402 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006403 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006404 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006405 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006406
6407 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6408
6409 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006410 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006411 X86::getShuffleSHUFImmediate(SVOp), DAG);
6412}
6413
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006414static inline unsigned getUNPCKLOpcode(EVT VT) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006415 switch(VT.getSimpleVT().SimpleTy) {
6416 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
6417 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006418 case MVT::v4f32: return X86ISD::UNPCKLPS;
6419 case MVT::v2f64: return X86ISD::UNPCKLPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006420 case MVT::v8i32: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006421 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006422 case MVT::v4i64: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006423 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006424 case MVT::v16i8: return X86ISD::PUNPCKLBW;
6425 case MVT::v8i16: return X86ISD::PUNPCKLWD;
6426 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006427 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006428 }
6429 return 0;
6430}
6431
6432static inline unsigned getUNPCKHOpcode(EVT VT) {
6433 switch(VT.getSimpleVT().SimpleTy) {
6434 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
6435 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
6436 case MVT::v4f32: return X86ISD::UNPCKHPS;
6437 case MVT::v2f64: return X86ISD::UNPCKHPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006438 case MVT::v8i32: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006439 case MVT::v8f32: return X86ISD::VUNPCKHPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006440 case MVT::v4i64: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006441 case MVT::v4f64: return X86ISD::VUNPCKHPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006442 case MVT::v16i8: return X86ISD::PUNPCKHBW;
6443 case MVT::v8i16: return X86ISD::PUNPCKHWD;
6444 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006445 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006446 }
6447 return 0;
6448}
6449
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006450static inline unsigned getVPERMILOpcode(EVT VT) {
6451 switch(VT.getSimpleVT().SimpleTy) {
6452 case MVT::v4i32:
6453 case MVT::v4f32: return X86ISD::VPERMILPS;
6454 case MVT::v2i64:
6455 case MVT::v2f64: return X86ISD::VPERMILPD;
6456 case MVT::v8i32:
6457 case MVT::v8f32: return X86ISD::VPERMILPSY;
6458 case MVT::v4i64:
6459 case MVT::v4f64: return X86ISD::VPERMILPDY;
6460 default:
6461 llvm_unreachable("Unknown type for vpermil");
6462 }
6463 return 0;
6464}
6465
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006466/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
6467/// a vbroadcast node. The nodes are suitable whenever we can fold a load coming
6468/// from a 32 or 64 bit scalar. Update Op to the desired load to be folded.
6469static bool isVectorBroadcast(SDValue &Op) {
6470 EVT VT = Op.getValueType();
6471 bool Is256 = VT.getSizeInBits() == 256;
6472
6473 assert((VT.getSizeInBits() == 128 || Is256) &&
6474 "Unsupported type for vbroadcast node");
6475
6476 SDValue V = Op;
6477 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
6478 V = V.getOperand(0);
6479
6480 if (Is256 && !(V.hasOneUse() &&
6481 V.getOpcode() == ISD::INSERT_SUBVECTOR &&
6482 V.getOperand(0).getOpcode() == ISD::UNDEF))
6483 return false;
6484
6485 if (Is256)
6486 V = V.getOperand(1);
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006487
6488 if (!V.hasOneUse())
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006489 return false;
6490
6491 // Check the source scalar_to_vector type. 256-bit broadcasts are
6492 // supported for 32/64-bit sizes, while 128-bit ones are only supported
6493 // for 32-bit scalars.
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006494 if (V.getOpcode() != ISD::SCALAR_TO_VECTOR)
6495 return false;
6496
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006497 unsigned ScalarSize = V.getOperand(0).getValueType().getSizeInBits();
6498 if (ScalarSize != 32 && ScalarSize != 64)
6499 return false;
6500 if (!Is256 && ScalarSize == 64)
6501 return false;
6502
6503 V = V.getOperand(0);
6504 if (!MayFoldLoad(V))
6505 return false;
6506
6507 // Return the load node
6508 Op = V;
6509 return true;
6510}
6511
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006512static
6513SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006514 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006515 const X86Subtarget *Subtarget) {
6516 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6517 EVT VT = Op.getValueType();
6518 DebugLoc dl = Op.getDebugLoc();
6519 SDValue V1 = Op.getOperand(0);
6520 SDValue V2 = Op.getOperand(1);
6521
6522 if (isZeroShuffle(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006523 return getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006524
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006525 // Handle splat operations
6526 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006527 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006528 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006529 // Special case, this is the only place now where it's allowed to return
6530 // a vector_shuffle operation without using a target specific node, because
6531 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6532 // this be moved to DAGCombine instead?
6533 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006534 return Op;
6535
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006536 // Use vbroadcast whenever the splat comes from a foldable load
6537 if (Subtarget->hasAVX() && isVectorBroadcast(V1))
6538 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, V1);
6539
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006540 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006541 if ((Size == 128 && NumElem <= 4) ||
6542 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006543 return SDValue();
6544
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006545 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006546 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006547 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006548
6549 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6550 // do it!
6551 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6552 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6553 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006554 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006555 } else if ((VT == MVT::v4i32 ||
6556 (VT == MVT::v4f32 && Subtarget->hasXMMInt()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006557 // FIXME: Figure out a cleaner way to do this.
6558 // Try to make use of movq to zero out the top part.
6559 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6560 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6561 if (NewOp.getNode()) {
6562 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6563 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6564 DAG, Subtarget, dl);
6565 }
6566 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6567 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6568 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6569 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6570 DAG, Subtarget, dl);
6571 }
6572 }
6573 return SDValue();
6574}
6575
Dan Gohman475871a2008-07-27 21:46:04 +00006576SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006577X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006578 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006579 SDValue V1 = Op.getOperand(0);
6580 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006581 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006582 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006583 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006584 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006585 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
6586 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006587 bool V1IsSplat = false;
6588 bool V2IsSplat = false;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006589 bool HasXMMInt = Subtarget->hasXMMInt();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006590 MachineFunction &MF = DAG.getMachineFunction();
6591 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006592
Dale Johannesen0488fb62010-09-30 23:57:10 +00006593 // Shuffle operations on MMX not supported.
6594 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006595 return Op;
6596
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006597 // Vector shuffle lowering takes 3 steps:
6598 //
6599 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6600 // narrowing and commutation of operands should be handled.
6601 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6602 // shuffle nodes.
6603 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6604 // so the shuffle can be broken into other shuffles and the legalizer can
6605 // try the lowering again.
6606 //
6607 // The general ideia is that no vector_shuffle operation should be left to
6608 // be matched during isel, all of them must be converted to a target specific
6609 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006610
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006611 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6612 // narrowing and commutation of operands should be handled. The actual code
6613 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006614 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006615 if (NewOp.getNode())
6616 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006617
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006618 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6619 // unpckh_undef). Only use pshufd if speed is more important than size.
6620 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006621 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006622 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006623 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006624
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006625 if (X86::isMOVDDUPMask(SVOp) &&
6626 (Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
6627 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006628 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006629
Dale Johannesen0488fb62010-09-30 23:57:10 +00006630 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006631 return getMOVHighToLow(Op, dl, DAG);
6632
6633 // Use to match splats
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006634 if (HasXMMInt && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006635 (VT == MVT::v2f64 || VT == MVT::v2i64))
6636 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
6637
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006638 if (X86::isPSHUFDMask(SVOp)) {
6639 // The actual implementation will match the mask in the if above and then
6640 // during isel it can match several different instructions, not only pshufd
6641 // as its name says, sad but true, emulate the behavior for now...
6642 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6643 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6644
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006645 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6646
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006647 if (HasXMMInt && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006648 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6649
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006650 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6651 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006652 }
Eric Christopherfd179292009-08-27 18:07:15 +00006653
Evan Chengf26ffe92008-05-29 08:22:04 +00006654 // Check if this can be converted into a logical shift.
6655 bool isLeft = false;
6656 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006657 SDValue ShVal;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006658 bool isShift = getSubtarget()->hasXMMInt() &&
6659 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006660 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006661 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006662 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006663 EVT EltVT = VT.getVectorElementType();
6664 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006665 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006666 }
Eric Christopherfd179292009-08-27 18:07:15 +00006667
Nate Begeman9008ca62009-04-27 18:41:29 +00006668 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006669 if (V1IsUndef)
6670 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00006671 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006672 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006673 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006674 if (HasXMMInt && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006675 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6676
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006677 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006678 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6679 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006680 }
Eric Christopherfd179292009-08-27 18:07:15 +00006681
Nate Begeman9008ca62009-04-27 18:41:29 +00006682 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00006683 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006684 return getMOVLowToHigh(Op, dl, DAG, HasXMMInt);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006685
Dale Johannesen0488fb62010-09-30 23:57:10 +00006686 if (X86::isMOVHLPSMask(SVOp))
6687 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006688
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006689 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006690 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006691
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006692 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006693 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006694
Dale Johannesen0488fb62010-09-30 23:57:10 +00006695 if (X86::isMOVLPMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006696 return getMOVLP(Op, dl, DAG, HasXMMInt);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006697
Nate Begeman9008ca62009-04-27 18:41:29 +00006698 if (ShouldXformToMOVHLPS(SVOp) ||
6699 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6700 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006701
Evan Chengf26ffe92008-05-29 08:22:04 +00006702 if (isShift) {
6703 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006704 EVT EltVT = VT.getVectorElementType();
6705 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006706 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006707 }
Eric Christopherfd179292009-08-27 18:07:15 +00006708
Evan Cheng9eca5e82006-10-25 21:49:50 +00006709 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006710 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6711 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006712 V1IsSplat = isSplatVector(V1.getNode());
6713 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006714
Chris Lattner8a594482007-11-25 00:24:49 +00006715 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00006716 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006717 Op = CommuteVectorShuffle(SVOp, DAG);
6718 SVOp = cast<ShuffleVectorSDNode>(Op);
6719 V1 = SVOp->getOperand(0);
6720 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006721 std::swap(V1IsSplat, V2IsSplat);
6722 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006723 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006724 }
6725
Nate Begeman9008ca62009-04-27 18:41:29 +00006726 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6727 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006728 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006729 return V1;
6730 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6731 // the instruction selector will not match, so get a canonical MOVL with
6732 // swapped operands to undo the commute.
6733 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006734 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006735
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006736 if (X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006737 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006738
6739 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006740 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006741
Evan Cheng9bbbb982006-10-25 20:48:19 +00006742 if (V2IsSplat) {
6743 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006744 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006745 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006746 SDValue NewMask = NormalizeMask(SVOp, DAG);
6747 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6748 if (NSVOp != SVOp) {
6749 if (X86::isUNPCKLMask(NSVOp, true)) {
6750 return NewMask;
6751 } else if (X86::isUNPCKHMask(NSVOp, true)) {
6752 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006753 }
6754 }
6755 }
6756
Evan Cheng9eca5e82006-10-25 21:49:50 +00006757 if (Commuted) {
6758 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006759 // FIXME: this seems wrong.
6760 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6761 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006762
6763 if (X86::isUNPCKLMask(NewSVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006764 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006765
6766 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006767 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006768 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006769
Nate Begeman9008ca62009-04-27 18:41:29 +00006770 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006771 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006772 return CommuteVectorShuffle(SVOp, DAG);
6773
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006774 // The checks below are all present in isShuffleMaskLegal, but they are
6775 // inlined here right now to enable us to directly emit target specific
6776 // nodes, and remove one by one until they don't return Op anymore.
6777 SmallVector<int, 16> M;
6778 SVOp->getMask(M);
6779
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006780 if (isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006781 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6782 X86::getShufflePALIGNRImmediate(SVOp),
6783 DAG);
6784
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006785 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6786 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006787 if (VT == MVT::v2f64)
6788 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006789 if (VT == MVT::v2i64)
6790 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6791 }
6792
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006793 if (isPSHUFHWMask(M, VT))
6794 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6795 X86::getShufflePSHUFHWImmediate(SVOp),
6796 DAG);
6797
6798 if (isPSHUFLWMask(M, VT))
6799 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6800 X86::getShufflePSHUFLWImmediate(SVOp),
6801 DAG);
6802
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006803 if (isSHUFPMask(M, VT))
6804 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6805 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006806
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006807 if (X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006808 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006809 if (X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006810 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006811
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006812 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006813 // Generate target specific nodes for 128 or 256-bit shuffles only
6814 // supported in the AVX instruction set.
6815 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006816
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006817 // Handle VMOVDDUPY permutations
6818 if (isMOVDDUPYMask(SVOp, Subtarget))
6819 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6820
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006821 // Handle VPERMILPS* permutations
6822 if (isVPERMILPSMask(M, VT, Subtarget))
6823 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6824 getShuffleVPERMILPSImmediate(SVOp), DAG);
6825
6826 // Handle VPERMILPD* permutations
6827 if (isVPERMILPDMask(M, VT, Subtarget))
6828 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6829 getShuffleVPERMILPDImmediate(SVOp), DAG);
6830
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00006831 // Handle VPERM2F128 permutations
6832 if (isVPERM2F128Mask(M, VT, Subtarget))
6833 return getTargetShuffleNode(X86ISD::VPERM2F128, dl, VT, V1, V2,
6834 getShuffleVPERM2F128Immediate(SVOp), DAG);
6835
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006836 // Handle VSHUFPSY permutations
6837 if (isVSHUFPSYMask(M, VT, Subtarget))
6838 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6839 getShuffleVSHUFPSYImmediate(SVOp), DAG);
6840
6841 // Handle VSHUFPDY permutations
6842 if (isVSHUFPDYMask(M, VT, Subtarget))
6843 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6844 getShuffleVSHUFPDYImmediate(SVOp), DAG);
6845
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006846 //===--------------------------------------------------------------------===//
6847 // Since no target specific shuffle was selected for this generic one,
6848 // lower it into other known shuffles. FIXME: this isn't true yet, but
6849 // this is the plan.
6850 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006851
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006852 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6853 if (VT == MVT::v8i16) {
6854 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6855 if (NewOp.getNode())
6856 return NewOp;
6857 }
6858
6859 if (VT == MVT::v16i8) {
6860 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6861 if (NewOp.getNode())
6862 return NewOp;
6863 }
6864
6865 // Handle all 128-bit wide vectors with 4 elements, and match them with
6866 // several different shuffle types.
6867 if (NumElems == 4 && VT.getSizeInBits() == 128)
6868 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6869
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006870 // Handle general 256-bit shuffles
6871 if (VT.is256BitVector())
6872 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6873
Dan Gohman475871a2008-07-27 21:46:04 +00006874 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006875}
6876
Dan Gohman475871a2008-07-27 21:46:04 +00006877SDValue
6878X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006879 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006880 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006881 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006882
6883 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6884 return SDValue();
6885
Duncan Sands83ec4b62008-06-06 12:08:01 +00006886 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006887 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006888 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006889 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006890 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006891 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006892 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006893 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6894 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6895 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006896 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6897 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006898 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006899 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006900 Op.getOperand(0)),
6901 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006902 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006903 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006904 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006905 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006906 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006907 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006908 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6909 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006910 // result has a single use which is a store or a bitcast to i32. And in
6911 // the case of a store, it's not worth it if the index is a constant 0,
6912 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006913 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006914 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006915 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006916 if ((User->getOpcode() != ISD::STORE ||
6917 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6918 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006919 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006920 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006921 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006922 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006923 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006924 Op.getOperand(0)),
6925 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006926 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006927 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006928 // ExtractPS works with constant index.
6929 if (isa<ConstantSDNode>(Op.getOperand(1)))
6930 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006931 }
Dan Gohman475871a2008-07-27 21:46:04 +00006932 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006933}
6934
6935
Dan Gohman475871a2008-07-27 21:46:04 +00006936SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006937X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6938 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006939 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006940 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006941
David Greene74a579d2011-02-10 16:57:36 +00006942 SDValue Vec = Op.getOperand(0);
6943 EVT VecVT = Vec.getValueType();
6944
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006945 // If this is a 256-bit vector result, first extract the 128-bit vector and
6946 // then extract the element from the 128-bit vector.
6947 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006948 DebugLoc dl = Op.getNode()->getDebugLoc();
6949 unsigned NumElems = VecVT.getVectorNumElements();
6950 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006951 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6952
6953 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006954 bool Upper = IdxVal >= NumElems/2;
6955 Vec = Extract128BitVector(Vec,
6956 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006957
David Greene74a579d2011-02-10 16:57:36 +00006958 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006959 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006960 }
6961
6962 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6963
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006964 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006965 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006966 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006967 return Res;
6968 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006969
Owen Andersone50ed302009-08-10 22:56:29 +00006970 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006971 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006972 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006973 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006974 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006975 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006976 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006977 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6978 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006979 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006980 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006981 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006982 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006983 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006984 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006985 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006986 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006987 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006988 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006989 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006990 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006991 if (Idx == 0)
6992 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006993
Evan Cheng0db9fe62006-04-25 20:13:52 +00006994 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006995 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006996 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006997 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006998 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006999 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007000 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00007001 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007002 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7003 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7004 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007005 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007006 if (Idx == 0)
7007 return Op;
7008
7009 // UNPCKHPD the element to the lowest double word, then movsd.
7010 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7011 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007012 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007013 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007014 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007015 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007016 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007017 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007018 }
7019
Dan Gohman475871a2008-07-27 21:46:04 +00007020 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007021}
7022
Dan Gohman475871a2008-07-27 21:46:04 +00007023SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007024X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7025 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007026 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007027 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007028 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007029
Dan Gohman475871a2008-07-27 21:46:04 +00007030 SDValue N0 = Op.getOperand(0);
7031 SDValue N1 = Op.getOperand(1);
7032 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007033
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007034 if (VT.getSizeInBits() == 256)
7035 return SDValue();
7036
Dan Gohman8a55ce42009-09-23 21:02:20 +00007037 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007038 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007039 unsigned Opc;
7040 if (VT == MVT::v8i16)
7041 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007042 else if (VT == MVT::v16i8)
7043 Opc = X86ISD::PINSRB;
7044 else
7045 Opc = X86ISD::PINSRB;
7046
Nate Begeman14d12ca2008-02-11 04:19:36 +00007047 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7048 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007049 if (N1.getValueType() != MVT::i32)
7050 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7051 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007052 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007053 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007054 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007055 // Bits [7:6] of the constant are the source select. This will always be
7056 // zero here. The DAG Combiner may combine an extract_elt index into these
7057 // bits. For example (insert (extract, 3), 2) could be matched by putting
7058 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007059 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007060 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007061 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007062 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007063 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007064 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007065 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007066 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007067 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007068 // PINSR* works with constant index.
7069 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007070 }
Dan Gohman475871a2008-07-27 21:46:04 +00007071 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007072}
7073
Dan Gohman475871a2008-07-27 21:46:04 +00007074SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007075X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007076 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007077 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007078
David Greene6b381262011-02-09 15:32:06 +00007079 DebugLoc dl = Op.getDebugLoc();
7080 SDValue N0 = Op.getOperand(0);
7081 SDValue N1 = Op.getOperand(1);
7082 SDValue N2 = Op.getOperand(2);
7083
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007084 // If this is a 256-bit vector result, first extract the 128-bit vector,
7085 // insert the element into the extracted half and then place it back.
7086 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007087 if (!isa<ConstantSDNode>(N2))
7088 return SDValue();
7089
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007090 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007091 unsigned NumElems = VT.getVectorNumElements();
7092 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007093 bool Upper = IdxVal >= NumElems/2;
7094 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
7095 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007096
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007097 // Insert the element into the desired half.
7098 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
7099 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00007100
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007101 // Insert the changed part back to the 256-bit vector
7102 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007103 }
7104
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007105 if (Subtarget->hasSSE41() || Subtarget->hasAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007106 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7107
Dan Gohman8a55ce42009-09-23 21:02:20 +00007108 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007109 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007110
Dan Gohman8a55ce42009-09-23 21:02:20 +00007111 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007112 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7113 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007114 if (N1.getValueType() != MVT::i32)
7115 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7116 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007117 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007118 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007119 }
Dan Gohman475871a2008-07-27 21:46:04 +00007120 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007121}
7122
Dan Gohman475871a2008-07-27 21:46:04 +00007123SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007124X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007125 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007126 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007127 EVT OpVT = Op.getValueType();
7128
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007129 // If this is a 256-bit vector result, first insert into a 128-bit
7130 // vector and then insert into the 256-bit vector.
7131 if (OpVT.getSizeInBits() > 128) {
7132 // Insert into a 128-bit vector.
7133 EVT VT128 = EVT::getVectorVT(*Context,
7134 OpVT.getVectorElementType(),
7135 OpVT.getVectorNumElements() / 2);
7136
7137 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7138
7139 // Insert the 128-bit vector.
7140 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7141 DAG.getConstant(0, MVT::i32),
7142 DAG, dl);
7143 }
7144
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007145 if (Op.getValueType() == MVT::v1i64 &&
7146 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007147 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007148
Owen Anderson825b72b2009-08-11 20:47:22 +00007149 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007150 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7151 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007152 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007153 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007154}
7155
David Greene91585092011-01-26 15:38:49 +00007156// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7157// a simple subregister reference or explicit instructions to grab
7158// upper bits of a vector.
7159SDValue
7160X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7161 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007162 DebugLoc dl = Op.getNode()->getDebugLoc();
7163 SDValue Vec = Op.getNode()->getOperand(0);
7164 SDValue Idx = Op.getNode()->getOperand(1);
7165
7166 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7167 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7168 return Extract128BitVector(Vec, Idx, DAG, dl);
7169 }
David Greene91585092011-01-26 15:38:49 +00007170 }
7171 return SDValue();
7172}
7173
David Greenecfe33c42011-01-26 19:13:22 +00007174// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7175// simple superregister reference or explicit instructions to insert
7176// the upper bits of a vector.
7177SDValue
7178X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7179 if (Subtarget->hasAVX()) {
7180 DebugLoc dl = Op.getNode()->getDebugLoc();
7181 SDValue Vec = Op.getNode()->getOperand(0);
7182 SDValue SubVec = Op.getNode()->getOperand(1);
7183 SDValue Idx = Op.getNode()->getOperand(2);
7184
7185 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7186 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007187 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007188 }
7189 }
7190 return SDValue();
7191}
7192
Bill Wendling056292f2008-09-16 21:48:12 +00007193// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7194// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7195// one of the above mentioned nodes. It has to be wrapped because otherwise
7196// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7197// be used to form addressing mode. These wrapped nodes will be selected
7198// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007199SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007200X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007201 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007202
Chris Lattner41621a22009-06-26 19:22:52 +00007203 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7204 // global base reg.
7205 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007206 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007207 CodeModel::Model M = getTargetMachine().getCodeModel();
7208
Chris Lattner4f066492009-07-11 20:29:19 +00007209 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007210 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007211 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007212 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007213 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007214 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007215 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007216
Evan Cheng1606e8e2009-03-13 07:51:59 +00007217 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007218 CP->getAlignment(),
7219 CP->getOffset(), OpFlag);
7220 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007221 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007222 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007223 if (OpFlag) {
7224 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007225 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007226 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007227 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007228 }
7229
7230 return Result;
7231}
7232
Dan Gohmand858e902010-04-17 15:26:15 +00007233SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007234 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007235
Chris Lattner18c59872009-06-27 04:16:01 +00007236 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7237 // global base reg.
7238 unsigned char OpFlag = 0;
7239 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007240 CodeModel::Model M = getTargetMachine().getCodeModel();
7241
Chris Lattner4f066492009-07-11 20:29:19 +00007242 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007243 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007244 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007245 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007246 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007247 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007248 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007249
Chris Lattner18c59872009-06-27 04:16:01 +00007250 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7251 OpFlag);
7252 DebugLoc DL = JT->getDebugLoc();
7253 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007254
Chris Lattner18c59872009-06-27 04:16:01 +00007255 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007256 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007257 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7258 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007259 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007260 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007261
Chris Lattner18c59872009-06-27 04:16:01 +00007262 return Result;
7263}
7264
7265SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007266X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007267 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007268
Chris Lattner18c59872009-06-27 04:16:01 +00007269 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7270 // global base reg.
7271 unsigned char OpFlag = 0;
7272 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007273 CodeModel::Model M = getTargetMachine().getCodeModel();
7274
Chris Lattner4f066492009-07-11 20:29:19 +00007275 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007276 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7277 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7278 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007279 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007280 } else if (Subtarget->isPICStyleGOT()) {
7281 OpFlag = X86II::MO_GOT;
7282 } else if (Subtarget->isPICStyleStubPIC()) {
7283 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7284 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7285 OpFlag = X86II::MO_DARWIN_NONLAZY;
7286 }
Eric Christopherfd179292009-08-27 18:07:15 +00007287
Chris Lattner18c59872009-06-27 04:16:01 +00007288 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007289
Chris Lattner18c59872009-06-27 04:16:01 +00007290 DebugLoc DL = Op.getDebugLoc();
7291 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007292
7293
Chris Lattner18c59872009-06-27 04:16:01 +00007294 // With PIC, the address is actually $g + Offset.
7295 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007296 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007297 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7298 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007299 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007300 Result);
7301 }
Eric Christopherfd179292009-08-27 18:07:15 +00007302
Eli Friedman586272d2011-08-11 01:48:05 +00007303 // For symbols that require a load from a stub to get the address, emit the
7304 // load.
7305 if (isGlobalStubReference(OpFlag))
7306 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
7307 MachinePointerInfo::getGOT(), false, false, 0);
7308
Chris Lattner18c59872009-06-27 04:16:01 +00007309 return Result;
7310}
7311
Dan Gohman475871a2008-07-27 21:46:04 +00007312SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007313X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007314 // Create the TargetBlockAddressAddress node.
7315 unsigned char OpFlags =
7316 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007317 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007318 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007319 DebugLoc dl = Op.getDebugLoc();
7320 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7321 /*isTarget=*/true, OpFlags);
7322
Dan Gohmanf705adb2009-10-30 01:28:02 +00007323 if (Subtarget->isPICStyleRIPRel() &&
7324 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007325 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7326 else
7327 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007328
Dan Gohman29cbade2009-11-20 23:18:13 +00007329 // With PIC, the address is actually $g + Offset.
7330 if (isGlobalRelativeToPICBase(OpFlags)) {
7331 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7332 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7333 Result);
7334 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007335
7336 return Result;
7337}
7338
7339SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007340X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007341 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007342 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007343 // Create the TargetGlobalAddress node, folding in the constant
7344 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007345 unsigned char OpFlags =
7346 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007347 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007348 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007349 if (OpFlags == X86II::MO_NO_FLAG &&
7350 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007351 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007352 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007353 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007354 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007355 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007356 }
Eric Christopherfd179292009-08-27 18:07:15 +00007357
Chris Lattner4f066492009-07-11 20:29:19 +00007358 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007359 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007360 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7361 else
7362 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007363
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007364 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007365 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007366 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7367 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007368 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007369 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007370
Chris Lattner36c25012009-07-10 07:34:39 +00007371 // For globals that require a load from a stub to get the address, emit the
7372 // load.
7373 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007374 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007375 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007376
Dan Gohman6520e202008-10-18 02:06:02 +00007377 // If there was a non-zero offset that we didn't fold, create an explicit
7378 // addition for it.
7379 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007380 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007381 DAG.getConstant(Offset, getPointerTy()));
7382
Evan Cheng0db9fe62006-04-25 20:13:52 +00007383 return Result;
7384}
7385
Evan Chengda43bcf2008-09-24 00:05:32 +00007386SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007387X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007388 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007389 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007390 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007391}
7392
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007393static SDValue
7394GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007395 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007396 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007397 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007398 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007399 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007400 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007401 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007402 GA->getOffset(),
7403 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007404 if (InFlag) {
7405 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007406 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007407 } else {
7408 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007409 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007410 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007411
7412 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007413 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007414
Rafael Espindola15f1b662009-04-24 12:59:40 +00007415 SDValue Flag = Chain.getValue(1);
7416 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007417}
7418
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007419// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007420static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007421LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007422 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007423 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007424 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7425 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007426 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007427 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007428 InFlag = Chain.getValue(1);
7429
Chris Lattnerb903bed2009-06-26 21:20:29 +00007430 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007431}
7432
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007433// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007434static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007435LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007436 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007437 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7438 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007439}
7440
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007441// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7442// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007443static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007444 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007445 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007446 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007447
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007448 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7449 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7450 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007451
Michael J. Spencerec38de22010-10-10 22:04:20 +00007452 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007453 DAG.getIntPtrConstant(0),
7454 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007455
Chris Lattnerb903bed2009-06-26 21:20:29 +00007456 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007457 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7458 // initialexec.
7459 unsigned WrapperKind = X86ISD::Wrapper;
7460 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007461 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007462 } else if (is64Bit) {
7463 assert(model == TLSModel::InitialExec);
7464 OperandFlags = X86II::MO_GOTTPOFF;
7465 WrapperKind = X86ISD::WrapperRIP;
7466 } else {
7467 assert(model == TLSModel::InitialExec);
7468 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007469 }
Eric Christopherfd179292009-08-27 18:07:15 +00007470
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007471 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7472 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007473 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007474 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007475 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007476 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007477
Rafael Espindola9a580232009-02-27 13:37:18 +00007478 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007479 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007480 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007481
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007482 // The address of the thread local variable is the add of the thread
7483 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007484 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007485}
7486
Dan Gohman475871a2008-07-27 21:46:04 +00007487SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007488X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007489
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007490 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007491 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007492
Eric Christopher30ef0e52010-06-03 04:07:48 +00007493 if (Subtarget->isTargetELF()) {
7494 // TODO: implement the "local dynamic" model
7495 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007496
Eric Christopher30ef0e52010-06-03 04:07:48 +00007497 // If GV is an alias then use the aliasee for determining
7498 // thread-localness.
7499 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7500 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007501
7502 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007503 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007504
Eric Christopher30ef0e52010-06-03 04:07:48 +00007505 switch (model) {
7506 case TLSModel::GeneralDynamic:
7507 case TLSModel::LocalDynamic: // not implemented
7508 if (Subtarget->is64Bit())
7509 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7510 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007511
Eric Christopher30ef0e52010-06-03 04:07:48 +00007512 case TLSModel::InitialExec:
7513 case TLSModel::LocalExec:
7514 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7515 Subtarget->is64Bit());
7516 }
7517 } else if (Subtarget->isTargetDarwin()) {
7518 // Darwin only has one model of TLS. Lower to that.
7519 unsigned char OpFlag = 0;
7520 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7521 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007522
Eric Christopher30ef0e52010-06-03 04:07:48 +00007523 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7524 // global base reg.
7525 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7526 !Subtarget->is64Bit();
7527 if (PIC32)
7528 OpFlag = X86II::MO_TLVP_PIC_BASE;
7529 else
7530 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007531 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007532 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007533 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007534 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007535 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007536
Eric Christopher30ef0e52010-06-03 04:07:48 +00007537 // With PIC32, the address is actually $g + Offset.
7538 if (PIC32)
7539 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7540 DAG.getNode(X86ISD::GlobalBaseReg,
7541 DebugLoc(), getPointerTy()),
7542 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007543
Eric Christopher30ef0e52010-06-03 04:07:48 +00007544 // Lowering the machine isd will make sure everything is in the right
7545 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007546 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007547 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007548 SDValue Args[] = { Chain, Offset };
7549 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007550
Eric Christopher30ef0e52010-06-03 04:07:48 +00007551 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7552 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7553 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007554
Eric Christopher30ef0e52010-06-03 04:07:48 +00007555 // And our return value (tls address) is in the standard call return value
7556 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007557 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007558 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7559 Chain.getValue(1));
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007560 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007561
Eric Christopher30ef0e52010-06-03 04:07:48 +00007562 assert(false &&
7563 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007564
Torok Edwinc23197a2009-07-14 16:55:14 +00007565 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007566 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007567}
7568
Evan Cheng0db9fe62006-04-25 20:13:52 +00007569
Nadav Rotem43012222011-05-11 08:12:09 +00007570/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007571/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007572SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007573 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007574 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007575 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007576 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007577 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007578 SDValue ShOpLo = Op.getOperand(0);
7579 SDValue ShOpHi = Op.getOperand(1);
7580 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007581 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007582 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007583 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007584
Dan Gohman475871a2008-07-27 21:46:04 +00007585 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007586 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007587 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7588 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007589 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007590 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7591 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007592 }
Evan Chenge3413162006-01-09 18:33:28 +00007593
Owen Anderson825b72b2009-08-11 20:47:22 +00007594 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7595 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007596 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007597 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007598
Dan Gohman475871a2008-07-27 21:46:04 +00007599 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007600 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007601 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7602 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007603
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007604 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007605 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7606 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007607 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007608 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7609 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007610 }
7611
Dan Gohman475871a2008-07-27 21:46:04 +00007612 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007613 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007614}
Evan Chenga3195e82006-01-12 22:54:21 +00007615
Dan Gohmand858e902010-04-17 15:26:15 +00007616SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7617 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007618 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007619
Dale Johannesen0488fb62010-09-30 23:57:10 +00007620 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007621 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007622
Owen Anderson825b72b2009-08-11 20:47:22 +00007623 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007624 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007625
Eli Friedman36df4992009-05-27 00:47:34 +00007626 // These are really Legal; return the operand so the caller accepts it as
7627 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007628 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007629 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007630 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007631 Subtarget->is64Bit()) {
7632 return Op;
7633 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007634
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007635 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007636 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007637 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007638 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007639 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007640 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007641 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007642 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007643 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007644 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7645}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007646
Owen Andersone50ed302009-08-10 22:56:29 +00007647SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007648 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007649 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007650 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007651 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007652 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007653 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007654 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007655 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007656 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007657 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007658
Chris Lattner492a43e2010-09-22 01:28:21 +00007659 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007660
Stuart Hastings84be9582011-06-02 15:57:11 +00007661 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7662 MachineMemOperand *MMO;
7663 if (FI) {
7664 int SSFI = FI->getIndex();
7665 MMO =
7666 DAG.getMachineFunction()
7667 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7668 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7669 } else {
7670 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7671 StackSlot = StackSlot.getOperand(1);
7672 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007673 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007674 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7675 X86ISD::FILD, DL,
7676 Tys, Ops, array_lengthof(Ops),
7677 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007678
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007679 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007680 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007681 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007682
7683 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7684 // shouldn't be necessary except that RFP cannot be live across
7685 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007686 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007687 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7688 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007689 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007690 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007691 SDValue Ops[] = {
7692 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7693 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007694 MachineMemOperand *MMO =
7695 DAG.getMachineFunction()
7696 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007697 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007698
Chris Lattner492a43e2010-09-22 01:28:21 +00007699 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7700 Ops, array_lengthof(Ops),
7701 Op.getValueType(), MMO);
7702 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007703 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007704 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007705 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007706
Evan Cheng0db9fe62006-04-25 20:13:52 +00007707 return Result;
7708}
7709
Bill Wendling8b8a6362009-01-17 03:56:04 +00007710// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007711SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7712 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007713 // This algorithm is not obvious. Here it is in C code, more or less:
7714 /*
7715 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7716 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7717 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007718
Bill Wendling8b8a6362009-01-17 03:56:04 +00007719 // Copy ints to xmm registers.
7720 __m128i xh = _mm_cvtsi32_si128( hi );
7721 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007722
Bill Wendling8b8a6362009-01-17 03:56:04 +00007723 // Combine into low half of a single xmm register.
7724 __m128i x = _mm_unpacklo_epi32( xh, xl );
7725 __m128d d;
7726 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007727
Bill Wendling8b8a6362009-01-17 03:56:04 +00007728 // Merge in appropriate exponents to give the integer bits the right
7729 // magnitude.
7730 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007731
Bill Wendling8b8a6362009-01-17 03:56:04 +00007732 // Subtract away the biases to deal with the IEEE-754 double precision
7733 // implicit 1.
7734 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007735
Bill Wendling8b8a6362009-01-17 03:56:04 +00007736 // All conversions up to here are exact. The correctly rounded result is
7737 // calculated using the current rounding mode using the following
7738 // horizontal add.
7739 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7740 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7741 // store doesn't really need to be here (except
7742 // maybe to zero the other double)
7743 return sd;
7744 }
7745 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007746
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007747 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007748 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007749
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007750 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007751 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007752 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7753 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7754 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7755 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007756 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007757 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007758
Bill Wendling8b8a6362009-01-17 03:56:04 +00007759 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007760 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007761 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007762 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007763 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007764 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007765 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007766
Owen Anderson825b72b2009-08-11 20:47:22 +00007767 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7768 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007769 Op.getOperand(0),
7770 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007771 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7772 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007773 Op.getOperand(0),
7774 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007775 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7776 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007777 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007778 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007779 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007780 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007781 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007782 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007783 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007784 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007785
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007786 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007787 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007788 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7789 DAG.getUNDEF(MVT::v2f64), ShufMask);
7790 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7791 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007792 DAG.getIntPtrConstant(0));
7793}
7794
Bill Wendling8b8a6362009-01-17 03:56:04 +00007795// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007796SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7797 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007798 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007799 // FP constant to bias correct the final result.
7800 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007801 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007802
7803 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007804 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007805 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007806
Eli Friedmanf3704762011-08-29 21:15:46 +00007807 // Zero out the upper parts of the register.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00007808 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasXMMInt(),
7809 DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007810
Owen Anderson825b72b2009-08-11 20:47:22 +00007811 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007812 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007813 DAG.getIntPtrConstant(0));
7814
7815 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007816 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007817 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007818 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007819 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007820 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007821 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007822 MVT::v2f64, Bias)));
7823 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007824 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007825 DAG.getIntPtrConstant(0));
7826
7827 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007828 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007829
7830 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007831 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007832
Owen Anderson825b72b2009-08-11 20:47:22 +00007833 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007834 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007835 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007836 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007837 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007838 }
7839
7840 // Handle final rounding.
7841 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007842}
7843
Dan Gohmand858e902010-04-17 15:26:15 +00007844SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7845 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007846 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007847 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007848
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007849 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007850 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7851 // the optimization here.
7852 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007853 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007854
Owen Andersone50ed302009-08-10 22:56:29 +00007855 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007856 EVT DstVT = Op.getValueType();
7857 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007858 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007859 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007860 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007861
7862 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007863 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007864 if (SrcVT == MVT::i32) {
7865 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7866 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7867 getPointerTy(), StackSlot, WordOff);
7868 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007869 StackSlot, MachinePointerInfo(),
7870 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007871 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007872 OffsetSlot, MachinePointerInfo(),
7873 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007874 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7875 return Fild;
7876 }
7877
7878 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7879 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007880 StackSlot, MachinePointerInfo(),
7881 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007882 // For i64 source, we need to add the appropriate power of 2 if the input
7883 // was negative. This is the same as the optimization in
7884 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7885 // we must be careful to do the computation in x87 extended precision, not
7886 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007887 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7888 MachineMemOperand *MMO =
7889 DAG.getMachineFunction()
7890 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7891 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007892
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007893 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7894 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007895 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7896 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007897
7898 APInt FF(32, 0x5F800000ULL);
7899
7900 // Check whether the sign bit is set.
7901 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7902 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7903 ISD::SETLT);
7904
7905 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7906 SDValue FudgePtr = DAG.getConstantPool(
7907 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7908 getPointerTy());
7909
7910 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7911 SDValue Zero = DAG.getIntPtrConstant(0);
7912 SDValue Four = DAG.getIntPtrConstant(4);
7913 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7914 Zero, Four);
7915 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7916
7917 // Load the value out, extending it from f32 to f80.
7918 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007919 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007920 FudgePtr, MachinePointerInfo::getConstantPool(),
7921 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007922 // Extend everything to 80 bits to force it to be done on x87.
7923 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7924 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007925}
7926
Dan Gohman475871a2008-07-27 21:46:04 +00007927std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007928FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007929 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007930
Owen Andersone50ed302009-08-10 22:56:29 +00007931 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007932
7933 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007934 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7935 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007936 }
7937
Owen Anderson825b72b2009-08-11 20:47:22 +00007938 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7939 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007940 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007941
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007942 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007943 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007944 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007945 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007946 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007947 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007948 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007949 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007950
Evan Cheng87c89352007-10-15 20:11:21 +00007951 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7952 // stack slot.
7953 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007954 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007955 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007956 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007957
Michael J. Spencerec38de22010-10-10 22:04:20 +00007958
7959
Evan Cheng0db9fe62006-04-25 20:13:52 +00007960 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007961 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007962 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007963 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7964 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7965 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007966 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007967
Dan Gohman475871a2008-07-27 21:46:04 +00007968 SDValue Chain = DAG.getEntryNode();
7969 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007970 EVT TheVT = Op.getOperand(0).getValueType();
7971 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007972 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007973 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007974 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007975 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007976 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007977 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007978 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007979 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007980
Chris Lattner492a43e2010-09-22 01:28:21 +00007981 MachineMemOperand *MMO =
7982 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7983 MachineMemOperand::MOLoad, MemSize, MemSize);
7984 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7985 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007986 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007987 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007988 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7989 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007990
Chris Lattner07290932010-09-22 01:05:16 +00007991 MachineMemOperand *MMO =
7992 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7993 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007994
Evan Cheng0db9fe62006-04-25 20:13:52 +00007995 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007996 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007997 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7998 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007999
Chris Lattner27a6c732007-11-24 07:07:01 +00008000 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008001}
8002
Dan Gohmand858e902010-04-17 15:26:15 +00008003SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8004 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00008005 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008006 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008007
Eli Friedman948e95a2009-05-23 09:59:16 +00008008 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00008009 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008010 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8011 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008012
Chris Lattner27a6c732007-11-24 07:07:01 +00008013 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008014 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008015 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00008016}
8017
Dan Gohmand858e902010-04-17 15:26:15 +00008018SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8019 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00008020 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
8021 SDValue FIST = Vals.first, StackSlot = Vals.second;
8022 assert(FIST.getNode() && "Unexpected failure");
8023
8024 // Load the result.
8025 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008026 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00008027}
8028
Dan Gohmand858e902010-04-17 15:26:15 +00008029SDValue X86TargetLowering::LowerFABS(SDValue Op,
8030 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008031 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008032 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008033 EVT VT = Op.getValueType();
8034 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008035 if (VT.isVector())
8036 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008037 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008038 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008039 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00008040 CV.push_back(C);
8041 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008042 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008043 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00008044 CV.push_back(C);
8045 CV.push_back(C);
8046 CV.push_back(C);
8047 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008048 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008049 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008050 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008051 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008052 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008053 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008054 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008055}
8056
Dan Gohmand858e902010-04-17 15:26:15 +00008057SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008058 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008059 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008060 EVT VT = Op.getValueType();
8061 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00008062 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008063 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008064 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008065 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008066 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00008067 CV.push_back(C);
8068 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008069 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008070 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00008071 CV.push_back(C);
8072 CV.push_back(C);
8073 CV.push_back(C);
8074 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008075 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008076 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008077 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008078 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008079 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008080 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008081 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008082 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008083 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008084 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008085 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008086 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008087 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008088 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00008089 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008090}
8091
Dan Gohmand858e902010-04-17 15:26:15 +00008092SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008093 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008094 SDValue Op0 = Op.getOperand(0);
8095 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008096 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008097 EVT VT = Op.getValueType();
8098 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008099
8100 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008101 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008102 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008103 SrcVT = VT;
8104 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008105 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008106 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008107 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008108 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008109 }
8110
8111 // At this point the operands and the result should have the same
8112 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008113
Evan Cheng68c47cb2007-01-05 07:55:56 +00008114 // First get the sign bit of second operand.
8115 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008116 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008117 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8118 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008119 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008120 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8121 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8122 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8123 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008124 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008125 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008126 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008127 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008128 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008129 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008130 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008131
8132 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008133 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008134 // Op0 is MVT::f32, Op1 is MVT::f64.
8135 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8136 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8137 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008138 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008139 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008140 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008141 }
8142
Evan Cheng73d6cf12007-01-05 21:37:56 +00008143 // Clear first operand sign bit.
8144 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008145 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008146 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8147 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008148 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008149 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8150 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8151 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8152 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008153 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008154 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008155 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008156 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008157 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008158 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008159 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008160
8161 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008162 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008163}
8164
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008165SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8166 SDValue N0 = Op.getOperand(0);
8167 DebugLoc dl = Op.getDebugLoc();
8168 EVT VT = Op.getValueType();
8169
8170 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8171 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8172 DAG.getConstant(1, VT));
8173 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8174}
8175
Dan Gohman076aee32009-03-04 19:44:21 +00008176/// Emit nodes that will be selected as "test Op0,Op0", or something
8177/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008178SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008179 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008180 DebugLoc dl = Op.getDebugLoc();
8181
Dan Gohman31125812009-03-07 01:58:32 +00008182 // CF and OF aren't always set the way we want. Determine which
8183 // of these we need.
8184 bool NeedCF = false;
8185 bool NeedOF = false;
8186 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008187 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008188 case X86::COND_A: case X86::COND_AE:
8189 case X86::COND_B: case X86::COND_BE:
8190 NeedCF = true;
8191 break;
8192 case X86::COND_G: case X86::COND_GE:
8193 case X86::COND_L: case X86::COND_LE:
8194 case X86::COND_O: case X86::COND_NO:
8195 NeedOF = true;
8196 break;
Dan Gohman31125812009-03-07 01:58:32 +00008197 }
8198
Dan Gohman076aee32009-03-04 19:44:21 +00008199 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008200 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8201 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008202 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8203 // Emit a CMP with 0, which is the TEST pattern.
8204 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8205 DAG.getConstant(0, Op.getValueType()));
8206
8207 unsigned Opcode = 0;
8208 unsigned NumOperands = 0;
8209 switch (Op.getNode()->getOpcode()) {
8210 case ISD::ADD:
8211 // Due to an isel shortcoming, be conservative if this add is likely to be
8212 // selected as part of a load-modify-store instruction. When the root node
8213 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8214 // uses of other nodes in the match, such as the ADD in this case. This
8215 // leads to the ADD being left around and reselected, with the result being
8216 // two adds in the output. Alas, even if none our users are stores, that
8217 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8218 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8219 // climbing the DAG back to the root, and it doesn't seem to be worth the
8220 // effort.
8221 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00008222 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008223 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
8224 goto default_case;
8225
8226 if (ConstantSDNode *C =
8227 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8228 // An add of one will be selected as an INC.
8229 if (C->getAPIntValue() == 1) {
8230 Opcode = X86ISD::INC;
8231 NumOperands = 1;
8232 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008233 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008234
8235 // An add of negative one (subtract of one) will be selected as a DEC.
8236 if (C->getAPIntValue().isAllOnesValue()) {
8237 Opcode = X86ISD::DEC;
8238 NumOperands = 1;
8239 break;
8240 }
Dan Gohman076aee32009-03-04 19:44:21 +00008241 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008242
8243 // Otherwise use a regular EFLAGS-setting add.
8244 Opcode = X86ISD::ADD;
8245 NumOperands = 2;
8246 break;
8247 case ISD::AND: {
8248 // If the primary and result isn't used, don't bother using X86ISD::AND,
8249 // because a TEST instruction will be better.
8250 bool NonFlagUse = false;
8251 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8252 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8253 SDNode *User = *UI;
8254 unsigned UOpNo = UI.getOperandNo();
8255 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8256 // Look pass truncate.
8257 UOpNo = User->use_begin().getOperandNo();
8258 User = *User->use_begin();
8259 }
8260
8261 if (User->getOpcode() != ISD::BRCOND &&
8262 User->getOpcode() != ISD::SETCC &&
8263 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8264 NonFlagUse = true;
8265 break;
8266 }
Dan Gohman076aee32009-03-04 19:44:21 +00008267 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008268
8269 if (!NonFlagUse)
8270 break;
8271 }
8272 // FALL THROUGH
8273 case ISD::SUB:
8274 case ISD::OR:
8275 case ISD::XOR:
8276 // Due to the ISEL shortcoming noted above, be conservative if this op is
8277 // likely to be selected as part of a load-modify-store instruction.
8278 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8279 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8280 if (UI->getOpcode() == ISD::STORE)
8281 goto default_case;
8282
8283 // Otherwise use a regular EFLAGS-setting instruction.
8284 switch (Op.getNode()->getOpcode()) {
8285 default: llvm_unreachable("unexpected operator!");
8286 case ISD::SUB: Opcode = X86ISD::SUB; break;
8287 case ISD::OR: Opcode = X86ISD::OR; break;
8288 case ISD::XOR: Opcode = X86ISD::XOR; break;
8289 case ISD::AND: Opcode = X86ISD::AND; break;
8290 }
8291
8292 NumOperands = 2;
8293 break;
8294 case X86ISD::ADD:
8295 case X86ISD::SUB:
8296 case X86ISD::INC:
8297 case X86ISD::DEC:
8298 case X86ISD::OR:
8299 case X86ISD::XOR:
8300 case X86ISD::AND:
8301 return SDValue(Op.getNode(), 1);
8302 default:
8303 default_case:
8304 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008305 }
8306
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008307 if (Opcode == 0)
8308 // Emit a CMP with 0, which is the TEST pattern.
8309 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8310 DAG.getConstant(0, Op.getValueType()));
8311
8312 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8313 SmallVector<SDValue, 4> Ops;
8314 for (unsigned i = 0; i != NumOperands; ++i)
8315 Ops.push_back(Op.getOperand(i));
8316
8317 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8318 DAG.ReplaceAllUsesWith(Op, New);
8319 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008320}
8321
8322/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8323/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008324SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008325 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008326 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8327 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008328 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008329
8330 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008331 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008332}
8333
Evan Chengd40d03e2010-01-06 19:38:29 +00008334/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8335/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008336SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8337 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008338 SDValue Op0 = And.getOperand(0);
8339 SDValue Op1 = And.getOperand(1);
8340 if (Op0.getOpcode() == ISD::TRUNCATE)
8341 Op0 = Op0.getOperand(0);
8342 if (Op1.getOpcode() == ISD::TRUNCATE)
8343 Op1 = Op1.getOperand(0);
8344
Evan Chengd40d03e2010-01-06 19:38:29 +00008345 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008346 if (Op1.getOpcode() == ISD::SHL)
8347 std::swap(Op0, Op1);
8348 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008349 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8350 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008351 // If we looked past a truncate, check that it's only truncating away
8352 // known zeros.
8353 unsigned BitWidth = Op0.getValueSizeInBits();
8354 unsigned AndBitWidth = And.getValueSizeInBits();
8355 if (BitWidth > AndBitWidth) {
8356 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8357 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8358 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8359 return SDValue();
8360 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008361 LHS = Op1;
8362 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008363 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008364 } else if (Op1.getOpcode() == ISD::Constant) {
8365 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
8366 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00008367 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
8368 LHS = AndLHS.getOperand(0);
8369 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008370 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008371 }
Evan Cheng0488db92007-09-25 01:57:46 +00008372
Evan Chengd40d03e2010-01-06 19:38:29 +00008373 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008374 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008375 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008376 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008377 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008378 // Also promote i16 to i32 for performance / code size reason.
8379 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008380 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008381 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008382
Evan Chengd40d03e2010-01-06 19:38:29 +00008383 // If the operand types disagree, extend the shift amount to match. Since
8384 // BT ignores high bits (like shifts) we can use anyextend.
8385 if (LHS.getValueType() != RHS.getValueType())
8386 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008387
Evan Chengd40d03e2010-01-06 19:38:29 +00008388 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8389 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8390 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8391 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008392 }
8393
Evan Cheng54de3ea2010-01-05 06:52:31 +00008394 return SDValue();
8395}
8396
Dan Gohmand858e902010-04-17 15:26:15 +00008397SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008398
8399 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8400
Evan Cheng54de3ea2010-01-05 06:52:31 +00008401 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8402 SDValue Op0 = Op.getOperand(0);
8403 SDValue Op1 = Op.getOperand(1);
8404 DebugLoc dl = Op.getDebugLoc();
8405 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8406
8407 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008408 // Lower (X & (1 << N)) == 0 to BT(X, N).
8409 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8410 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008411 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008412 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008413 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008414 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8415 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8416 if (NewSetCC.getNode())
8417 return NewSetCC;
8418 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008419
Chris Lattner481eebc2010-12-19 21:23:48 +00008420 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8421 // these.
8422 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008423 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008424 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8425 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008426
Chris Lattner481eebc2010-12-19 21:23:48 +00008427 // If the input is a setcc, then reuse the input setcc or use a new one with
8428 // the inverted condition.
8429 if (Op0.getOpcode() == X86ISD::SETCC) {
8430 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8431 bool Invert = (CC == ISD::SETNE) ^
8432 cast<ConstantSDNode>(Op1)->isNullValue();
8433 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008434
Evan Cheng2c755ba2010-02-27 07:36:59 +00008435 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008436 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8437 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8438 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008439 }
8440
Evan Chenge5b51ac2010-04-17 06:13:15 +00008441 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008442 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008443 if (X86CC == X86::COND_INVALID)
8444 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008445
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008446 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008447 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008448 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008449}
8450
Craig Topper89af15e2011-09-18 08:03:58 +00008451// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008452// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008453static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008454 EVT VT = Op.getValueType();
8455
Duncan Sands28b77e92011-09-06 19:07:46 +00008456 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008457 "Unsupported value type for operation");
8458
8459 int NumElems = VT.getVectorNumElements();
8460 DebugLoc dl = Op.getDebugLoc();
8461 SDValue CC = Op.getOperand(2);
8462 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8463 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8464
8465 // Extract the LHS vectors
8466 SDValue LHS = Op.getOperand(0);
8467 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8468 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8469
8470 // Extract the RHS vectors
8471 SDValue RHS = Op.getOperand(1);
8472 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8473 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8474
8475 // Issue the operation on the smaller types and concatenate the result back
8476 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8477 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8478 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8479 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8480 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8481}
8482
8483
Dan Gohmand858e902010-04-17 15:26:15 +00008484SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008485 SDValue Cond;
8486 SDValue Op0 = Op.getOperand(0);
8487 SDValue Op1 = Op.getOperand(1);
8488 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008489 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008490 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8491 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008492 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008493
8494 if (isFP) {
8495 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008496 EVT EltVT = Op0.getValueType().getVectorElementType();
8497 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8498
8499 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008500 bool Swap = false;
8501
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008502 // SSE Condition code mapping:
8503 // 0 - EQ
8504 // 1 - LT
8505 // 2 - LE
8506 // 3 - UNORD
8507 // 4 - NEQ
8508 // 5 - NLT
8509 // 6 - NLE
8510 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008511 switch (SetCCOpcode) {
8512 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008513 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008514 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008515 case ISD::SETOGT:
8516 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008517 case ISD::SETLT:
8518 case ISD::SETOLT: SSECC = 1; break;
8519 case ISD::SETOGE:
8520 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008521 case ISD::SETLE:
8522 case ISD::SETOLE: SSECC = 2; break;
8523 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008524 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008525 case ISD::SETNE: SSECC = 4; break;
8526 case ISD::SETULE: Swap = true;
8527 case ISD::SETUGE: SSECC = 5; break;
8528 case ISD::SETULT: Swap = true;
8529 case ISD::SETUGT: SSECC = 6; break;
8530 case ISD::SETO: SSECC = 7; break;
8531 }
8532 if (Swap)
8533 std::swap(Op0, Op1);
8534
Nate Begemanfb8ead02008-07-25 19:05:58 +00008535 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008536 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008537 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008538 SDValue UNORD, EQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008539 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8540 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008541 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008542 }
8543 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008544 SDValue ORD, NEQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008545 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8546 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008547 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008548 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008549 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008550 }
8551 // Handle all other FP comparisons here.
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008552 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008553 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008554
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008555 // Break 256-bit integer vector compare into smaller ones.
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008556 if (!isFP && VT.getSizeInBits() == 256)
Craig Topper89af15e2011-09-18 08:03:58 +00008557 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008558
Nate Begeman30a0de92008-07-17 16:51:19 +00008559 // We are handling one of the integer comparisons here. Since SSE only has
8560 // GT and EQ comparisons for integer, swapping operands and multiple
8561 // operations may be required for some comparisons.
8562 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8563 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008564
Owen Anderson825b72b2009-08-11 20:47:22 +00008565 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008566 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008567 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008568 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008569 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8570 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008571 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008572
Nate Begeman30a0de92008-07-17 16:51:19 +00008573 switch (SetCCOpcode) {
8574 default: break;
8575 case ISD::SETNE: Invert = true;
8576 case ISD::SETEQ: Opc = EQOpc; break;
8577 case ISD::SETLT: Swap = true;
8578 case ISD::SETGT: Opc = GTOpc; break;
8579 case ISD::SETGE: Swap = true;
8580 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8581 case ISD::SETULT: Swap = true;
8582 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8583 case ISD::SETUGE: Swap = true;
8584 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8585 }
8586 if (Swap)
8587 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008588
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008589 // Check that the operation in question is available (most are plain SSE2,
8590 // but PCMPGTQ and PCMPEQQ have different requirements).
8591 if (Opc == X86ISD::PCMPGTQ && !Subtarget->hasSSE42() && !Subtarget->hasAVX())
8592 return SDValue();
8593 if (Opc == X86ISD::PCMPEQQ && !Subtarget->hasSSE41() && !Subtarget->hasAVX())
8594 return SDValue();
8595
Nate Begeman30a0de92008-07-17 16:51:19 +00008596 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8597 // bits of the inputs before performing those operations.
8598 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008599 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008600 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8601 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008602 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008603 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8604 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008605 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8606 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008607 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008608
Dale Johannesenace16102009-02-03 19:33:06 +00008609 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008610
8611 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008612 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008613 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008614
Nate Begeman30a0de92008-07-17 16:51:19 +00008615 return Result;
8616}
Evan Cheng0488db92007-09-25 01:57:46 +00008617
Evan Cheng370e5342008-12-03 08:38:43 +00008618// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008619static bool isX86LogicalCmp(SDValue Op) {
8620 unsigned Opc = Op.getNode()->getOpcode();
8621 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8622 return true;
8623 if (Op.getResNo() == 1 &&
8624 (Opc == X86ISD::ADD ||
8625 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008626 Opc == X86ISD::ADC ||
8627 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008628 Opc == X86ISD::SMUL ||
8629 Opc == X86ISD::UMUL ||
8630 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008631 Opc == X86ISD::DEC ||
8632 Opc == X86ISD::OR ||
8633 Opc == X86ISD::XOR ||
8634 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008635 return true;
8636
Chris Lattner9637d5b2010-12-05 07:49:54 +00008637 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8638 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008639
Dan Gohman076aee32009-03-04 19:44:21 +00008640 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008641}
8642
Chris Lattnera2b56002010-12-05 01:23:24 +00008643static bool isZero(SDValue V) {
8644 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8645 return C && C->isNullValue();
8646}
8647
Chris Lattner96908b12010-12-05 02:00:51 +00008648static bool isAllOnes(SDValue V) {
8649 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8650 return C && C->isAllOnesValue();
8651}
8652
Dan Gohmand858e902010-04-17 15:26:15 +00008653SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008654 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008655 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008656 SDValue Op1 = Op.getOperand(1);
8657 SDValue Op2 = Op.getOperand(2);
8658 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008659 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008660
Dan Gohman1a492952009-10-20 16:22:37 +00008661 if (Cond.getOpcode() == ISD::SETCC) {
8662 SDValue NewCond = LowerSETCC(Cond, DAG);
8663 if (NewCond.getNode())
8664 Cond = NewCond;
8665 }
Evan Cheng734503b2006-09-11 02:19:56 +00008666
Chris Lattnera2b56002010-12-05 01:23:24 +00008667 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008668 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008669 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008670 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008671 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008672 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8673 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008674 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008675
Chris Lattnera2b56002010-12-05 01:23:24 +00008676 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008677
8678 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008679 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8680 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008681
8682 SDValue CmpOp0 = Cmp.getOperand(0);
8683 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8684 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008685
Chris Lattner96908b12010-12-05 02:00:51 +00008686 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008687 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8688 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008689
Chris Lattner96908b12010-12-05 02:00:51 +00008690 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8691 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008692
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008693 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008694 if (N2C == 0 || !N2C->isNullValue())
8695 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8696 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008697 }
8698 }
8699
Chris Lattnera2b56002010-12-05 01:23:24 +00008700 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008701 if (Cond.getOpcode() == ISD::AND &&
8702 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8703 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008704 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008705 Cond = Cond.getOperand(0);
8706 }
8707
Evan Cheng3f41d662007-10-08 22:16:29 +00008708 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8709 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008710 unsigned CondOpcode = Cond.getOpcode();
8711 if (CondOpcode == X86ISD::SETCC ||
8712 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008713 CC = Cond.getOperand(0);
8714
Dan Gohman475871a2008-07-27 21:46:04 +00008715 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008716 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008717 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008718
Evan Cheng3f41d662007-10-08 22:16:29 +00008719 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008720 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008721 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008722 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008723
Chris Lattnerd1980a52009-03-12 06:52:53 +00008724 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8725 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008726 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008727 addTest = false;
8728 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008729 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8730 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8731 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8732 Cond.getOperand(0).getValueType() != MVT::i8)) {
8733 SDValue LHS = Cond.getOperand(0);
8734 SDValue RHS = Cond.getOperand(1);
8735 unsigned X86Opcode;
8736 unsigned X86Cond;
8737 SDVTList VTs;
8738 switch (CondOpcode) {
8739 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8740 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8741 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8742 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8743 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8744 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8745 default: llvm_unreachable("unexpected overflowing operator");
8746 }
8747 if (CondOpcode == ISD::UMULO)
8748 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8749 MVT::i32);
8750 else
8751 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8752
8753 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
8754
8755 if (CondOpcode == ISD::UMULO)
8756 Cond = X86Op.getValue(2);
8757 else
8758 Cond = X86Op.getValue(1);
8759
8760 CC = DAG.getConstant(X86Cond, MVT::i8);
8761 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00008762 }
8763
8764 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008765 // Look pass the truncate.
8766 if (Cond.getOpcode() == ISD::TRUNCATE)
8767 Cond = Cond.getOperand(0);
8768
8769 // We know the result of AND is compared against zero. Try to match
8770 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008771 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008772 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008773 if (NewSetCC.getNode()) {
8774 CC = NewSetCC.getOperand(0);
8775 Cond = NewSetCC.getOperand(1);
8776 addTest = false;
8777 }
8778 }
8779 }
8780
8781 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008782 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008783 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008784 }
8785
Benjamin Kramere915ff32010-12-22 23:09:28 +00008786 // a < b ? -1 : 0 -> RES = ~setcc_carry
8787 // a < b ? 0 : -1 -> RES = setcc_carry
8788 // a >= b ? -1 : 0 -> RES = setcc_carry
8789 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8790 if (Cond.getOpcode() == X86ISD::CMP) {
8791 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8792
8793 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8794 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8795 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8796 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8797 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8798 return DAG.getNOT(DL, Res, Res.getValueType());
8799 return Res;
8800 }
8801 }
8802
Evan Cheng0488db92007-09-25 01:57:46 +00008803 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8804 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008805 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008806 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008807 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008808}
8809
Evan Cheng370e5342008-12-03 08:38:43 +00008810// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8811// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8812// from the AND / OR.
8813static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8814 Opc = Op.getOpcode();
8815 if (Opc != ISD::OR && Opc != ISD::AND)
8816 return false;
8817 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8818 Op.getOperand(0).hasOneUse() &&
8819 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8820 Op.getOperand(1).hasOneUse());
8821}
8822
Evan Cheng961d6d42009-02-02 08:19:07 +00008823// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8824// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008825static bool isXor1OfSetCC(SDValue Op) {
8826 if (Op.getOpcode() != ISD::XOR)
8827 return false;
8828 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8829 if (N1C && N1C->getAPIntValue() == 1) {
8830 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8831 Op.getOperand(0).hasOneUse();
8832 }
8833 return false;
8834}
8835
Dan Gohmand858e902010-04-17 15:26:15 +00008836SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008837 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008838 SDValue Chain = Op.getOperand(0);
8839 SDValue Cond = Op.getOperand(1);
8840 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008841 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008842 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00008843 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00008844
Dan Gohman1a492952009-10-20 16:22:37 +00008845 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00008846 // Check for setcc([su]{add,sub,mul}o == 0).
8847 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
8848 isa<ConstantSDNode>(Cond.getOperand(1)) &&
8849 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
8850 Cond.getOperand(0).getResNo() == 1 &&
8851 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
8852 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
8853 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
8854 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
8855 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
8856 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
8857 Inverted = true;
8858 Cond = Cond.getOperand(0);
8859 } else {
8860 SDValue NewCond = LowerSETCC(Cond, DAG);
8861 if (NewCond.getNode())
8862 Cond = NewCond;
8863 }
Dan Gohman1a492952009-10-20 16:22:37 +00008864 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008865#if 0
8866 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008867 else if (Cond.getOpcode() == X86ISD::ADD ||
8868 Cond.getOpcode() == X86ISD::SUB ||
8869 Cond.getOpcode() == X86ISD::SMUL ||
8870 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008871 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008872#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008873
Evan Chengad9c0a32009-12-15 00:53:42 +00008874 // Look pass (and (setcc_carry (cmp ...)), 1).
8875 if (Cond.getOpcode() == ISD::AND &&
8876 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8877 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008878 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008879 Cond = Cond.getOperand(0);
8880 }
8881
Evan Cheng3f41d662007-10-08 22:16:29 +00008882 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8883 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008884 unsigned CondOpcode = Cond.getOpcode();
8885 if (CondOpcode == X86ISD::SETCC ||
8886 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008887 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008888
Dan Gohman475871a2008-07-27 21:46:04 +00008889 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008890 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008891 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008892 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008893 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008894 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008895 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008896 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008897 default: break;
8898 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008899 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008900 // These can only come from an arithmetic instruction with overflow,
8901 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008902 Cond = Cond.getNode()->getOperand(1);
8903 addTest = false;
8904 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008905 }
Evan Cheng0488db92007-09-25 01:57:46 +00008906 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008907 }
8908 CondOpcode = Cond.getOpcode();
8909 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8910 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8911 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8912 Cond.getOperand(0).getValueType() != MVT::i8)) {
8913 SDValue LHS = Cond.getOperand(0);
8914 SDValue RHS = Cond.getOperand(1);
8915 unsigned X86Opcode;
8916 unsigned X86Cond;
8917 SDVTList VTs;
8918 switch (CondOpcode) {
8919 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8920 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8921 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8922 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8923 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8924 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8925 default: llvm_unreachable("unexpected overflowing operator");
8926 }
8927 if (Inverted)
8928 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
8929 if (CondOpcode == ISD::UMULO)
8930 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8931 MVT::i32);
8932 else
8933 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8934
8935 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
8936
8937 if (CondOpcode == ISD::UMULO)
8938 Cond = X86Op.getValue(2);
8939 else
8940 Cond = X86Op.getValue(1);
8941
8942 CC = DAG.getConstant(X86Cond, MVT::i8);
8943 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00008944 } else {
8945 unsigned CondOpc;
8946 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8947 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008948 if (CondOpc == ISD::OR) {
8949 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8950 // two branches instead of an explicit OR instruction with a
8951 // separate test.
8952 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008953 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008954 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008955 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008956 Chain, Dest, CC, Cmp);
8957 CC = Cond.getOperand(1).getOperand(0);
8958 Cond = Cmp;
8959 addTest = false;
8960 }
8961 } else { // ISD::AND
8962 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8963 // two branches instead of an explicit AND instruction with a
8964 // separate test. However, we only do this if this block doesn't
8965 // have a fall-through edge, because this requires an explicit
8966 // jmp when the condition is false.
8967 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008968 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008969 Op.getNode()->hasOneUse()) {
8970 X86::CondCode CCode =
8971 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8972 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008973 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008974 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008975 // Look for an unconditional branch following this conditional branch.
8976 // We need this because we need to reverse the successors in order
8977 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008978 if (User->getOpcode() == ISD::BR) {
8979 SDValue FalseBB = User->getOperand(1);
8980 SDNode *NewBR =
8981 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008982 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008983 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008984 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008985
Dale Johannesene4d209d2009-02-03 20:21:25 +00008986 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008987 Chain, Dest, CC, Cmp);
8988 X86::CondCode CCode =
8989 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8990 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008991 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008992 Cond = Cmp;
8993 addTest = false;
8994 }
8995 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008996 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008997 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8998 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8999 // It should be transformed during dag combiner except when the condition
9000 // is set by a arithmetics with overflow node.
9001 X86::CondCode CCode =
9002 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9003 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009004 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009005 Cond = Cond.getOperand(0).getOperand(1);
9006 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009007 } else if (Cond.getOpcode() == ISD::SETCC &&
9008 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9009 // For FCMP_OEQ, we can emit
9010 // two branches instead of an explicit AND instruction with a
9011 // separate test. However, we only do this if this block doesn't
9012 // have a fall-through edge, because this requires an explicit
9013 // jmp when the condition is false.
9014 if (Op.getNode()->hasOneUse()) {
9015 SDNode *User = *Op.getNode()->use_begin();
9016 // Look for an unconditional branch following this conditional branch.
9017 // We need this because we need to reverse the successors in order
9018 // to implement FCMP_OEQ.
9019 if (User->getOpcode() == ISD::BR) {
9020 SDValue FalseBB = User->getOperand(1);
9021 SDNode *NewBR =
9022 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9023 assert(NewBR == User);
9024 (void)NewBR;
9025 Dest = FalseBB;
9026
9027 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9028 Cond.getOperand(0), Cond.getOperand(1));
9029 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9030 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9031 Chain, Dest, CC, Cmp);
9032 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9033 Cond = Cmp;
9034 addTest = false;
9035 }
9036 }
9037 } else if (Cond.getOpcode() == ISD::SETCC &&
9038 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9039 // For FCMP_UNE, we can emit
9040 // two branches instead of an explicit AND instruction with a
9041 // separate test. However, we only do this if this block doesn't
9042 // have a fall-through edge, because this requires an explicit
9043 // jmp when the condition is false.
9044 if (Op.getNode()->hasOneUse()) {
9045 SDNode *User = *Op.getNode()->use_begin();
9046 // Look for an unconditional branch following this conditional branch.
9047 // We need this because we need to reverse the successors in order
9048 // to implement FCMP_UNE.
9049 if (User->getOpcode() == ISD::BR) {
9050 SDValue FalseBB = User->getOperand(1);
9051 SDNode *NewBR =
9052 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9053 assert(NewBR == User);
9054 (void)NewBR;
9055
9056 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9057 Cond.getOperand(0), Cond.getOperand(1));
9058 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9059 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9060 Chain, Dest, CC, Cmp);
9061 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9062 Cond = Cmp;
9063 addTest = false;
9064 Dest = FalseBB;
9065 }
9066 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009067 }
Evan Cheng0488db92007-09-25 01:57:46 +00009068 }
9069
9070 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009071 // Look pass the truncate.
9072 if (Cond.getOpcode() == ISD::TRUNCATE)
9073 Cond = Cond.getOperand(0);
9074
9075 // We know the result of AND is compared against zero. Try to match
9076 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009077 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009078 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9079 if (NewSetCC.getNode()) {
9080 CC = NewSetCC.getOperand(0);
9081 Cond = NewSetCC.getOperand(1);
9082 addTest = false;
9083 }
9084 }
9085 }
9086
9087 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009088 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009089 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009090 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00009091 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009092 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009093}
9094
Anton Korobeynikove060b532007-04-17 19:34:00 +00009095
9096// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9097// Calls to _alloca is needed to probe the stack when allocating more than 4k
9098// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9099// that the guard pages used by the OS virtual memory manager are allocated in
9100// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009101SDValue
9102X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009103 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009104 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
9105 EnableSegmentedStacks) &&
9106 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009107 "are being used");
9108 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009109 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009110
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009111 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009112 SDValue Chain = Op.getOperand(0);
9113 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009114 // FIXME: Ensure alignment here
9115
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009116 bool Is64Bit = Subtarget->is64Bit();
9117 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009118
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009119 if (EnableSegmentedStacks) {
9120 MachineFunction &MF = DAG.getMachineFunction();
9121 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009122
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009123 if (Is64Bit) {
9124 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009125 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009126 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009127
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009128 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
9129 I != E; I++)
9130 if (I->hasNestAttr())
9131 report_fatal_error("Cannot use segmented stacks with functions that "
9132 "have nested arguments.");
9133 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009134
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009135 const TargetRegisterClass *AddrRegClass =
9136 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9137 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9138 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9139 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9140 DAG.getRegister(Vreg, SPTy));
9141 SDValue Ops1[2] = { Value, Chain };
9142 return DAG.getMergeValues(Ops1, 2, dl);
9143 } else {
9144 SDValue Flag;
9145 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009146
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009147 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9148 Flag = Chain.getValue(1);
9149 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009150
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009151 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9152 Flag = Chain.getValue(1);
9153
9154 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9155
9156 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9157 return DAG.getMergeValues(Ops1, 2, dl);
9158 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009159}
9160
Dan Gohmand858e902010-04-17 15:26:15 +00009161SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009162 MachineFunction &MF = DAG.getMachineFunction();
9163 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9164
Dan Gohman69de1932008-02-06 22:27:42 +00009165 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009166 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009167
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009168 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009169 // vastart just stores the address of the VarArgsFrameIndex slot into the
9170 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009171 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9172 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009173 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9174 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009175 }
9176
9177 // __va_list_tag:
9178 // gp_offset (0 - 6 * 8)
9179 // fp_offset (48 - 48 + 8 * 16)
9180 // overflow_arg_area (point to parameters coming in memory).
9181 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009182 SmallVector<SDValue, 8> MemOps;
9183 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009184 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009185 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009186 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9187 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009188 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009189 MemOps.push_back(Store);
9190
9191 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009192 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009193 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009194 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009195 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9196 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009197 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009198 MemOps.push_back(Store);
9199
9200 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009201 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009202 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009203 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9204 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009205 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9206 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009207 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009208 MemOps.push_back(Store);
9209
9210 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009211 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009212 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009213 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9214 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009215 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9216 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009217 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009218 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009219 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009220}
9221
Dan Gohmand858e902010-04-17 15:26:15 +00009222SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009223 assert(Subtarget->is64Bit() &&
9224 "LowerVAARG only handles 64-bit va_arg!");
9225 assert((Subtarget->isTargetLinux() ||
9226 Subtarget->isTargetDarwin()) &&
9227 "Unhandled target in LowerVAARG");
9228 assert(Op.getNode()->getNumOperands() == 4);
9229 SDValue Chain = Op.getOperand(0);
9230 SDValue SrcPtr = Op.getOperand(1);
9231 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9232 unsigned Align = Op.getConstantOperandVal(3);
9233 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009234
Dan Gohman320afb82010-10-12 18:00:49 +00009235 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009236 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009237 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9238 uint8_t ArgMode;
9239
9240 // Decide which area this value should be read from.
9241 // TODO: Implement the AMD64 ABI in its entirety. This simple
9242 // selection mechanism works only for the basic types.
9243 if (ArgVT == MVT::f80) {
9244 llvm_unreachable("va_arg for f80 not yet implemented");
9245 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9246 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9247 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9248 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9249 } else {
9250 llvm_unreachable("Unhandled argument type in LowerVAARG");
9251 }
9252
9253 if (ArgMode == 2) {
9254 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00009255 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009256 !(DAG.getMachineFunction()
9257 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009258 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009259 }
9260
9261 // Insert VAARG_64 node into the DAG
9262 // VAARG_64 returns two values: Variable Argument Address, Chain
9263 SmallVector<SDValue, 11> InstOps;
9264 InstOps.push_back(Chain);
9265 InstOps.push_back(SrcPtr);
9266 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9267 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9268 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9269 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9270 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9271 VTs, &InstOps[0], InstOps.size(),
9272 MVT::i64,
9273 MachinePointerInfo(SV),
9274 /*Align=*/0,
9275 /*Volatile=*/false,
9276 /*ReadMem=*/true,
9277 /*WriteMem=*/true);
9278 Chain = VAARG.getValue(1);
9279
9280 // Load the next argument and return it
9281 return DAG.getLoad(ArgVT, dl,
9282 Chain,
9283 VAARG,
9284 MachinePointerInfo(),
9285 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009286}
9287
Dan Gohmand858e902010-04-17 15:26:15 +00009288SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009289 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009290 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009291 SDValue Chain = Op.getOperand(0);
9292 SDValue DstPtr = Op.getOperand(1);
9293 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009294 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9295 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009296 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009297
Chris Lattnere72f2022010-09-21 05:40:29 +00009298 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009299 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009300 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009301 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009302}
9303
Dan Gohman475871a2008-07-27 21:46:04 +00009304SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009305X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009306 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009307 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009308 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009309 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009310 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009311 case Intrinsic::x86_sse_comieq_ss:
9312 case Intrinsic::x86_sse_comilt_ss:
9313 case Intrinsic::x86_sse_comile_ss:
9314 case Intrinsic::x86_sse_comigt_ss:
9315 case Intrinsic::x86_sse_comige_ss:
9316 case Intrinsic::x86_sse_comineq_ss:
9317 case Intrinsic::x86_sse_ucomieq_ss:
9318 case Intrinsic::x86_sse_ucomilt_ss:
9319 case Intrinsic::x86_sse_ucomile_ss:
9320 case Intrinsic::x86_sse_ucomigt_ss:
9321 case Intrinsic::x86_sse_ucomige_ss:
9322 case Intrinsic::x86_sse_ucomineq_ss:
9323 case Intrinsic::x86_sse2_comieq_sd:
9324 case Intrinsic::x86_sse2_comilt_sd:
9325 case Intrinsic::x86_sse2_comile_sd:
9326 case Intrinsic::x86_sse2_comigt_sd:
9327 case Intrinsic::x86_sse2_comige_sd:
9328 case Intrinsic::x86_sse2_comineq_sd:
9329 case Intrinsic::x86_sse2_ucomieq_sd:
9330 case Intrinsic::x86_sse2_ucomilt_sd:
9331 case Intrinsic::x86_sse2_ucomile_sd:
9332 case Intrinsic::x86_sse2_ucomigt_sd:
9333 case Intrinsic::x86_sse2_ucomige_sd:
9334 case Intrinsic::x86_sse2_ucomineq_sd: {
9335 unsigned Opc = 0;
9336 ISD::CondCode CC = ISD::SETCC_INVALID;
9337 switch (IntNo) {
9338 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009339 case Intrinsic::x86_sse_comieq_ss:
9340 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009341 Opc = X86ISD::COMI;
9342 CC = ISD::SETEQ;
9343 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009344 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009345 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009346 Opc = X86ISD::COMI;
9347 CC = ISD::SETLT;
9348 break;
9349 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009350 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009351 Opc = X86ISD::COMI;
9352 CC = ISD::SETLE;
9353 break;
9354 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009355 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009356 Opc = X86ISD::COMI;
9357 CC = ISD::SETGT;
9358 break;
9359 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009360 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009361 Opc = X86ISD::COMI;
9362 CC = ISD::SETGE;
9363 break;
9364 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009365 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009366 Opc = X86ISD::COMI;
9367 CC = ISD::SETNE;
9368 break;
9369 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009370 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009371 Opc = X86ISD::UCOMI;
9372 CC = ISD::SETEQ;
9373 break;
9374 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009375 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009376 Opc = X86ISD::UCOMI;
9377 CC = ISD::SETLT;
9378 break;
9379 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009380 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009381 Opc = X86ISD::UCOMI;
9382 CC = ISD::SETLE;
9383 break;
9384 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009385 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009386 Opc = X86ISD::UCOMI;
9387 CC = ISD::SETGT;
9388 break;
9389 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009390 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009391 Opc = X86ISD::UCOMI;
9392 CC = ISD::SETGE;
9393 break;
9394 case Intrinsic::x86_sse_ucomineq_ss:
9395 case Intrinsic::x86_sse2_ucomineq_sd:
9396 Opc = X86ISD::UCOMI;
9397 CC = ISD::SETNE;
9398 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009399 }
Evan Cheng734503b2006-09-11 02:19:56 +00009400
Dan Gohman475871a2008-07-27 21:46:04 +00009401 SDValue LHS = Op.getOperand(1);
9402 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009403 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009404 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009405 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9406 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9407 DAG.getConstant(X86CC, MVT::i8), Cond);
9408 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009409 }
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009410 // Arithmetic intrinsics.
9411 case Intrinsic::x86_sse3_hadd_ps:
9412 case Intrinsic::x86_sse3_hadd_pd:
9413 case Intrinsic::x86_avx_hadd_ps_256:
9414 case Intrinsic::x86_avx_hadd_pd_256:
9415 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9416 Op.getOperand(1), Op.getOperand(2));
9417 case Intrinsic::x86_sse3_hsub_ps:
9418 case Intrinsic::x86_sse3_hsub_pd:
9419 case Intrinsic::x86_avx_hsub_ps_256:
9420 case Intrinsic::x86_avx_hsub_pd_256:
9421 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9422 Op.getOperand(1), Op.getOperand(2));
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009423 // ptest and testp intrinsics. The intrinsic these come from are designed to
9424 // return an integer value, not just an instruction so lower it to the ptest
9425 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009426 case Intrinsic::x86_sse41_ptestz:
9427 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009428 case Intrinsic::x86_sse41_ptestnzc:
9429 case Intrinsic::x86_avx_ptestz_256:
9430 case Intrinsic::x86_avx_ptestc_256:
9431 case Intrinsic::x86_avx_ptestnzc_256:
9432 case Intrinsic::x86_avx_vtestz_ps:
9433 case Intrinsic::x86_avx_vtestc_ps:
9434 case Intrinsic::x86_avx_vtestnzc_ps:
9435 case Intrinsic::x86_avx_vtestz_pd:
9436 case Intrinsic::x86_avx_vtestc_pd:
9437 case Intrinsic::x86_avx_vtestnzc_pd:
9438 case Intrinsic::x86_avx_vtestz_ps_256:
9439 case Intrinsic::x86_avx_vtestc_ps_256:
9440 case Intrinsic::x86_avx_vtestnzc_ps_256:
9441 case Intrinsic::x86_avx_vtestz_pd_256:
9442 case Intrinsic::x86_avx_vtestc_pd_256:
9443 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9444 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009445 unsigned X86CC = 0;
9446 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009447 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009448 case Intrinsic::x86_avx_vtestz_ps:
9449 case Intrinsic::x86_avx_vtestz_pd:
9450 case Intrinsic::x86_avx_vtestz_ps_256:
9451 case Intrinsic::x86_avx_vtestz_pd_256:
9452 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009453 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009454 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009455 // ZF = 1
9456 X86CC = X86::COND_E;
9457 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009458 case Intrinsic::x86_avx_vtestc_ps:
9459 case Intrinsic::x86_avx_vtestc_pd:
9460 case Intrinsic::x86_avx_vtestc_ps_256:
9461 case Intrinsic::x86_avx_vtestc_pd_256:
9462 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009463 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009464 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009465 // CF = 1
9466 X86CC = X86::COND_B;
9467 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009468 case Intrinsic::x86_avx_vtestnzc_ps:
9469 case Intrinsic::x86_avx_vtestnzc_pd:
9470 case Intrinsic::x86_avx_vtestnzc_ps_256:
9471 case Intrinsic::x86_avx_vtestnzc_pd_256:
9472 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009473 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009474 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009475 // ZF and CF = 0
9476 X86CC = X86::COND_A;
9477 break;
9478 }
Eric Christopherfd179292009-08-27 18:07:15 +00009479
Eric Christopher71c67532009-07-29 00:28:05 +00009480 SDValue LHS = Op.getOperand(1);
9481 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009482 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9483 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009484 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9485 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9486 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009487 }
Evan Cheng5759f972008-05-04 09:15:50 +00009488
9489 // Fix vector shift instructions where the last operand is a non-immediate
9490 // i32 value.
9491 case Intrinsic::x86_sse2_pslli_w:
9492 case Intrinsic::x86_sse2_pslli_d:
9493 case Intrinsic::x86_sse2_pslli_q:
9494 case Intrinsic::x86_sse2_psrli_w:
9495 case Intrinsic::x86_sse2_psrli_d:
9496 case Intrinsic::x86_sse2_psrli_q:
9497 case Intrinsic::x86_sse2_psrai_w:
9498 case Intrinsic::x86_sse2_psrai_d:
9499 case Intrinsic::x86_mmx_pslli_w:
9500 case Intrinsic::x86_mmx_pslli_d:
9501 case Intrinsic::x86_mmx_pslli_q:
9502 case Intrinsic::x86_mmx_psrli_w:
9503 case Intrinsic::x86_mmx_psrli_d:
9504 case Intrinsic::x86_mmx_psrli_q:
9505 case Intrinsic::x86_mmx_psrai_w:
9506 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009507 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009508 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009509 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009510
9511 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009512 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009513 switch (IntNo) {
9514 case Intrinsic::x86_sse2_pslli_w:
9515 NewIntNo = Intrinsic::x86_sse2_psll_w;
9516 break;
9517 case Intrinsic::x86_sse2_pslli_d:
9518 NewIntNo = Intrinsic::x86_sse2_psll_d;
9519 break;
9520 case Intrinsic::x86_sse2_pslli_q:
9521 NewIntNo = Intrinsic::x86_sse2_psll_q;
9522 break;
9523 case Intrinsic::x86_sse2_psrli_w:
9524 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9525 break;
9526 case Intrinsic::x86_sse2_psrli_d:
9527 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9528 break;
9529 case Intrinsic::x86_sse2_psrli_q:
9530 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9531 break;
9532 case Intrinsic::x86_sse2_psrai_w:
9533 NewIntNo = Intrinsic::x86_sse2_psra_w;
9534 break;
9535 case Intrinsic::x86_sse2_psrai_d:
9536 NewIntNo = Intrinsic::x86_sse2_psra_d;
9537 break;
9538 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009539 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009540 switch (IntNo) {
9541 case Intrinsic::x86_mmx_pslli_w:
9542 NewIntNo = Intrinsic::x86_mmx_psll_w;
9543 break;
9544 case Intrinsic::x86_mmx_pslli_d:
9545 NewIntNo = Intrinsic::x86_mmx_psll_d;
9546 break;
9547 case Intrinsic::x86_mmx_pslli_q:
9548 NewIntNo = Intrinsic::x86_mmx_psll_q;
9549 break;
9550 case Intrinsic::x86_mmx_psrli_w:
9551 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9552 break;
9553 case Intrinsic::x86_mmx_psrli_d:
9554 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9555 break;
9556 case Intrinsic::x86_mmx_psrli_q:
9557 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9558 break;
9559 case Intrinsic::x86_mmx_psrai_w:
9560 NewIntNo = Intrinsic::x86_mmx_psra_w;
9561 break;
9562 case Intrinsic::x86_mmx_psrai_d:
9563 NewIntNo = Intrinsic::x86_mmx_psra_d;
9564 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009565 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009566 }
9567 break;
9568 }
9569 }
Mon P Wangefa42202009-09-03 19:56:25 +00009570
9571 // The vector shift intrinsics with scalars uses 32b shift amounts but
9572 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9573 // to be zero.
9574 SDValue ShOps[4];
9575 ShOps[0] = ShAmt;
9576 ShOps[1] = DAG.getConstant(0, MVT::i32);
9577 if (ShAmtVT == MVT::v4i32) {
9578 ShOps[2] = DAG.getUNDEF(MVT::i32);
9579 ShOps[3] = DAG.getUNDEF(MVT::i32);
9580 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9581 } else {
9582 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009583// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009584 }
9585
Owen Andersone50ed302009-08-10 22:56:29 +00009586 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009587 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009588 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009589 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009590 Op.getOperand(1), ShAmt);
9591 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009592 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009593}
Evan Cheng72261582005-12-20 06:22:03 +00009594
Dan Gohmand858e902010-04-17 15:26:15 +00009595SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9596 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009597 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9598 MFI->setReturnAddressIsTaken(true);
9599
Bill Wendling64e87322009-01-16 19:25:27 +00009600 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009601 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009602
9603 if (Depth > 0) {
9604 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9605 SDValue Offset =
9606 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009607 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009608 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009609 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009610 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00009611 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009612 }
9613
9614 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009615 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009616 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009617 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009618}
9619
Dan Gohmand858e902010-04-17 15:26:15 +00009620SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009621 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9622 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009623
Owen Andersone50ed302009-08-10 22:56:29 +00009624 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009625 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009626 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9627 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009628 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009629 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009630 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9631 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00009632 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009633 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009634}
9635
Dan Gohman475871a2008-07-27 21:46:04 +00009636SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009637 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009638 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009639}
9640
Dan Gohmand858e902010-04-17 15:26:15 +00009641SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009642 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009643 SDValue Chain = Op.getOperand(0);
9644 SDValue Offset = Op.getOperand(1);
9645 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009646 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009647
Dan Gohmand8816272010-08-11 18:14:00 +00009648 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9649 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9650 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009651 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009652
Dan Gohmand8816272010-08-11 18:14:00 +00009653 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9654 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009655 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009656 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9657 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009658 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009659 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009660
Dale Johannesene4d209d2009-02-03 20:21:25 +00009661 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009662 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009663 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009664}
9665
Duncan Sands4a544a72011-09-06 13:37:06 +00009666SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9667 SelectionDAG &DAG) const {
9668 return Op.getOperand(0);
9669}
9670
9671SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9672 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009673 SDValue Root = Op.getOperand(0);
9674 SDValue Trmp = Op.getOperand(1); // trampoline
9675 SDValue FPtr = Op.getOperand(2); // nested function
9676 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009677 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009678
Dan Gohman69de1932008-02-06 22:27:42 +00009679 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009680
9681 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009682 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009683
9684 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009685 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9686 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009687
Evan Cheng0e6a0522011-07-18 20:57:22 +00009688 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9689 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009690
9691 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9692
9693 // Load the pointer to the nested function into R11.
9694 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009695 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009696 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009697 Addr, MachinePointerInfo(TrmpAddr),
9698 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009699
Owen Anderson825b72b2009-08-11 20:47:22 +00009700 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9701 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009702 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9703 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009704 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009705
9706 // Load the 'nest' parameter value into R10.
9707 // R10 is specified in X86CallingConv.td
9708 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009709 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9710 DAG.getConstant(10, MVT::i64));
9711 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009712 Addr, MachinePointerInfo(TrmpAddr, 10),
9713 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009714
Owen Anderson825b72b2009-08-11 20:47:22 +00009715 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9716 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009717 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9718 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009719 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009720
9721 // Jump to the nested function.
9722 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009723 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9724 DAG.getConstant(20, MVT::i64));
9725 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009726 Addr, MachinePointerInfo(TrmpAddr, 20),
9727 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009728
9729 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009730 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9731 DAG.getConstant(22, MVT::i64));
9732 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009733 MachinePointerInfo(TrmpAddr, 22),
9734 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009735
Duncan Sands4a544a72011-09-06 13:37:06 +00009736 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009737 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009738 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009739 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009740 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009741 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009742
9743 switch (CC) {
9744 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009745 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009746 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009747 case CallingConv::X86_StdCall: {
9748 // Pass 'nest' parameter in ECX.
9749 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009750 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009751
9752 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009753 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009754 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009755
Chris Lattner58d74912008-03-12 17:45:29 +00009756 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009757 unsigned InRegCount = 0;
9758 unsigned Idx = 1;
9759
9760 for (FunctionType::param_iterator I = FTy->param_begin(),
9761 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009762 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009763 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009764 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009765
9766 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009767 report_fatal_error("Nest register in use - reduce number of inreg"
9768 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009769 }
9770 }
9771 break;
9772 }
9773 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009774 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009775 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009776 // Pass 'nest' parameter in EAX.
9777 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009778 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009779 break;
9780 }
9781
Dan Gohman475871a2008-07-27 21:46:04 +00009782 SDValue OutChains[4];
9783 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009784
Owen Anderson825b72b2009-08-11 20:47:22 +00009785 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9786 DAG.getConstant(10, MVT::i32));
9787 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009788
Chris Lattnera62fe662010-02-05 19:20:30 +00009789 // This is storing the opcode for MOV32ri.
9790 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009791 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009792 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009793 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009794 Trmp, MachinePointerInfo(TrmpAddr),
9795 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009796
Owen Anderson825b72b2009-08-11 20:47:22 +00009797 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9798 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009799 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9800 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009801 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009802
Chris Lattnera62fe662010-02-05 19:20:30 +00009803 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009804 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9805 DAG.getConstant(5, MVT::i32));
9806 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009807 MachinePointerInfo(TrmpAddr, 5),
9808 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009809
Owen Anderson825b72b2009-08-11 20:47:22 +00009810 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9811 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009812 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9813 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009814 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009815
Duncan Sands4a544a72011-09-06 13:37:06 +00009816 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009817 }
9818}
9819
Dan Gohmand858e902010-04-17 15:26:15 +00009820SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9821 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009822 /*
9823 The rounding mode is in bits 11:10 of FPSR, and has the following
9824 settings:
9825 00 Round to nearest
9826 01 Round to -inf
9827 10 Round to +inf
9828 11 Round to 0
9829
9830 FLT_ROUNDS, on the other hand, expects the following:
9831 -1 Undefined
9832 0 Round to 0
9833 1 Round to nearest
9834 2 Round to +inf
9835 3 Round to -inf
9836
9837 To perform the conversion, we do:
9838 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9839 */
9840
9841 MachineFunction &MF = DAG.getMachineFunction();
9842 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009843 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009844 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009845 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009846 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009847
9848 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009849 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009850 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009851
Michael J. Spencerec38de22010-10-10 22:04:20 +00009852
Chris Lattner2156b792010-09-22 01:11:26 +00009853 MachineMemOperand *MMO =
9854 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9855 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009856
Chris Lattner2156b792010-09-22 01:11:26 +00009857 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9858 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9859 DAG.getVTList(MVT::Other),
9860 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009861
9862 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009863 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00009864 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009865
9866 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009867 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009868 DAG.getNode(ISD::SRL, DL, MVT::i16,
9869 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009870 CWD, DAG.getConstant(0x800, MVT::i16)),
9871 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009872 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009873 DAG.getNode(ISD::SRL, DL, MVT::i16,
9874 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009875 CWD, DAG.getConstant(0x400, MVT::i16)),
9876 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009877
Dan Gohman475871a2008-07-27 21:46:04 +00009878 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009879 DAG.getNode(ISD::AND, DL, MVT::i16,
9880 DAG.getNode(ISD::ADD, DL, MVT::i16,
9881 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009882 DAG.getConstant(1, MVT::i16)),
9883 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009884
9885
Duncan Sands83ec4b62008-06-06 12:08:01 +00009886 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009887 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009888}
9889
Dan Gohmand858e902010-04-17 15:26:15 +00009890SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009891 EVT VT = Op.getValueType();
9892 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009893 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009894 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009895
9896 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009897 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009898 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009899 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009900 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009901 }
Evan Cheng18efe262007-12-14 02:13:44 +00009902
Evan Cheng152804e2007-12-14 08:30:15 +00009903 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009904 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009905 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009906
9907 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009908 SDValue Ops[] = {
9909 Op,
9910 DAG.getConstant(NumBits+NumBits-1, OpVT),
9911 DAG.getConstant(X86::COND_E, MVT::i8),
9912 Op.getValue(1)
9913 };
9914 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009915
9916 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009917 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009918
Owen Anderson825b72b2009-08-11 20:47:22 +00009919 if (VT == MVT::i8)
9920 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009921 return Op;
9922}
9923
Dan Gohmand858e902010-04-17 15:26:15 +00009924SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009925 EVT VT = Op.getValueType();
9926 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009927 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009928 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009929
9930 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009931 if (VT == MVT::i8) {
9932 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009933 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009934 }
Evan Cheng152804e2007-12-14 08:30:15 +00009935
9936 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009937 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009938 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009939
9940 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009941 SDValue Ops[] = {
9942 Op,
9943 DAG.getConstant(NumBits, OpVT),
9944 DAG.getConstant(X86::COND_E, MVT::i8),
9945 Op.getValue(1)
9946 };
9947 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009948
Owen Anderson825b72b2009-08-11 20:47:22 +00009949 if (VT == MVT::i8)
9950 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009951 return Op;
9952}
9953
Craig Topper13894fa2011-08-24 06:14:18 +00009954// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9955// ones, and then concatenate the result back.
9956static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009957 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009958
9959 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9960 "Unsupported value type for operation");
9961
9962 int NumElems = VT.getVectorNumElements();
9963 DebugLoc dl = Op.getDebugLoc();
9964 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9965 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9966
9967 // Extract the LHS vectors
9968 SDValue LHS = Op.getOperand(0);
9969 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9970 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9971
9972 // Extract the RHS vectors
9973 SDValue RHS = Op.getOperand(1);
9974 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9975 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9976
9977 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9978 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9979
9980 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9981 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
9982 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
9983}
9984
9985SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
9986 assert(Op.getValueType().getSizeInBits() == 256 &&
9987 Op.getValueType().isInteger() &&
9988 "Only handle AVX 256-bit vector integer operation");
9989 return Lower256IntArith(Op, DAG);
9990}
9991
9992SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
9993 assert(Op.getValueType().getSizeInBits() == 256 &&
9994 Op.getValueType().isInteger() &&
9995 "Only handle AVX 256-bit vector integer operation");
9996 return Lower256IntArith(Op, DAG);
9997}
9998
9999SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
10000 EVT VT = Op.getValueType();
10001
10002 // Decompose 256-bit ops into smaller 128-bit ops.
10003 if (VT.getSizeInBits() == 256)
10004 return Lower256IntArith(Op, DAG);
10005
Owen Anderson825b72b2009-08-11 20:47:22 +000010006 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010007 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010008
Mon P Wangaf9b9522008-12-18 21:42:19 +000010009 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
10010 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
10011 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
10012 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
10013 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
10014 //
10015 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
10016 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
10017 // return AloBlo + AloBhi + AhiBlo;
10018
10019 SDValue A = Op.getOperand(0);
10020 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010021
Dale Johannesene4d209d2009-02-03 20:21:25 +000010022 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010023 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10024 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010025 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010026 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10027 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010028 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010029 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010030 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010031 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010032 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010033 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010034 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010035 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010036 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010037 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010038 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10039 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010040 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010041 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10042 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010043 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10044 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010045 return Res;
10046}
10047
Nadav Rotem43012222011-05-11 08:12:09 +000010048SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10049
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010050 EVT VT = Op.getValueType();
10051 DebugLoc dl = Op.getDebugLoc();
10052 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010053 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010054 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010055
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010056 if (!Subtarget->hasXMMInt())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010057 return SDValue();
10058
10059 // Decompose 256-bit shifts into smaller 128-bit shifts.
10060 if (VT.getSizeInBits() == 256) {
10061 int NumElems = VT.getVectorNumElements();
10062 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10063 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10064
10065 // Extract the two vectors
10066 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
10067 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
10068 DAG, dl);
10069
10070 // Recreate the shift amount vectors
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +000010071 SDValue Amt1, Amt2;
10072 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10073 // Constant shift amount
10074 SmallVector<SDValue, 4> Amt1Csts;
10075 SmallVector<SDValue, 4> Amt2Csts;
10076 for (int i = 0; i < NumElems/2; ++i)
10077 Amt1Csts.push_back(Amt->getOperand(i));
10078 for (int i = NumElems/2; i < NumElems; ++i)
10079 Amt2Csts.push_back(Amt->getOperand(i));
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010080
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +000010081 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10082 &Amt1Csts[0], NumElems/2);
10083 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10084 &Amt2Csts[0], NumElems/2);
10085 } else {
10086 // Variable shift amount
10087 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
10088 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
10089 DAG, dl);
10090 }
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010091
10092 // Issue new vector shifts for the smaller types
10093 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10094 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10095
10096 // Concatenate the result back
10097 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10098 }
Nate Begeman51409212010-07-28 00:21:48 +000010099
Nadav Rotem43012222011-05-11 08:12:09 +000010100 // Optimize shl/srl/sra with constant shift amount.
10101 if (isSplatVector(Amt.getNode())) {
10102 SDValue SclrAmt = Amt->getOperand(0);
10103 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10104 uint64_t ShiftAmt = C->getZExtValue();
10105
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010106 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SHL) {
10107 // Make a large shift.
10108 SDValue SHL =
10109 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10110 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10111 R, DAG.getConstant(ShiftAmt, MVT::i32));
10112 // Zero out the rightmost bits.
10113 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10114 MVT::i8));
10115 return DAG.getNode(ISD::AND, dl, VT, SHL,
10116 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10117 }
10118
Nadav Rotem43012222011-05-11 08:12:09 +000010119 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
10120 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10121 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10122 R, DAG.getConstant(ShiftAmt, MVT::i32));
10123
10124 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
10125 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10126 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10127 R, DAG.getConstant(ShiftAmt, MVT::i32));
10128
10129 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
10130 return 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
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010134 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRL) {
10135 // Make a large shift.
10136 SDValue SRL =
10137 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10138 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10139 R, DAG.getConstant(ShiftAmt, MVT::i32));
10140 // Zero out the leftmost bits.
10141 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10142 MVT::i8));
10143 return DAG.getNode(ISD::AND, dl, VT, SRL,
10144 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10145 }
10146
Nadav Rotem43012222011-05-11 08:12:09 +000010147 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
10148 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10149 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10150 R, DAG.getConstant(ShiftAmt, MVT::i32));
10151
10152 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
10153 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10154 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
10155 R, DAG.getConstant(ShiftAmt, MVT::i32));
10156
10157 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
10158 return 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
10162 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
10163 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10164 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
10165 R, DAG.getConstant(ShiftAmt, MVT::i32));
10166
10167 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
10168 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10169 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
10170 R, DAG.getConstant(ShiftAmt, MVT::i32));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010171
10172 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRA) {
10173 if (ShiftAmt == 7) {
10174 // R s>> 7 === R s< 0
10175 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10176 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10177 }
10178
10179 // R s>> a === ((R u>> a) ^ m) - m
10180 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10181 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10182 MVT::i8));
10183 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10184 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10185 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10186 return Res;
10187 }
Nadav Rotem43012222011-05-11 08:12:09 +000010188 }
10189 }
10190
10191 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010192 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010193 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10194 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10195 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
10196
10197 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010198
Nate Begeman51409212010-07-28 00:21:48 +000010199 std::vector<Constant*> CV(4, CI);
10200 Constant *C = ConstantVector::get(CV);
10201 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10202 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010203 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +000010204 false, false, 16);
10205
10206 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010207 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010208 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10209 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10210 }
Nadav Rotem43012222011-05-11 08:12:09 +000010211 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010212 // a = a << 5;
10213 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10214 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10215 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
10216
10217 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
10218 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
10219
10220 std::vector<Constant*> CVM1(16, CM1);
10221 std::vector<Constant*> CVM2(16, CM2);
10222 Constant *C = ConstantVector::get(CVM1);
10223 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10224 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010225 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +000010226 false, false, 16);
10227
10228 // r = pblendv(r, psllw(r & (char16)15, 4), a);
10229 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10230 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10231 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10232 DAG.getConstant(4, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010233 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010234 // a += a
10235 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010236
Nate Begeman51409212010-07-28 00:21:48 +000010237 C = ConstantVector::get(CVM2);
10238 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10239 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010240 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010241 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010242
Nate Begeman51409212010-07-28 00:21:48 +000010243 // r = pblendv(r, psllw(r & (char16)63, 2), a);
10244 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10245 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10246 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10247 DAG.getConstant(2, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010248 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010249 // a += a
10250 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010251
Nate Begeman51409212010-07-28 00:21:48 +000010252 // return pblendv(r, r+r, a);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010253 R = DAG.getNode(ISD::VSELECT, dl, VT, Op,
10254 R, DAG.getNode(ISD::ADD, dl, VT, R, R));
Nate Begeman51409212010-07-28 00:21:48 +000010255 return R;
10256 }
10257 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010258}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010259
Dan Gohmand858e902010-04-17 15:26:15 +000010260SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010261 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10262 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010263 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10264 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010265 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010266 SDValue LHS = N->getOperand(0);
10267 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010268 unsigned BaseOp = 0;
10269 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010270 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010271 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010272 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010273 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010274 // A subtract of one will be selected as a INC. Note that INC doesn't
10275 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010276 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10277 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010278 BaseOp = X86ISD::INC;
10279 Cond = X86::COND_O;
10280 break;
10281 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010282 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010283 Cond = X86::COND_O;
10284 break;
10285 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010286 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010287 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010288 break;
10289 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010290 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10291 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010292 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10293 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010294 BaseOp = X86ISD::DEC;
10295 Cond = X86::COND_O;
10296 break;
10297 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010298 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010299 Cond = X86::COND_O;
10300 break;
10301 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010302 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010303 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010304 break;
10305 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010306 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010307 Cond = X86::COND_O;
10308 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010309 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10310 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10311 MVT::i32);
10312 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010313
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010314 SDValue SetCC =
10315 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10316 DAG.getConstant(X86::COND_O, MVT::i32),
10317 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010318
Dan Gohman6e5fda22011-07-22 18:45:15 +000010319 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010320 }
Bill Wendling74c37652008-12-09 22:08:41 +000010321 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010322
Bill Wendling61edeb52008-12-02 01:06:39 +000010323 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010324 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010325 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010326
Bill Wendling61edeb52008-12-02 01:06:39 +000010327 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010328 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10329 DAG.getConstant(Cond, MVT::i32),
10330 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010331
Dan Gohman6e5fda22011-07-22 18:45:15 +000010332 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010333}
10334
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010335SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10336 DebugLoc dl = Op.getDebugLoc();
10337 SDNode* Node = Op.getNode();
10338 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
10339 EVT VT = Node->getValueType(0);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010340 if (Subtarget->hasXMMInt() && VT.isVector()) {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010341 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10342 ExtraVT.getScalarType().getSizeInBits();
10343 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10344
10345 unsigned SHLIntrinsicsID = 0;
10346 unsigned SRAIntrinsicsID = 0;
10347 switch (VT.getSimpleVT().SimpleTy) {
10348 default:
10349 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010350 case MVT::v4i32: {
10351 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10352 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10353 break;
10354 }
10355 case MVT::v8i16: {
10356 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10357 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10358 break;
10359 }
10360 }
10361
10362 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10363 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
10364 Node->getOperand(0), ShAmt);
10365
Nadav Rotema7934dd2011-10-10 19:31:45 +000010366 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10367 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10368 Tmp1, ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010369 }
10370
10371 return SDValue();
10372}
10373
10374
Eric Christopher9a9d2752010-07-22 02:48:34 +000010375SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10376 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010377
Eric Christopher77ed1352011-07-08 00:04:56 +000010378 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10379 // There isn't any reason to disable it if the target processor supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010380 if (!Subtarget->hasXMMInt() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010381 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010382 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010383 SDValue Ops[] = {
10384 DAG.getRegister(X86::ESP, MVT::i32), // Base
10385 DAG.getTargetConstant(1, MVT::i8), // Scale
10386 DAG.getRegister(0, MVT::i32), // Index
10387 DAG.getTargetConstant(0, MVT::i32), // Disp
10388 DAG.getRegister(0, MVT::i32), // Segment.
10389 Zero,
10390 Chain
10391 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010392 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010393 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10394 array_lengthof(Ops));
10395 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010396 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010397
Eric Christopher9a9d2752010-07-22 02:48:34 +000010398 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010399 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010400 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010401
Chris Lattner132929a2010-08-14 17:26:09 +000010402 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10403 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10404 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10405 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010406
Chris Lattner132929a2010-08-14 17:26:09 +000010407 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10408 if (!Op1 && !Op2 && !Op3 && Op4)
10409 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010410
Chris Lattner132929a2010-08-14 17:26:09 +000010411 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10412 if (Op1 && !Op2 && !Op3 && !Op4)
10413 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010414
10415 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010416 // (MFENCE)>;
10417 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010418}
10419
Eli Friedman14648462011-07-27 22:21:52 +000010420SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10421 SelectionDAG &DAG) const {
10422 DebugLoc dl = Op.getDebugLoc();
10423 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10424 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10425 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10426 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10427
10428 // The only fence that needs an instruction is a sequentially-consistent
10429 // cross-thread fence.
10430 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10431 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10432 // no-sse2). There isn't any reason to disable it if the target processor
10433 // supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010434 if (Subtarget->hasXMMInt() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010435 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10436
10437 SDValue Chain = Op.getOperand(0);
10438 SDValue Zero = DAG.getConstant(0, MVT::i32);
10439 SDValue Ops[] = {
10440 DAG.getRegister(X86::ESP, MVT::i32), // Base
10441 DAG.getTargetConstant(1, MVT::i8), // Scale
10442 DAG.getRegister(0, MVT::i32), // Index
10443 DAG.getTargetConstant(0, MVT::i32), // Disp
10444 DAG.getRegister(0, MVT::i32), // Segment.
10445 Zero,
10446 Chain
10447 };
10448 SDNode *Res =
10449 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10450 array_lengthof(Ops));
10451 return SDValue(Res, 0);
10452 }
10453
10454 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10455 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10456}
10457
10458
Dan Gohmand858e902010-04-17 15:26:15 +000010459SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010460 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010461 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010462 unsigned Reg = 0;
10463 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010464 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010465 default:
10466 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010467 case MVT::i8: Reg = X86::AL; size = 1; break;
10468 case MVT::i16: Reg = X86::AX; size = 2; break;
10469 case MVT::i32: Reg = X86::EAX; size = 4; break;
10470 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010471 assert(Subtarget->is64Bit() && "Node not type legal!");
10472 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010473 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010474 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010475 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010476 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010477 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010478 Op.getOperand(1),
10479 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010480 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010481 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010482 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010483 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10484 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10485 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010486 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010487 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010488 return cpOut;
10489}
10490
Duncan Sands1607f052008-12-01 11:39:25 +000010491SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010492 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010493 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010494 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010495 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010496 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010497 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010498 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10499 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010500 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010501 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10502 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010503 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010504 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010505 rdx.getValue(1)
10506 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010507 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010508}
10509
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010510SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010511 SelectionDAG &DAG) const {
10512 EVT SrcVT = Op.getOperand(0).getValueType();
10513 EVT DstVT = Op.getValueType();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010514 assert(Subtarget->is64Bit() && !Subtarget->hasXMMInt() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010515 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010516 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010517 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010518 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010519 // i64 <=> MMX conversions are Legal.
10520 if (SrcVT==MVT::i64 && DstVT.isVector())
10521 return Op;
10522 if (DstVT==MVT::i64 && SrcVT.isVector())
10523 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010524 // MMX <=> MMX conversions are Legal.
10525 if (SrcVT.isVector() && DstVT.isVector())
10526 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010527 // All other conversions need to be expanded.
10528 return SDValue();
10529}
Chris Lattner5b856542010-12-20 00:59:46 +000010530
Dan Gohmand858e902010-04-17 15:26:15 +000010531SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010532 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010533 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010534 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010535 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010536 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010537 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010538 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010539 Node->getOperand(0),
10540 Node->getOperand(1), negOp,
10541 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010542 cast<AtomicSDNode>(Node)->getAlignment(),
10543 cast<AtomicSDNode>(Node)->getOrdering(),
10544 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010545}
10546
Eli Friedman327236c2011-08-24 20:50:09 +000010547static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10548 SDNode *Node = Op.getNode();
10549 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010550 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010551
10552 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010553 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10554 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10555 // (The only way to get a 16-byte store is cmpxchg16b)
10556 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10557 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10558 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010559 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10560 cast<AtomicSDNode>(Node)->getMemoryVT(),
10561 Node->getOperand(0),
10562 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010563 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010564 cast<AtomicSDNode>(Node)->getOrdering(),
10565 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010566 return Swap.getValue(1);
10567 }
10568 // Other atomic stores have a simple pattern.
10569 return Op;
10570}
10571
Chris Lattner5b856542010-12-20 00:59:46 +000010572static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10573 EVT VT = Op.getNode()->getValueType(0);
10574
10575 // Let legalize expand this if it isn't a legal type yet.
10576 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10577 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010578
Chris Lattner5b856542010-12-20 00:59:46 +000010579 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010580
Chris Lattner5b856542010-12-20 00:59:46 +000010581 unsigned Opc;
10582 bool ExtraOp = false;
10583 switch (Op.getOpcode()) {
10584 default: assert(0 && "Invalid code");
10585 case ISD::ADDC: Opc = X86ISD::ADD; break;
10586 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10587 case ISD::SUBC: Opc = X86ISD::SUB; break;
10588 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10589 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010590
Chris Lattner5b856542010-12-20 00:59:46 +000010591 if (!ExtraOp)
10592 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10593 Op.getOperand(1));
10594 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10595 Op.getOperand(1), Op.getOperand(2));
10596}
10597
Evan Cheng0db9fe62006-04-25 20:13:52 +000010598/// LowerOperation - Provide custom lowering hooks for some operations.
10599///
Dan Gohmand858e902010-04-17 15:26:15 +000010600SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010601 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010602 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010603 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010604 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010605 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010606 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10607 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010608 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010609 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010610 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010611 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10612 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10613 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010614 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010615 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010616 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10617 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10618 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010619 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010620 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010621 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010622 case ISD::SHL_PARTS:
10623 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010624 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010625 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010626 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010627 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010628 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010629 case ISD::FABS: return LowerFABS(Op, DAG);
10630 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010631 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010632 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010633 case ISD::SETCC: return LowerSETCC(Op, DAG);
10634 case ISD::SELECT: return LowerSELECT(Op, DAG);
10635 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010636 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010637 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010638 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010639 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010640 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010641 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10642 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010643 case ISD::FRAME_TO_ARGS_OFFSET:
10644 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010645 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010646 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010647 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10648 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010649 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010650 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10651 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010652 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010653 case ISD::SRA:
10654 case ISD::SRL:
10655 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010656 case ISD::SADDO:
10657 case ISD::UADDO:
10658 case ISD::SSUBO:
10659 case ISD::USUBO:
10660 case ISD::SMULO:
10661 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010662 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010663 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010664 case ISD::ADDC:
10665 case ISD::ADDE:
10666 case ISD::SUBC:
10667 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010668 case ISD::ADD: return LowerADD(Op, DAG);
10669 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010670 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010671}
10672
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010673static void ReplaceATOMIC_LOAD(SDNode *Node,
10674 SmallVectorImpl<SDValue> &Results,
10675 SelectionDAG &DAG) {
10676 DebugLoc dl = Node->getDebugLoc();
10677 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10678
10679 // Convert wide load -> cmpxchg8b/cmpxchg16b
10680 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10681 // (The only way to get a 16-byte load is cmpxchg16b)
10682 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010683 SDValue Zero = DAG.getConstant(0, VT);
10684 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010685 Node->getOperand(0),
10686 Node->getOperand(1), Zero, Zero,
10687 cast<AtomicSDNode>(Node)->getMemOperand(),
10688 cast<AtomicSDNode>(Node)->getOrdering(),
10689 cast<AtomicSDNode>(Node)->getSynchScope());
10690 Results.push_back(Swap.getValue(0));
10691 Results.push_back(Swap.getValue(1));
10692}
10693
Duncan Sands1607f052008-12-01 11:39:25 +000010694void X86TargetLowering::
10695ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010696 SelectionDAG &DAG, unsigned NewOp) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010697 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000010698 assert (Node->getValueType(0) == MVT::i64 &&
10699 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010700
10701 SDValue Chain = Node->getOperand(0);
10702 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010703 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010704 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010705 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010706 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010707 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010708 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010709 SDValue Result =
10710 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10711 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010712 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010713 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010714 Results.push_back(Result.getValue(2));
10715}
10716
Duncan Sands126d9072008-07-04 11:47:58 +000010717/// ReplaceNodeResults - Replace a node with an illegal result type
10718/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010719void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10720 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010721 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010722 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010723 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010724 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010725 assert(false && "Do not know how to custom type legalize this operation!");
10726 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010727 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010728 case ISD::ADDC:
10729 case ISD::ADDE:
10730 case ISD::SUBC:
10731 case ISD::SUBE:
10732 // We don't want to expand or promote these.
10733 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010734 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010735 std::pair<SDValue,SDValue> Vals =
10736 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010737 SDValue FIST = Vals.first, StackSlot = Vals.second;
10738 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010739 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010740 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010741 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
10742 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010743 }
10744 return;
10745 }
10746 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010747 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010748 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010749 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010750 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010751 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010752 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010753 eax.getValue(2));
10754 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10755 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010756 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010757 Results.push_back(edx.getValue(1));
10758 return;
10759 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010760 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010761 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010762 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010763 bool Regs64bit = T == MVT::i128;
10764 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010765 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010766 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10767 DAG.getConstant(0, HalfT));
10768 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10769 DAG.getConstant(1, HalfT));
10770 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10771 Regs64bit ? X86::RAX : X86::EAX,
10772 cpInL, SDValue());
10773 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10774 Regs64bit ? X86::RDX : X86::EDX,
10775 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010776 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010777 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10778 DAG.getConstant(0, HalfT));
10779 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10780 DAG.getConstant(1, HalfT));
10781 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10782 Regs64bit ? X86::RBX : X86::EBX,
10783 swapInL, cpInH.getValue(1));
10784 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10785 Regs64bit ? X86::RCX : X86::ECX,
10786 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010787 SDValue Ops[] = { swapInH.getValue(0),
10788 N->getOperand(1),
10789 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010790 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010791 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010792 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10793 X86ISD::LCMPXCHG8_DAG;
10794 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010795 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010796 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10797 Regs64bit ? X86::RAX : X86::EAX,
10798 HalfT, Result.getValue(1));
10799 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10800 Regs64bit ? X86::RDX : X86::EDX,
10801 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010802 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010803 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010804 Results.push_back(cpOutH.getValue(1));
10805 return;
10806 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010807 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010808 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10809 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010810 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010811 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10812 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010813 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010814 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10815 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010816 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010817 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10818 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010819 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010820 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10821 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010822 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010823 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10824 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010825 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010826 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10827 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010828 case ISD::ATOMIC_LOAD:
10829 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010830 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010831}
10832
Evan Cheng72261582005-12-20 06:22:03 +000010833const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10834 switch (Opcode) {
10835 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010836 case X86ISD::BSF: return "X86ISD::BSF";
10837 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010838 case X86ISD::SHLD: return "X86ISD::SHLD";
10839 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010840 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010841 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010842 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010843 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010844 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010845 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010846 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10847 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10848 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010849 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010850 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010851 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010852 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010853 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010854 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010855 case X86ISD::COMI: return "X86ISD::COMI";
10856 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010857 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010858 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010859 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10860 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010861 case X86ISD::CMOV: return "X86ISD::CMOV";
10862 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010863 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010864 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10865 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010866 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010867 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010868 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010869 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010870 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010871 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10872 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010873 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010874 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010875 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +000010876 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
10877 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
10878 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Evan Cheng8ca29322006-11-10 21:43:37 +000010879 case X86ISD::FMAX: return "X86ISD::FMAX";
10880 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000010881 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
10882 case X86ISD::FRCP: return "X86ISD::FRCP";
Duncan Sands17470be2011-09-22 20:15:48 +000010883 case X86ISD::FHADD: return "X86ISD::FHADD";
10884 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010885 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000010886 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010887 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000010888 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010889 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000010890 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
10891 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010892 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
10893 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
10894 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
10895 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
10896 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
10897 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000010898 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
10899 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000010900 case X86ISD::VSHL: return "X86ISD::VSHL";
10901 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000010902 case X86ISD::CMPPD: return "X86ISD::CMPPD";
10903 case X86ISD::CMPPS: return "X86ISD::CMPPS";
10904 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
10905 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
10906 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
10907 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
10908 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
10909 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
10910 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
10911 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010912 case X86ISD::ADD: return "X86ISD::ADD";
10913 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000010914 case X86ISD::ADC: return "X86ISD::ADC";
10915 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000010916 case X86ISD::SMUL: return "X86ISD::SMUL";
10917 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000010918 case X86ISD::INC: return "X86ISD::INC";
10919 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000010920 case X86ISD::OR: return "X86ISD::OR";
10921 case X86ISD::XOR: return "X86ISD::XOR";
10922 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000010923 case X86ISD::ANDN: return "X86ISD::ANDN";
Evan Cheng73f24c92009-03-30 21:36:47 +000010924 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000010925 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010926 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010927 case X86ISD::PALIGN: return "X86ISD::PALIGN";
10928 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
10929 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
10930 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
10931 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
10932 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
10933 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
10934 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
10935 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010936 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000010937 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010938 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000010939 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
10940 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010941 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
10942 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
10943 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
10944 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
10945 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
10946 case X86ISD::MOVSD: return "X86ISD::MOVSD";
10947 case X86ISD::MOVSS: return "X86ISD::MOVSS";
10948 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
10949 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +000010950 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010951 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
10952 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
10953 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
10954 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
10955 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
10956 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
10957 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
10958 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
10959 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
10960 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000010961 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000010962 case X86ISD::VPERMILPS: return "X86ISD::VPERMILPS";
10963 case X86ISD::VPERMILPSY: return "X86ISD::VPERMILPSY";
10964 case X86ISD::VPERMILPD: return "X86ISD::VPERMILPD";
10965 case X86ISD::VPERMILPDY: return "X86ISD::VPERMILPDY";
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000010966 case X86ISD::VPERM2F128: return "X86ISD::VPERM2F128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000010967 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000010968 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010969 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000010970 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000010971 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000010972 }
10973}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010974
Chris Lattnerc9addb72007-03-30 23:15:24 +000010975// isLegalAddressingMode - Return true if the addressing mode represented
10976// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000010977bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010978 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000010979 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010980 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000010981 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000010982
Chris Lattnerc9addb72007-03-30 23:15:24 +000010983 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010984 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010985 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000010986
Chris Lattnerc9addb72007-03-30 23:15:24 +000010987 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000010988 unsigned GVFlags =
10989 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010990
Chris Lattnerdfed4132009-07-10 07:38:24 +000010991 // If a reference to this global requires an extra load, we can't fold it.
10992 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010993 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010994
Chris Lattnerdfed4132009-07-10 07:38:24 +000010995 // If BaseGV requires a register for the PIC base, we cannot also have a
10996 // BaseReg specified.
10997 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000010998 return false;
Evan Cheng52787842007-08-01 23:46:47 +000010999
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011000 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011001 if ((M != CodeModel::Small || R != Reloc::Static) &&
11002 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011003 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011004 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011005
Chris Lattnerc9addb72007-03-30 23:15:24 +000011006 switch (AM.Scale) {
11007 case 0:
11008 case 1:
11009 case 2:
11010 case 4:
11011 case 8:
11012 // These scales always work.
11013 break;
11014 case 3:
11015 case 5:
11016 case 9:
11017 // These scales are formed with basereg+scalereg. Only accept if there is
11018 // no basereg yet.
11019 if (AM.HasBaseReg)
11020 return false;
11021 break;
11022 default: // Other stuff never works.
11023 return false;
11024 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011025
Chris Lattnerc9addb72007-03-30 23:15:24 +000011026 return true;
11027}
11028
11029
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011030bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011031 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011032 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011033 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11034 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011035 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011036 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011037 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011038}
11039
Owen Andersone50ed302009-08-10 22:56:29 +000011040bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011041 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011042 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011043 unsigned NumBits1 = VT1.getSizeInBits();
11044 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011045 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011046 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011047 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011048}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011049
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011050bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011051 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011052 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011053}
11054
Owen Andersone50ed302009-08-10 22:56:29 +000011055bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011056 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011057 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011058}
11059
Owen Andersone50ed302009-08-10 22:56:29 +000011060bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011061 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011062 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011063}
11064
Evan Cheng60c07e12006-07-05 22:17:51 +000011065/// isShuffleMaskLegal - Targets can use this to indicate that they only
11066/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11067/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11068/// are assumed to be legal.
11069bool
Eric Christopherfd179292009-08-27 18:07:15 +000011070X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011071 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011072 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011073 if (VT.getSizeInBits() == 64)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000011074 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX());
Nate Begeman9008ca62009-04-27 18:41:29 +000011075
Nate Begemana09008b2009-10-19 02:17:23 +000011076 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011077 return (VT.getVectorNumElements() == 2 ||
11078 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11079 isMOVLMask(M, VT) ||
11080 isSHUFPMask(M, VT) ||
11081 isPSHUFDMask(M, VT) ||
11082 isPSHUFHWMask(M, VT) ||
11083 isPSHUFLWMask(M, VT) ||
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000011084 isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011085 isUNPCKLMask(M, VT) ||
11086 isUNPCKHMask(M, VT) ||
11087 isUNPCKL_v_undef_Mask(M, VT) ||
11088 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011089}
11090
Dan Gohman7d8143f2008-04-09 20:09:42 +000011091bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011092X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011093 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011094 unsigned NumElts = VT.getVectorNumElements();
11095 // FIXME: This collection of masks seems suspect.
11096 if (NumElts == 2)
11097 return true;
11098 if (NumElts == 4 && VT.getSizeInBits() == 128) {
11099 return (isMOVLMask(Mask, VT) ||
11100 isCommutedMOVLMask(Mask, VT, true) ||
11101 isSHUFPMask(Mask, VT) ||
11102 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011103 }
11104 return false;
11105}
11106
11107//===----------------------------------------------------------------------===//
11108// X86 Scheduler Hooks
11109//===----------------------------------------------------------------------===//
11110
Mon P Wang63307c32008-05-05 19:05:59 +000011111// private utility function
11112MachineBasicBlock *
11113X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11114 MachineBasicBlock *MBB,
11115 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011116 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011117 unsigned LoadOpc,
11118 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011119 unsigned notOpc,
11120 unsigned EAXreg,
11121 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011122 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011123 // For the atomic bitwise operator, we generate
11124 // thisMBB:
11125 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011126 // ld t1 = [bitinstr.addr]
11127 // op t2 = t1, [bitinstr.val]
11128 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011129 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11130 // bz newMBB
11131 // fallthrough -->nextMBB
11132 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11133 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011134 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011135 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011136
Mon P Wang63307c32008-05-05 19:05:59 +000011137 /// First build the CFG
11138 MachineFunction *F = MBB->getParent();
11139 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011140 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11141 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11142 F->insert(MBBIter, newMBB);
11143 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011144
Dan Gohman14152b42010-07-06 20:24:04 +000011145 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11146 nextMBB->splice(nextMBB->begin(), thisMBB,
11147 llvm::next(MachineBasicBlock::iterator(bInstr)),
11148 thisMBB->end());
11149 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011150
Mon P Wang63307c32008-05-05 19:05:59 +000011151 // Update thisMBB to fall through to newMBB
11152 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011153
Mon P Wang63307c32008-05-05 19:05:59 +000011154 // newMBB jumps to itself and fall through to nextMBB
11155 newMBB->addSuccessor(nextMBB);
11156 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011157
Mon P Wang63307c32008-05-05 19:05:59 +000011158 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011159 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011160 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011161 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011162 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011163 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011164 int numArgs = bInstr->getNumOperands() - 1;
11165 for (int i=0; i < numArgs; ++i)
11166 argOpers[i] = &bInstr->getOperand(i+1);
11167
11168 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011169 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011170 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011171
Dale Johannesen140be2d2008-08-19 18:47:28 +000011172 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011173 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011174 for (int i=0; i <= lastAddrIndx; ++i)
11175 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011176
Dale Johannesen140be2d2008-08-19 18:47:28 +000011177 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011178 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011179 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011180 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011181 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011182 tt = t1;
11183
Dale Johannesen140be2d2008-08-19 18:47:28 +000011184 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011185 assert((argOpers[valArgIndx]->isReg() ||
11186 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011187 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011188 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011189 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011190 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011191 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011192 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000011193 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011194
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011195 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000011196 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011197
Dale Johannesene4d209d2009-02-03 20:21:25 +000011198 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011199 for (int i=0; i <= lastAddrIndx; ++i)
11200 (*MIB).addOperand(*argOpers[i]);
11201 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000011202 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011203 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11204 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011205
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011206 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011207 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011208
Mon P Wang63307c32008-05-05 19:05:59 +000011209 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011210 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011211
Dan Gohman14152b42010-07-06 20:24:04 +000011212 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011213 return nextMBB;
11214}
11215
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011216// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011217MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011218X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11219 MachineBasicBlock *MBB,
11220 unsigned regOpcL,
11221 unsigned regOpcH,
11222 unsigned immOpcL,
11223 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011224 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011225 // For the atomic bitwise operator, we generate
11226 // thisMBB (instructions are in pairs, except cmpxchg8b)
11227 // ld t1,t2 = [bitinstr.addr]
11228 // newMBB:
11229 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11230 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011231 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011232 // mov ECX, EBX <- t5, t6
11233 // mov EAX, EDX <- t1, t2
11234 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11235 // mov t3, t4 <- EAX, EDX
11236 // bz newMBB
11237 // result in out1, out2
11238 // fallthrough -->nextMBB
11239
11240 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11241 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011242 const unsigned NotOpc = X86::NOT32r;
11243 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11244 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11245 MachineFunction::iterator MBBIter = MBB;
11246 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011247
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011248 /// First build the CFG
11249 MachineFunction *F = MBB->getParent();
11250 MachineBasicBlock *thisMBB = MBB;
11251 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11252 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11253 F->insert(MBBIter, newMBB);
11254 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011255
Dan Gohman14152b42010-07-06 20:24:04 +000011256 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11257 nextMBB->splice(nextMBB->begin(), thisMBB,
11258 llvm::next(MachineBasicBlock::iterator(bInstr)),
11259 thisMBB->end());
11260 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011261
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011262 // Update thisMBB to fall through to newMBB
11263 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011264
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011265 // newMBB jumps to itself and fall through to nextMBB
11266 newMBB->addSuccessor(nextMBB);
11267 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011268
Dale Johannesene4d209d2009-02-03 20:21:25 +000011269 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011270 // Insert instructions into newMBB based on incoming instruction
11271 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011272 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011273 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011274 MachineOperand& dest1Oper = bInstr->getOperand(0);
11275 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011276 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11277 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011278 argOpers[i] = &bInstr->getOperand(i+2);
11279
Dan Gohman71ea4e52010-05-14 21:01:44 +000011280 // We use some of the operands multiple times, so conservatively just
11281 // clear any kill flags that might be present.
11282 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11283 argOpers[i]->setIsKill(false);
11284 }
11285
Evan Chengad5b52f2010-01-08 19:14:57 +000011286 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011287 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011288
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011289 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011290 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011291 for (int i=0; i <= lastAddrIndx; ++i)
11292 (*MIB).addOperand(*argOpers[i]);
11293 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011294 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011295 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011296 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011297 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011298 MachineOperand newOp3 = *(argOpers[3]);
11299 if (newOp3.isImm())
11300 newOp3.setImm(newOp3.getImm()+4);
11301 else
11302 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011303 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011304 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011305
11306 // t3/4 are defined later, at the bottom of the loop
11307 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11308 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011309 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011310 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011311 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011312 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11313
Evan Cheng306b4ca2010-01-08 23:41:50 +000011314 // The subsequent operations should be using the destination registers of
11315 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011316 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011317 t1 = F->getRegInfo().createVirtualRegister(RC);
11318 t2 = F->getRegInfo().createVirtualRegister(RC);
11319 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11320 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011321 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011322 t1 = dest1Oper.getReg();
11323 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011324 }
11325
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011326 int valArgIndx = lastAddrIndx + 1;
11327 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011328 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011329 "invalid operand");
11330 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11331 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011332 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011333 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011334 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011335 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011336 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011337 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011338 (*MIB).addOperand(*argOpers[valArgIndx]);
11339 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011340 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011341 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011342 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011343 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011344 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011345 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011346 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011347 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011348 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011349 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011350
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011351 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011352 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011353 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011354 MIB.addReg(t2);
11355
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011356 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011357 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011358 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011359 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011360
Dale Johannesene4d209d2009-02-03 20:21:25 +000011361 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011362 for (int i=0; i <= lastAddrIndx; ++i)
11363 (*MIB).addOperand(*argOpers[i]);
11364
11365 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011366 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11367 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011368
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011369 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011370 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011371 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011372 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011373
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011374 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011375 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011376
Dan Gohman14152b42010-07-06 20:24:04 +000011377 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011378 return nextMBB;
11379}
11380
11381// private utility function
11382MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011383X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11384 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011385 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011386 // For the atomic min/max operator, we generate
11387 // thisMBB:
11388 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011389 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011390 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011391 // cmp t1, t2
11392 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011393 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011394 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11395 // bz newMBB
11396 // fallthrough -->nextMBB
11397 //
11398 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11399 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011400 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011401 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011402
Mon P Wang63307c32008-05-05 19:05:59 +000011403 /// First build the CFG
11404 MachineFunction *F = MBB->getParent();
11405 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011406 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11407 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11408 F->insert(MBBIter, newMBB);
11409 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011410
Dan Gohman14152b42010-07-06 20:24:04 +000011411 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11412 nextMBB->splice(nextMBB->begin(), thisMBB,
11413 llvm::next(MachineBasicBlock::iterator(mInstr)),
11414 thisMBB->end());
11415 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011416
Mon P Wang63307c32008-05-05 19:05:59 +000011417 // Update thisMBB to fall through to newMBB
11418 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011419
Mon P Wang63307c32008-05-05 19:05:59 +000011420 // newMBB jumps to newMBB and fall through to nextMBB
11421 newMBB->addSuccessor(nextMBB);
11422 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011423
Dale Johannesene4d209d2009-02-03 20:21:25 +000011424 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011425 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011426 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011427 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011428 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011429 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011430 int numArgs = mInstr->getNumOperands() - 1;
11431 for (int i=0; i < numArgs; ++i)
11432 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011433
Mon P Wang63307c32008-05-05 19:05:59 +000011434 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011435 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011436 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011437
Mon P Wangab3e7472008-05-05 22:56:23 +000011438 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011439 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011440 for (int i=0; i <= lastAddrIndx; ++i)
11441 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011442
Mon P Wang63307c32008-05-05 19:05:59 +000011443 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011444 assert((argOpers[valArgIndx]->isReg() ||
11445 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011446 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011447
11448 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011449 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011450 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011451 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011452 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011453 (*MIB).addOperand(*argOpers[valArgIndx]);
11454
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011455 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011456 MIB.addReg(t1);
11457
Dale Johannesene4d209d2009-02-03 20:21:25 +000011458 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011459 MIB.addReg(t1);
11460 MIB.addReg(t2);
11461
11462 // Generate movc
11463 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011464 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011465 MIB.addReg(t2);
11466 MIB.addReg(t1);
11467
11468 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011469 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011470 for (int i=0; i <= lastAddrIndx; ++i)
11471 (*MIB).addOperand(*argOpers[i]);
11472 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011473 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011474 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11475 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011476
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011477 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011478 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011479
Mon P Wang63307c32008-05-05 19:05:59 +000011480 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011481 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011482
Dan Gohman14152b42010-07-06 20:24:04 +000011483 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011484 return nextMBB;
11485}
11486
Eric Christopherf83a5de2009-08-27 18:08:16 +000011487// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011488// or XMM0_V32I8 in AVX all of this code can be replaced with that
11489// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011490MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011491X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011492 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011493 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
11494 "Target must have SSE4.2 or AVX features enabled");
11495
Eric Christopherb120ab42009-08-18 22:50:32 +000011496 DebugLoc dl = MI->getDebugLoc();
11497 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011498 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011499 if (!Subtarget->hasAVX()) {
11500 if (memArg)
11501 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11502 else
11503 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11504 } else {
11505 if (memArg)
11506 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11507 else
11508 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11509 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011510
Eric Christopher41c902f2010-11-30 08:20:21 +000011511 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011512 for (unsigned i = 0; i < numArgs; ++i) {
11513 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011514 if (!(Op.isReg() && Op.isImplicit()))
11515 MIB.addOperand(Op);
11516 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011517 BuildMI(*BB, MI, dl,
11518 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11519 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011520 .addReg(X86::XMM0);
11521
Dan Gohman14152b42010-07-06 20:24:04 +000011522 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011523 return BB;
11524}
11525
11526MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011527X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011528 DebugLoc dl = MI->getDebugLoc();
11529 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011530
Eric Christopher228232b2010-11-30 07:20:12 +000011531 // Address into RAX/EAX, other two args into ECX, EDX.
11532 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11533 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11534 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11535 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011536 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011537
Eric Christopher228232b2010-11-30 07:20:12 +000011538 unsigned ValOps = X86::AddrNumOperands;
11539 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11540 .addReg(MI->getOperand(ValOps).getReg());
11541 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11542 .addReg(MI->getOperand(ValOps+1).getReg());
11543
11544 // The instruction doesn't actually take any operands though.
11545 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011546
Eric Christopher228232b2010-11-30 07:20:12 +000011547 MI->eraseFromParent(); // The pseudo is gone now.
11548 return BB;
11549}
11550
11551MachineBasicBlock *
11552X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011553 DebugLoc dl = MI->getDebugLoc();
11554 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011555
Eric Christopher228232b2010-11-30 07:20:12 +000011556 // First arg in ECX, the second in EAX.
11557 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11558 .addReg(MI->getOperand(0).getReg());
11559 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11560 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011561
Eric Christopher228232b2010-11-30 07:20:12 +000011562 // The instruction doesn't actually take any operands though.
11563 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011564
Eric Christopher228232b2010-11-30 07:20:12 +000011565 MI->eraseFromParent(); // The pseudo is gone now.
11566 return BB;
11567}
11568
11569MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011570X86TargetLowering::EmitVAARG64WithCustomInserter(
11571 MachineInstr *MI,
11572 MachineBasicBlock *MBB) const {
11573 // Emit va_arg instruction on X86-64.
11574
11575 // Operands to this pseudo-instruction:
11576 // 0 ) Output : destination address (reg)
11577 // 1-5) Input : va_list address (addr, i64mem)
11578 // 6 ) ArgSize : Size (in bytes) of vararg type
11579 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11580 // 8 ) Align : Alignment of type
11581 // 9 ) EFLAGS (implicit-def)
11582
11583 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11584 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11585
11586 unsigned DestReg = MI->getOperand(0).getReg();
11587 MachineOperand &Base = MI->getOperand(1);
11588 MachineOperand &Scale = MI->getOperand(2);
11589 MachineOperand &Index = MI->getOperand(3);
11590 MachineOperand &Disp = MI->getOperand(4);
11591 MachineOperand &Segment = MI->getOperand(5);
11592 unsigned ArgSize = MI->getOperand(6).getImm();
11593 unsigned ArgMode = MI->getOperand(7).getImm();
11594 unsigned Align = MI->getOperand(8).getImm();
11595
11596 // Memory Reference
11597 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11598 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11599 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11600
11601 // Machine Information
11602 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11603 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11604 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11605 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11606 DebugLoc DL = MI->getDebugLoc();
11607
11608 // struct va_list {
11609 // i32 gp_offset
11610 // i32 fp_offset
11611 // i64 overflow_area (address)
11612 // i64 reg_save_area (address)
11613 // }
11614 // sizeof(va_list) = 24
11615 // alignment(va_list) = 8
11616
11617 unsigned TotalNumIntRegs = 6;
11618 unsigned TotalNumXMMRegs = 8;
11619 bool UseGPOffset = (ArgMode == 1);
11620 bool UseFPOffset = (ArgMode == 2);
11621 unsigned MaxOffset = TotalNumIntRegs * 8 +
11622 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11623
11624 /* Align ArgSize to a multiple of 8 */
11625 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11626 bool NeedsAlign = (Align > 8);
11627
11628 MachineBasicBlock *thisMBB = MBB;
11629 MachineBasicBlock *overflowMBB;
11630 MachineBasicBlock *offsetMBB;
11631 MachineBasicBlock *endMBB;
11632
11633 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11634 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11635 unsigned OffsetReg = 0;
11636
11637 if (!UseGPOffset && !UseFPOffset) {
11638 // If we only pull from the overflow region, we don't create a branch.
11639 // We don't need to alter control flow.
11640 OffsetDestReg = 0; // unused
11641 OverflowDestReg = DestReg;
11642
11643 offsetMBB = NULL;
11644 overflowMBB = thisMBB;
11645 endMBB = thisMBB;
11646 } else {
11647 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11648 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11649 // If not, pull from overflow_area. (branch to overflowMBB)
11650 //
11651 // thisMBB
11652 // | .
11653 // | .
11654 // offsetMBB overflowMBB
11655 // | .
11656 // | .
11657 // endMBB
11658
11659 // Registers for the PHI in endMBB
11660 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11661 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11662
11663 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11664 MachineFunction *MF = MBB->getParent();
11665 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11666 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11667 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11668
11669 MachineFunction::iterator MBBIter = MBB;
11670 ++MBBIter;
11671
11672 // Insert the new basic blocks
11673 MF->insert(MBBIter, offsetMBB);
11674 MF->insert(MBBIter, overflowMBB);
11675 MF->insert(MBBIter, endMBB);
11676
11677 // Transfer the remainder of MBB and its successor edges to endMBB.
11678 endMBB->splice(endMBB->begin(), thisMBB,
11679 llvm::next(MachineBasicBlock::iterator(MI)),
11680 thisMBB->end());
11681 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11682
11683 // Make offsetMBB and overflowMBB successors of thisMBB
11684 thisMBB->addSuccessor(offsetMBB);
11685 thisMBB->addSuccessor(overflowMBB);
11686
11687 // endMBB is a successor of both offsetMBB and overflowMBB
11688 offsetMBB->addSuccessor(endMBB);
11689 overflowMBB->addSuccessor(endMBB);
11690
11691 // Load the offset value into a register
11692 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11693 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11694 .addOperand(Base)
11695 .addOperand(Scale)
11696 .addOperand(Index)
11697 .addDisp(Disp, UseFPOffset ? 4 : 0)
11698 .addOperand(Segment)
11699 .setMemRefs(MMOBegin, MMOEnd);
11700
11701 // Check if there is enough room left to pull this argument.
11702 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11703 .addReg(OffsetReg)
11704 .addImm(MaxOffset + 8 - ArgSizeA8);
11705
11706 // Branch to "overflowMBB" if offset >= max
11707 // Fall through to "offsetMBB" otherwise
11708 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11709 .addMBB(overflowMBB);
11710 }
11711
11712 // In offsetMBB, emit code to use the reg_save_area.
11713 if (offsetMBB) {
11714 assert(OffsetReg != 0);
11715
11716 // Read the reg_save_area address.
11717 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11718 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11719 .addOperand(Base)
11720 .addOperand(Scale)
11721 .addOperand(Index)
11722 .addDisp(Disp, 16)
11723 .addOperand(Segment)
11724 .setMemRefs(MMOBegin, MMOEnd);
11725
11726 // Zero-extend the offset
11727 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11728 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11729 .addImm(0)
11730 .addReg(OffsetReg)
11731 .addImm(X86::sub_32bit);
11732
11733 // Add the offset to the reg_save_area to get the final address.
11734 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11735 .addReg(OffsetReg64)
11736 .addReg(RegSaveReg);
11737
11738 // Compute the offset for the next argument
11739 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11740 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11741 .addReg(OffsetReg)
11742 .addImm(UseFPOffset ? 16 : 8);
11743
11744 // Store it back into the va_list.
11745 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11746 .addOperand(Base)
11747 .addOperand(Scale)
11748 .addOperand(Index)
11749 .addDisp(Disp, UseFPOffset ? 4 : 0)
11750 .addOperand(Segment)
11751 .addReg(NextOffsetReg)
11752 .setMemRefs(MMOBegin, MMOEnd);
11753
11754 // Jump to endMBB
11755 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11756 .addMBB(endMBB);
11757 }
11758
11759 //
11760 // Emit code to use overflow area
11761 //
11762
11763 // Load the overflow_area address into a register.
11764 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11765 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11766 .addOperand(Base)
11767 .addOperand(Scale)
11768 .addOperand(Index)
11769 .addDisp(Disp, 8)
11770 .addOperand(Segment)
11771 .setMemRefs(MMOBegin, MMOEnd);
11772
11773 // If we need to align it, do so. Otherwise, just copy the address
11774 // to OverflowDestReg.
11775 if (NeedsAlign) {
11776 // Align the overflow address
11777 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11778 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11779
11780 // aligned_addr = (addr + (align-1)) & ~(align-1)
11781 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11782 .addReg(OverflowAddrReg)
11783 .addImm(Align-1);
11784
11785 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11786 .addReg(TmpReg)
11787 .addImm(~(uint64_t)(Align-1));
11788 } else {
11789 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11790 .addReg(OverflowAddrReg);
11791 }
11792
11793 // Compute the next overflow address after this argument.
11794 // (the overflow address should be kept 8-byte aligned)
11795 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11796 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11797 .addReg(OverflowDestReg)
11798 .addImm(ArgSizeA8);
11799
11800 // Store the new overflow address.
11801 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11802 .addOperand(Base)
11803 .addOperand(Scale)
11804 .addOperand(Index)
11805 .addDisp(Disp, 8)
11806 .addOperand(Segment)
11807 .addReg(NextAddrReg)
11808 .setMemRefs(MMOBegin, MMOEnd);
11809
11810 // If we branched, emit the PHI to the front of endMBB.
11811 if (offsetMBB) {
11812 BuildMI(*endMBB, endMBB->begin(), DL,
11813 TII->get(X86::PHI), DestReg)
11814 .addReg(OffsetDestReg).addMBB(offsetMBB)
11815 .addReg(OverflowDestReg).addMBB(overflowMBB);
11816 }
11817
11818 // Erase the pseudo instruction
11819 MI->eraseFromParent();
11820
11821 return endMBB;
11822}
11823
11824MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011825X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11826 MachineInstr *MI,
11827 MachineBasicBlock *MBB) const {
11828 // Emit code to save XMM registers to the stack. The ABI says that the
11829 // number of registers to save is given in %al, so it's theoretically
11830 // possible to do an indirect jump trick to avoid saving all of them,
11831 // however this code takes a simpler approach and just executes all
11832 // of the stores if %al is non-zero. It's less code, and it's probably
11833 // easier on the hardware branch predictor, and stores aren't all that
11834 // expensive anyway.
11835
11836 // Create the new basic blocks. One block contains all the XMM stores,
11837 // and one block is the final destination regardless of whether any
11838 // stores were performed.
11839 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11840 MachineFunction *F = MBB->getParent();
11841 MachineFunction::iterator MBBIter = MBB;
11842 ++MBBIter;
11843 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11844 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11845 F->insert(MBBIter, XMMSaveMBB);
11846 F->insert(MBBIter, EndMBB);
11847
Dan Gohman14152b42010-07-06 20:24:04 +000011848 // Transfer the remainder of MBB and its successor edges to EndMBB.
11849 EndMBB->splice(EndMBB->begin(), MBB,
11850 llvm::next(MachineBasicBlock::iterator(MI)),
11851 MBB->end());
11852 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11853
Dan Gohmand6708ea2009-08-15 01:38:56 +000011854 // The original block will now fall through to the XMM save block.
11855 MBB->addSuccessor(XMMSaveMBB);
11856 // The XMMSaveMBB will fall through to the end block.
11857 XMMSaveMBB->addSuccessor(EndMBB);
11858
11859 // Now add the instructions.
11860 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11861 DebugLoc DL = MI->getDebugLoc();
11862
11863 unsigned CountReg = MI->getOperand(0).getReg();
11864 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11865 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11866
11867 if (!Subtarget->isTargetWin64()) {
11868 // If %al is 0, branch around the XMM save block.
11869 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011870 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011871 MBB->addSuccessor(EndMBB);
11872 }
11873
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011874 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000011875 // In the XMM save block, save all the XMM argument registers.
11876 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11877 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011878 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011879 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011880 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011881 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011882 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011883 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000011884 .addFrameIndex(RegSaveFrameIndex)
11885 .addImm(/*Scale=*/1)
11886 .addReg(/*IndexReg=*/0)
11887 .addImm(/*Disp=*/Offset)
11888 .addReg(/*Segment=*/0)
11889 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011890 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011891 }
11892
Dan Gohman14152b42010-07-06 20:24:04 +000011893 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011894
11895 return EndMBB;
11896}
Mon P Wang63307c32008-05-05 19:05:59 +000011897
Evan Cheng60c07e12006-07-05 22:17:51 +000011898MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000011899X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011900 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000011901 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11902 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000011903
Chris Lattner52600972009-09-02 05:57:00 +000011904 // To "insert" a SELECT_CC instruction, we actually have to insert the
11905 // diamond control-flow pattern. The incoming instruction knows the
11906 // destination vreg to set, the condition code register to branch on, the
11907 // true/false values to select between, and a branch opcode to use.
11908 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11909 MachineFunction::iterator It = BB;
11910 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000011911
Chris Lattner52600972009-09-02 05:57:00 +000011912 // thisMBB:
11913 // ...
11914 // TrueVal = ...
11915 // cmpTY ccX, r1, r2
11916 // bCC copy1MBB
11917 // fallthrough --> copy0MBB
11918 MachineBasicBlock *thisMBB = BB;
11919 MachineFunction *F = BB->getParent();
11920 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11921 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000011922 F->insert(It, copy0MBB);
11923 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000011924
Bill Wendling730c07e2010-06-25 20:48:10 +000011925 // If the EFLAGS register isn't dead in the terminator, then claim that it's
11926 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000011927 if (!MI->killsRegister(X86::EFLAGS)) {
11928 copy0MBB->addLiveIn(X86::EFLAGS);
11929 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000011930 }
11931
Dan Gohman14152b42010-07-06 20:24:04 +000011932 // Transfer the remainder of BB and its successor edges to sinkMBB.
11933 sinkMBB->splice(sinkMBB->begin(), BB,
11934 llvm::next(MachineBasicBlock::iterator(MI)),
11935 BB->end());
11936 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11937
11938 // Add the true and fallthrough blocks as its successors.
11939 BB->addSuccessor(copy0MBB);
11940 BB->addSuccessor(sinkMBB);
11941
11942 // Create the conditional branch instruction.
11943 unsigned Opc =
11944 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
11945 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
11946
Chris Lattner52600972009-09-02 05:57:00 +000011947 // copy0MBB:
11948 // %FalseValue = ...
11949 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000011950 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000011951
Chris Lattner52600972009-09-02 05:57:00 +000011952 // sinkMBB:
11953 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
11954 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000011955 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
11956 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000011957 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
11958 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
11959
Dan Gohman14152b42010-07-06 20:24:04 +000011960 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000011961 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000011962}
11963
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011964MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011965X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
11966 bool Is64Bit) const {
11967 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11968 DebugLoc DL = MI->getDebugLoc();
11969 MachineFunction *MF = BB->getParent();
11970 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11971
11972 assert(EnableSegmentedStacks);
11973
11974 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
11975 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
11976
11977 // BB:
11978 // ... [Till the alloca]
11979 // If stacklet is not large enough, jump to mallocMBB
11980 //
11981 // bumpMBB:
11982 // Allocate by subtracting from RSP
11983 // Jump to continueMBB
11984 //
11985 // mallocMBB:
11986 // Allocate by call to runtime
11987 //
11988 // continueMBB:
11989 // ...
11990 // [rest of original BB]
11991 //
11992
11993 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11994 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11995 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11996
11997 MachineRegisterInfo &MRI = MF->getRegInfo();
11998 const TargetRegisterClass *AddrRegClass =
11999 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12000
12001 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12002 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12003 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012004 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012005 sizeVReg = MI->getOperand(1).getReg(),
12006 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12007
12008 MachineFunction::iterator MBBIter = BB;
12009 ++MBBIter;
12010
12011 MF->insert(MBBIter, bumpMBB);
12012 MF->insert(MBBIter, mallocMBB);
12013 MF->insert(MBBIter, continueMBB);
12014
12015 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12016 (MachineBasicBlock::iterator(MI)), BB->end());
12017 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12018
12019 // Add code to the main basic block to check if the stack limit has been hit,
12020 // and if so, jump to mallocMBB otherwise to bumpMBB.
12021 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012022 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012023 .addReg(tmpSPVReg).addReg(sizeVReg);
12024 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
12025 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012026 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012027 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12028
12029 // bumpMBB simply decreases the stack pointer, since we know the current
12030 // stacklet has enough space.
12031 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012032 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012033 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012034 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012035 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12036
12037 // Calls into a routine in libgcc to allocate more space from the heap.
12038 if (Is64Bit) {
12039 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12040 .addReg(sizeVReg);
12041 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
12042 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
12043 } else {
12044 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12045 .addImm(12);
12046 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12047 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
12048 .addExternalSymbol("__morestack_allocate_stack_space");
12049 }
12050
12051 if (!Is64Bit)
12052 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12053 .addImm(16);
12054
12055 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12056 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12057 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12058
12059 // Set up the CFG correctly.
12060 BB->addSuccessor(bumpMBB);
12061 BB->addSuccessor(mallocMBB);
12062 mallocMBB->addSuccessor(continueMBB);
12063 bumpMBB->addSuccessor(continueMBB);
12064
12065 // Take care of the PHI nodes.
12066 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12067 MI->getOperand(0).getReg())
12068 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12069 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12070
12071 // Delete the original pseudo instruction.
12072 MI->eraseFromParent();
12073
12074 // And we're done.
12075 return continueMBB;
12076}
12077
12078MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012079X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012080 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012081 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12082 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012083
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012084 assert(!Subtarget->isTargetEnvMacho());
12085
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012086 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12087 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012088
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012089 if (Subtarget->isTargetWin64()) {
12090 if (Subtarget->isTargetCygMing()) {
12091 // ___chkstk(Mingw64):
12092 // Clobbers R10, R11, RAX and EFLAGS.
12093 // Updates RSP.
12094 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12095 .addExternalSymbol("___chkstk")
12096 .addReg(X86::RAX, RegState::Implicit)
12097 .addReg(X86::RSP, RegState::Implicit)
12098 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12099 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12100 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12101 } else {
12102 // __chkstk(MSVCRT): does not update stack pointer.
12103 // Clobbers R10, R11 and EFLAGS.
12104 // FIXME: RAX(allocated size) might be reused and not killed.
12105 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12106 .addExternalSymbol("__chkstk")
12107 .addReg(X86::RAX, RegState::Implicit)
12108 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12109 // RAX has the offset to subtracted from RSP.
12110 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12111 .addReg(X86::RSP)
12112 .addReg(X86::RAX);
12113 }
12114 } else {
12115 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012116 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12117
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012118 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12119 .addExternalSymbol(StackProbeSymbol)
12120 .addReg(X86::EAX, RegState::Implicit)
12121 .addReg(X86::ESP, RegState::Implicit)
12122 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12123 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12124 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12125 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012126
Dan Gohman14152b42010-07-06 20:24:04 +000012127 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012128 return BB;
12129}
Chris Lattner52600972009-09-02 05:57:00 +000012130
12131MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012132X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12133 MachineBasicBlock *BB) const {
12134 // This is pretty easy. We're taking the value that we received from
12135 // our load from the relocation, sticking it in either RDI (x86-64)
12136 // or EAX and doing an indirect call. The return value will then
12137 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012138 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012139 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012140 DebugLoc DL = MI->getDebugLoc();
12141 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012142
12143 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012144 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012145
Eric Christopher30ef0e52010-06-03 04:07:48 +000012146 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012147 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12148 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012149 .addReg(X86::RIP)
12150 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012151 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012152 MI->getOperand(3).getTargetFlags())
12153 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012154 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012155 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000012156 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012157 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12158 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012159 .addReg(0)
12160 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012161 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012162 MI->getOperand(3).getTargetFlags())
12163 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012164 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012165 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012166 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012167 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12168 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012169 .addReg(TII->getGlobalBaseReg(F))
12170 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012171 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012172 MI->getOperand(3).getTargetFlags())
12173 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012174 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012175 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012176 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012177
Dan Gohman14152b42010-07-06 20:24:04 +000012178 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012179 return BB;
12180}
12181
12182MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012183X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012184 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012185 switch (MI->getOpcode()) {
Richard Trieu23946fc2011-09-21 03:09:09 +000012186 default: assert(0 && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012187 case X86::TAILJMPd64:
12188 case X86::TAILJMPr64:
12189 case X86::TAILJMPm64:
Richard Trieu23946fc2011-09-21 03:09:09 +000012190 assert(0 && "TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012191 case X86::TCRETURNdi64:
12192 case X86::TCRETURNri64:
12193 case X86::TCRETURNmi64:
12194 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
12195 // On AMD64, additional defs should be added before register allocation.
12196 if (!Subtarget->isTargetWin64()) {
12197 MI->addRegisterDefined(X86::RSI);
12198 MI->addRegisterDefined(X86::RDI);
12199 MI->addRegisterDefined(X86::XMM6);
12200 MI->addRegisterDefined(X86::XMM7);
12201 MI->addRegisterDefined(X86::XMM8);
12202 MI->addRegisterDefined(X86::XMM9);
12203 MI->addRegisterDefined(X86::XMM10);
12204 MI->addRegisterDefined(X86::XMM11);
12205 MI->addRegisterDefined(X86::XMM12);
12206 MI->addRegisterDefined(X86::XMM13);
12207 MI->addRegisterDefined(X86::XMM14);
12208 MI->addRegisterDefined(X86::XMM15);
12209 }
12210 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012211 case X86::WIN_ALLOCA:
12212 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012213 case X86::SEG_ALLOCA_32:
12214 return EmitLoweredSegAlloca(MI, BB, false);
12215 case X86::SEG_ALLOCA_64:
12216 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012217 case X86::TLSCall_32:
12218 case X86::TLSCall_64:
12219 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012220 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012221 case X86::CMOV_FR32:
12222 case X86::CMOV_FR64:
12223 case X86::CMOV_V4F32:
12224 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012225 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012226 case X86::CMOV_V8F32:
12227 case X86::CMOV_V4F64:
12228 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012229 case X86::CMOV_GR16:
12230 case X86::CMOV_GR32:
12231 case X86::CMOV_RFP32:
12232 case X86::CMOV_RFP64:
12233 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012234 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012235
Dale Johannesen849f2142007-07-03 00:53:03 +000012236 case X86::FP32_TO_INT16_IN_MEM:
12237 case X86::FP32_TO_INT32_IN_MEM:
12238 case X86::FP32_TO_INT64_IN_MEM:
12239 case X86::FP64_TO_INT16_IN_MEM:
12240 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012241 case X86::FP64_TO_INT64_IN_MEM:
12242 case X86::FP80_TO_INT16_IN_MEM:
12243 case X86::FP80_TO_INT32_IN_MEM:
12244 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012245 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12246 DebugLoc DL = MI->getDebugLoc();
12247
Evan Cheng60c07e12006-07-05 22:17:51 +000012248 // Change the floating point control register to use "round towards zero"
12249 // mode when truncating to an integer value.
12250 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012251 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012252 addFrameReference(BuildMI(*BB, MI, DL,
12253 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012254
12255 // Load the old value of the high byte of the control word...
12256 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012257 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012258 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012259 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012260
12261 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012262 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012263 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012264
12265 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012266 addFrameReference(BuildMI(*BB, MI, DL,
12267 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012268
12269 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012270 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012271 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012272
12273 // Get the X86 opcode to use.
12274 unsigned Opc;
12275 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012276 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012277 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12278 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12279 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12280 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12281 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12282 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012283 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12284 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12285 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012286 }
12287
12288 X86AddressMode AM;
12289 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012290 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012291 AM.BaseType = X86AddressMode::RegBase;
12292 AM.Base.Reg = Op.getReg();
12293 } else {
12294 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012295 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012296 }
12297 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012298 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012299 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012300 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012301 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012302 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012303 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012304 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012305 AM.GV = Op.getGlobal();
12306 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012307 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012308 }
Dan Gohman14152b42010-07-06 20:24:04 +000012309 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012310 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012311
12312 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012313 addFrameReference(BuildMI(*BB, MI, DL,
12314 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012315
Dan Gohman14152b42010-07-06 20:24:04 +000012316 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012317 return BB;
12318 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012319 // String/text processing lowering.
12320 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012321 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012322 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12323 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012324 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012325 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12326 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012327 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012328 return EmitPCMP(MI, BB, 5, false /* in mem */);
12329 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012330 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012331 return EmitPCMP(MI, BB, 5, true /* in mem */);
12332
Eric Christopher228232b2010-11-30 07:20:12 +000012333 // Thread synchronization.
12334 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012335 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012336 case X86::MWAIT:
12337 return EmitMwait(MI, BB);
12338
Eric Christopherb120ab42009-08-18 22:50:32 +000012339 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012340 case X86::ATOMAND32:
12341 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012342 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012343 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012344 X86::NOT32r, X86::EAX,
12345 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012346 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012347 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12348 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012349 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012350 X86::NOT32r, X86::EAX,
12351 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012352 case X86::ATOMXOR32:
12353 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012354 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012355 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012356 X86::NOT32r, X86::EAX,
12357 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012358 case X86::ATOMNAND32:
12359 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012360 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012361 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012362 X86::NOT32r, X86::EAX,
12363 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012364 case X86::ATOMMIN32:
12365 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12366 case X86::ATOMMAX32:
12367 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12368 case X86::ATOMUMIN32:
12369 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12370 case X86::ATOMUMAX32:
12371 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012372
12373 case X86::ATOMAND16:
12374 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12375 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012376 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012377 X86::NOT16r, X86::AX,
12378 X86::GR16RegisterClass);
12379 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012380 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012381 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012382 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012383 X86::NOT16r, X86::AX,
12384 X86::GR16RegisterClass);
12385 case X86::ATOMXOR16:
12386 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12387 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012388 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012389 X86::NOT16r, X86::AX,
12390 X86::GR16RegisterClass);
12391 case X86::ATOMNAND16:
12392 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12393 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012394 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012395 X86::NOT16r, X86::AX,
12396 X86::GR16RegisterClass, true);
12397 case X86::ATOMMIN16:
12398 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12399 case X86::ATOMMAX16:
12400 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12401 case X86::ATOMUMIN16:
12402 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12403 case X86::ATOMUMAX16:
12404 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12405
12406 case X86::ATOMAND8:
12407 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12408 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012409 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012410 X86::NOT8r, X86::AL,
12411 X86::GR8RegisterClass);
12412 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012413 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012414 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012415 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012416 X86::NOT8r, X86::AL,
12417 X86::GR8RegisterClass);
12418 case X86::ATOMXOR8:
12419 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12420 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012421 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012422 X86::NOT8r, X86::AL,
12423 X86::GR8RegisterClass);
12424 case X86::ATOMNAND8:
12425 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12426 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012427 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012428 X86::NOT8r, X86::AL,
12429 X86::GR8RegisterClass, true);
12430 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012431 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012432 case X86::ATOMAND64:
12433 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012434 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012435 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012436 X86::NOT64r, X86::RAX,
12437 X86::GR64RegisterClass);
12438 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012439 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12440 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012441 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012442 X86::NOT64r, X86::RAX,
12443 X86::GR64RegisterClass);
12444 case X86::ATOMXOR64:
12445 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012446 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012447 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012448 X86::NOT64r, X86::RAX,
12449 X86::GR64RegisterClass);
12450 case X86::ATOMNAND64:
12451 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12452 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012453 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012454 X86::NOT64r, X86::RAX,
12455 X86::GR64RegisterClass, true);
12456 case X86::ATOMMIN64:
12457 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12458 case X86::ATOMMAX64:
12459 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12460 case X86::ATOMUMIN64:
12461 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12462 case X86::ATOMUMAX64:
12463 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012464
12465 // This group does 64-bit operations on a 32-bit host.
12466 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012467 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012468 X86::AND32rr, X86::AND32rr,
12469 X86::AND32ri, X86::AND32ri,
12470 false);
12471 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012472 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012473 X86::OR32rr, X86::OR32rr,
12474 X86::OR32ri, X86::OR32ri,
12475 false);
12476 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012477 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012478 X86::XOR32rr, X86::XOR32rr,
12479 X86::XOR32ri, X86::XOR32ri,
12480 false);
12481 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012482 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012483 X86::AND32rr, X86::AND32rr,
12484 X86::AND32ri, X86::AND32ri,
12485 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012486 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012487 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012488 X86::ADD32rr, X86::ADC32rr,
12489 X86::ADD32ri, X86::ADC32ri,
12490 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012491 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012492 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012493 X86::SUB32rr, X86::SBB32rr,
12494 X86::SUB32ri, X86::SBB32ri,
12495 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012496 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012497 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012498 X86::MOV32rr, X86::MOV32rr,
12499 X86::MOV32ri, X86::MOV32ri,
12500 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012501 case X86::VASTART_SAVE_XMM_REGS:
12502 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012503
12504 case X86::VAARG_64:
12505 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012506 }
12507}
12508
12509//===----------------------------------------------------------------------===//
12510// X86 Optimization Hooks
12511//===----------------------------------------------------------------------===//
12512
Dan Gohman475871a2008-07-27 21:46:04 +000012513void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012514 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012515 APInt &KnownZero,
12516 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012517 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012518 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012519 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012520 assert((Opc >= ISD::BUILTIN_OP_END ||
12521 Opc == ISD::INTRINSIC_WO_CHAIN ||
12522 Opc == ISD::INTRINSIC_W_CHAIN ||
12523 Opc == ISD::INTRINSIC_VOID) &&
12524 "Should use MaskedValueIsZero if you don't know whether Op"
12525 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012526
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012527 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012528 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012529 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012530 case X86ISD::ADD:
12531 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012532 case X86ISD::ADC:
12533 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012534 case X86ISD::SMUL:
12535 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012536 case X86ISD::INC:
12537 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012538 case X86ISD::OR:
12539 case X86ISD::XOR:
12540 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012541 // These nodes' second result is a boolean.
12542 if (Op.getResNo() == 0)
12543 break;
12544 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012545 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012546 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12547 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012548 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012549 case ISD::INTRINSIC_WO_CHAIN: {
12550 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
12551 unsigned NumLoBits = 0;
12552 switch (IntId) {
12553 default: break;
12554 case Intrinsic::x86_sse_movmsk_ps:
12555 case Intrinsic::x86_avx_movmsk_ps_256:
12556 case Intrinsic::x86_sse2_movmsk_pd:
12557 case Intrinsic::x86_avx_movmsk_pd_256:
12558 case Intrinsic::x86_mmx_pmovmskb:
12559 case Intrinsic::x86_sse2_pmovmskb_128: {
12560 // High bits of movmskp{s|d}, pmovmskb are known zero.
12561 switch (IntId) {
12562 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
12563 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
12564 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
12565 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
12566 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
12567 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
12568 }
12569 KnownZero = APInt::getHighBitsSet(Mask.getBitWidth(),
12570 Mask.getBitWidth() - NumLoBits);
12571 break;
12572 }
12573 }
12574 break;
12575 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012576 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012577}
Chris Lattner259e97c2006-01-31 19:43:35 +000012578
Owen Andersonbc146b02010-09-21 20:42:50 +000012579unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12580 unsigned Depth) const {
12581 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12582 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12583 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012584
Owen Andersonbc146b02010-09-21 20:42:50 +000012585 // Fallback case.
12586 return 1;
12587}
12588
Evan Cheng206ee9d2006-07-07 08:33:52 +000012589/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012590/// node is a GlobalAddress + offset.
12591bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012592 const GlobalValue* &GA,
12593 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012594 if (N->getOpcode() == X86ISD::Wrapper) {
12595 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012596 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012597 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012598 return true;
12599 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012600 }
Evan Chengad4196b2008-05-12 19:56:52 +000012601 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012602}
12603
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012604/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12605/// same as extracting the high 128-bit part of 256-bit vector and then
12606/// inserting the result into the low part of a new 256-bit vector
12607static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12608 EVT VT = SVOp->getValueType(0);
12609 int NumElems = VT.getVectorNumElements();
12610
12611 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12612 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12613 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12614 SVOp->getMaskElt(j) >= 0)
12615 return false;
12616
12617 return true;
12618}
12619
12620/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12621/// same as extracting the low 128-bit part of 256-bit vector and then
12622/// inserting the result into the high part of a new 256-bit vector
12623static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12624 EVT VT = SVOp->getValueType(0);
12625 int NumElems = VT.getVectorNumElements();
12626
12627 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12628 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12629 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12630 SVOp->getMaskElt(j) >= 0)
12631 return false;
12632
12633 return true;
12634}
12635
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012636/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12637static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12638 TargetLowering::DAGCombinerInfo &DCI) {
12639 DebugLoc dl = N->getDebugLoc();
12640 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12641 SDValue V1 = SVOp->getOperand(0);
12642 SDValue V2 = SVOp->getOperand(1);
12643 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012644 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012645
12646 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12647 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12648 //
12649 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012650 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012651 // V UNDEF BUILD_VECTOR UNDEF
12652 // \ / \ /
12653 // CONCAT_VECTOR CONCAT_VECTOR
12654 // \ /
12655 // \ /
12656 // RESULT: V + zero extended
12657 //
12658 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12659 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12660 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12661 return SDValue();
12662
12663 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12664 return SDValue();
12665
12666 // To match the shuffle mask, the first half of the mask should
12667 // be exactly the first vector, and all the rest a splat with the
12668 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012669 for (int i = 0; i < NumElems/2; ++i)
12670 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12671 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12672 return SDValue();
12673
12674 // Emit a zeroed vector and insert the desired subvector on its
12675 // first half.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000012676 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012677 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12678 DAG.getConstant(0, MVT::i32), DAG, dl);
12679 return DCI.CombineTo(N, InsV);
12680 }
12681
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012682 //===--------------------------------------------------------------------===//
12683 // Combine some shuffles into subvector extracts and inserts:
12684 //
12685
12686 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12687 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12688 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12689 DAG, dl);
12690 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12691 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12692 return DCI.CombineTo(N, InsV);
12693 }
12694
12695 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12696 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12697 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12698 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12699 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12700 return DCI.CombineTo(N, InsV);
12701 }
12702
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012703 return SDValue();
12704}
12705
12706/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012707static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012708 TargetLowering::DAGCombinerInfo &DCI,
12709 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012710 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012711 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012712
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012713 // Don't create instructions with illegal types after legalize types has run.
12714 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12715 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12716 return SDValue();
12717
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012718 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12719 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12720 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012721 return PerformShuffleCombine256(N, DAG, DCI);
12722
12723 // Only handle 128 wide vector from here on.
12724 if (VT.getSizeInBits() != 128)
12725 return SDValue();
12726
12727 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12728 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12729 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012730 SmallVector<SDValue, 16> Elts;
12731 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012732 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012733
Nate Begemanfdea31a2010-03-24 20:49:50 +000012734 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012735}
Evan Chengd880b972008-05-09 21:53:03 +000012736
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012737/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12738/// generation and convert it from being a bunch of shuffles and extracts
12739/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012740static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12741 const TargetLowering &TLI) {
12742 SDValue InputVector = N->getOperand(0);
12743
12744 // Only operate on vectors of 4 elements, where the alternative shuffling
12745 // gets to be more expensive.
12746 if (InputVector.getValueType() != MVT::v4i32)
12747 return SDValue();
12748
12749 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12750 // single use which is a sign-extend or zero-extend, and all elements are
12751 // used.
12752 SmallVector<SDNode *, 4> Uses;
12753 unsigned ExtractedElements = 0;
12754 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12755 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12756 if (UI.getUse().getResNo() != InputVector.getResNo())
12757 return SDValue();
12758
12759 SDNode *Extract = *UI;
12760 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12761 return SDValue();
12762
12763 if (Extract->getValueType(0) != MVT::i32)
12764 return SDValue();
12765 if (!Extract->hasOneUse())
12766 return SDValue();
12767 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12768 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12769 return SDValue();
12770 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12771 return SDValue();
12772
12773 // Record which element was extracted.
12774 ExtractedElements |=
12775 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12776
12777 Uses.push_back(Extract);
12778 }
12779
12780 // If not all the elements were used, this may not be worthwhile.
12781 if (ExtractedElements != 15)
12782 return SDValue();
12783
12784 // Ok, we've now decided to do the transformation.
12785 DebugLoc dl = InputVector.getDebugLoc();
12786
12787 // Store the value to a temporary stack slot.
12788 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012789 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12790 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012791
12792 // Replace each use (extract) with a load of the appropriate element.
12793 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12794 UE = Uses.end(); UI != UE; ++UI) {
12795 SDNode *Extract = *UI;
12796
Nadav Rotem86694292011-05-17 08:31:57 +000012797 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012798 SDValue Idx = Extract->getOperand(1);
12799 unsigned EltSize =
12800 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12801 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12802 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12803
Nadav Rotem86694292011-05-17 08:31:57 +000012804 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012805 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012806
12807 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012808 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012809 ScalarAddr, MachinePointerInfo(),
12810 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012811
12812 // Replace the exact with the load.
12813 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12814 }
12815
12816 // The replacement was made in place; don't return anything.
12817 return SDValue();
12818}
12819
Duncan Sands6bcd2192011-09-17 16:49:39 +000012820/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
12821/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012822static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012823 const X86Subtarget *Subtarget) {
12824 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012825 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012826 // Get the LHS/RHS of the select.
12827 SDValue LHS = N->getOperand(1);
12828 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000012829 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000012830
Dan Gohman670e5392009-09-21 18:03:22 +000012831 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012832 // instructions match the semantics of the common C idiom x<y?x:y but not
12833 // x<=y?x:y, because of how they handle negative zero (which can be
12834 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000012835 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
12836 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
12837 (Subtarget->hasXMMInt() ||
12838 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012839 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012840
Chris Lattner47b4ce82009-03-11 05:48:52 +000012841 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000012842 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000012843 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12844 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012845 switch (CC) {
12846 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012847 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012848 // Converting this to a min would handle NaNs incorrectly, and swapping
12849 // the operands would cause it to handle comparisons between positive
12850 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012851 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012852 if (!UnsafeFPMath &&
12853 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12854 break;
12855 std::swap(LHS, RHS);
12856 }
Dan Gohman670e5392009-09-21 18:03:22 +000012857 Opcode = X86ISD::FMIN;
12858 break;
12859 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012860 // Converting this to a min would handle comparisons between positive
12861 // and negative zero incorrectly.
12862 if (!UnsafeFPMath &&
12863 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12864 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012865 Opcode = X86ISD::FMIN;
12866 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012867 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012868 // Converting this to a min would handle both negative zeros and NaNs
12869 // incorrectly, but we can swap the operands to fix both.
12870 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012871 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012872 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012873 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012874 Opcode = X86ISD::FMIN;
12875 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012876
Dan Gohman670e5392009-09-21 18:03:22 +000012877 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012878 // Converting this to a max would handle comparisons between positive
12879 // and negative zero incorrectly.
12880 if (!UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000012881 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012882 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012883 Opcode = X86ISD::FMAX;
12884 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012885 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012886 // Converting this to a max would handle NaNs incorrectly, and swapping
12887 // the operands would cause it to handle comparisons between positive
12888 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012889 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012890 if (!UnsafeFPMath &&
12891 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12892 break;
12893 std::swap(LHS, RHS);
12894 }
Dan Gohman670e5392009-09-21 18:03:22 +000012895 Opcode = X86ISD::FMAX;
12896 break;
12897 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012898 // Converting this to a max would handle both negative zeros and NaNs
12899 // incorrectly, but we can swap the operands to fix both.
12900 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012901 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012902 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012903 case ISD::SETGE:
12904 Opcode = X86ISD::FMAX;
12905 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000012906 }
Dan Gohman670e5392009-09-21 18:03:22 +000012907 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000012908 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
12909 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012910 switch (CC) {
12911 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012912 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012913 // Converting this to a min would handle comparisons between positive
12914 // and negative zero incorrectly, and swapping the operands would
12915 // cause it to handle NaNs incorrectly.
12916 if (!UnsafeFPMath &&
12917 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000012918 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012919 break;
12920 std::swap(LHS, RHS);
12921 }
Dan Gohman670e5392009-09-21 18:03:22 +000012922 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000012923 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012924 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012925 // Converting this to a min would handle NaNs incorrectly.
12926 if (!UnsafeFPMath &&
12927 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
12928 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012929 Opcode = X86ISD::FMIN;
12930 break;
12931 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012932 // Converting this to a min would handle both negative zeros and NaNs
12933 // incorrectly, but we can swap the operands to fix both.
12934 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012935 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012936 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012937 case ISD::SETGE:
12938 Opcode = X86ISD::FMIN;
12939 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012940
Dan Gohman670e5392009-09-21 18:03:22 +000012941 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012942 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012943 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012944 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012945 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000012946 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012947 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012948 // Converting this to a max would handle comparisons between positive
12949 // and negative zero incorrectly, and swapping the operands would
12950 // cause it to handle NaNs incorrectly.
12951 if (!UnsafeFPMath &&
12952 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000012953 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012954 break;
12955 std::swap(LHS, RHS);
12956 }
Dan Gohman670e5392009-09-21 18:03:22 +000012957 Opcode = X86ISD::FMAX;
12958 break;
12959 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012960 // Converting this to a max would handle both negative zeros and NaNs
12961 // incorrectly, but we can swap the operands to fix both.
12962 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012963 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012964 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012965 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012966 Opcode = X86ISD::FMAX;
12967 break;
12968 }
Chris Lattner83e6c992006-10-04 06:57:07 +000012969 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012970
Chris Lattner47b4ce82009-03-11 05:48:52 +000012971 if (Opcode)
12972 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000012973 }
Eric Christopherfd179292009-08-27 18:07:15 +000012974
Chris Lattnerd1980a52009-03-12 06:52:53 +000012975 // If this is a select between two integer constants, try to do some
12976 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000012977 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
12978 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000012979 // Don't do this for crazy integer types.
12980 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
12981 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000012982 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012983 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000012984
Chris Lattnercee56e72009-03-13 05:53:31 +000012985 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000012986 // Efficiently invertible.
12987 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
12988 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
12989 isa<ConstantSDNode>(Cond.getOperand(1))))) {
12990 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000012991 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012992 }
Eric Christopherfd179292009-08-27 18:07:15 +000012993
Chris Lattnerd1980a52009-03-12 06:52:53 +000012994 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012995 if (FalseC->getAPIntValue() == 0 &&
12996 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012997 if (NeedsCondInvert) // Invert the condition if needed.
12998 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12999 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013000
Chris Lattnerd1980a52009-03-12 06:52:53 +000013001 // Zero extend the condition if needed.
13002 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013003
Chris Lattnercee56e72009-03-13 05:53:31 +000013004 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013005 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013006 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013007 }
Eric Christopherfd179292009-08-27 18:07:15 +000013008
Chris Lattner97a29a52009-03-13 05:22:11 +000013009 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013010 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013011 if (NeedsCondInvert) // Invert the condition if needed.
13012 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13013 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013014
Chris Lattner97a29a52009-03-13 05:22:11 +000013015 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013016 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13017 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013018 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013019 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013020 }
Eric Christopherfd179292009-08-27 18:07:15 +000013021
Chris Lattnercee56e72009-03-13 05:53:31 +000013022 // Optimize cases that will turn into an LEA instruction. This requires
13023 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013024 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013025 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013026 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013027
Chris Lattnercee56e72009-03-13 05:53:31 +000013028 bool isFastMultiplier = false;
13029 if (Diff < 10) {
13030 switch ((unsigned char)Diff) {
13031 default: break;
13032 case 1: // result = add base, cond
13033 case 2: // result = lea base( , cond*2)
13034 case 3: // result = lea base(cond, cond*2)
13035 case 4: // result = lea base( , cond*4)
13036 case 5: // result = lea base(cond, cond*4)
13037 case 8: // result = lea base( , cond*8)
13038 case 9: // result = lea base(cond, cond*8)
13039 isFastMultiplier = true;
13040 break;
13041 }
13042 }
Eric Christopherfd179292009-08-27 18:07:15 +000013043
Chris Lattnercee56e72009-03-13 05:53:31 +000013044 if (isFastMultiplier) {
13045 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
13046 if (NeedsCondInvert) // Invert the condition if needed.
13047 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13048 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013049
Chris Lattnercee56e72009-03-13 05:53:31 +000013050 // Zero extend the condition if needed.
13051 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13052 Cond);
13053 // Scale the condition by the difference.
13054 if (Diff != 1)
13055 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13056 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013057
Chris Lattnercee56e72009-03-13 05:53:31 +000013058 // Add the base if non-zero.
13059 if (FalseC->getAPIntValue() != 0)
13060 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13061 SDValue(FalseC, 0));
13062 return Cond;
13063 }
Eric Christopherfd179292009-08-27 18:07:15 +000013064 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013065 }
13066 }
Eric Christopherfd179292009-08-27 18:07:15 +000013067
Dan Gohman475871a2008-07-27 21:46:04 +000013068 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000013069}
13070
Chris Lattnerd1980a52009-03-12 06:52:53 +000013071/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
13072static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
13073 TargetLowering::DAGCombinerInfo &DCI) {
13074 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000013075
Chris Lattnerd1980a52009-03-12 06:52:53 +000013076 // If the flag operand isn't dead, don't touch this CMOV.
13077 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
13078 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000013079
Evan Chengb5a55d92011-05-24 01:48:22 +000013080 SDValue FalseOp = N->getOperand(0);
13081 SDValue TrueOp = N->getOperand(1);
13082 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
13083 SDValue Cond = N->getOperand(3);
13084 if (CC == X86::COND_E || CC == X86::COND_NE) {
13085 switch (Cond.getOpcode()) {
13086 default: break;
13087 case X86ISD::BSR:
13088 case X86ISD::BSF:
13089 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
13090 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
13091 return (CC == X86::COND_E) ? FalseOp : TrueOp;
13092 }
13093 }
13094
Chris Lattnerd1980a52009-03-12 06:52:53 +000013095 // If this is a select between two integer constants, try to do some
13096 // optimizations. Note that the operands are ordered the opposite of SELECT
13097 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000013098 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
13099 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013100 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
13101 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000013102 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
13103 CC = X86::GetOppositeBranchCondition(CC);
13104 std::swap(TrueC, FalseC);
13105 }
Eric Christopherfd179292009-08-27 18:07:15 +000013106
Chris Lattnerd1980a52009-03-12 06:52:53 +000013107 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013108 // This is efficient for any integer data type (including i8/i16) and
13109 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013110 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013111 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13112 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013113
Chris Lattnerd1980a52009-03-12 06:52:53 +000013114 // Zero extend the condition if needed.
13115 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013116
Chris Lattnerd1980a52009-03-12 06:52:53 +000013117 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
13118 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013119 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013120 if (N->getNumValues() == 2) // Dead flag value?
13121 return DCI.CombineTo(N, Cond, SDValue());
13122 return Cond;
13123 }
Eric Christopherfd179292009-08-27 18:07:15 +000013124
Chris Lattnercee56e72009-03-13 05:53:31 +000013125 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
13126 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000013127 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013128 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13129 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013130
Chris Lattner97a29a52009-03-13 05:22:11 +000013131 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013132 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13133 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013134 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13135 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000013136
Chris Lattner97a29a52009-03-13 05:22:11 +000013137 if (N->getNumValues() == 2) // Dead flag value?
13138 return DCI.CombineTo(N, Cond, SDValue());
13139 return Cond;
13140 }
Eric Christopherfd179292009-08-27 18:07:15 +000013141
Chris Lattnercee56e72009-03-13 05:53:31 +000013142 // Optimize cases that will turn into an LEA instruction. This requires
13143 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013144 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013145 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013146 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013147
Chris Lattnercee56e72009-03-13 05:53:31 +000013148 bool isFastMultiplier = false;
13149 if (Diff < 10) {
13150 switch ((unsigned char)Diff) {
13151 default: break;
13152 case 1: // result = add base, cond
13153 case 2: // result = lea base( , cond*2)
13154 case 3: // result = lea base(cond, cond*2)
13155 case 4: // result = lea base( , cond*4)
13156 case 5: // result = lea base(cond, cond*4)
13157 case 8: // result = lea base( , cond*8)
13158 case 9: // result = lea base(cond, cond*8)
13159 isFastMultiplier = true;
13160 break;
13161 }
13162 }
Eric Christopherfd179292009-08-27 18:07:15 +000013163
Chris Lattnercee56e72009-03-13 05:53:31 +000013164 if (isFastMultiplier) {
13165 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013166 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13167 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000013168 // Zero extend the condition if needed.
13169 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13170 Cond);
13171 // Scale the condition by the difference.
13172 if (Diff != 1)
13173 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13174 DAG.getConstant(Diff, Cond.getValueType()));
13175
13176 // Add the base if non-zero.
13177 if (FalseC->getAPIntValue() != 0)
13178 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13179 SDValue(FalseC, 0));
13180 if (N->getNumValues() == 2) // Dead flag value?
13181 return DCI.CombineTo(N, Cond, SDValue());
13182 return Cond;
13183 }
Eric Christopherfd179292009-08-27 18:07:15 +000013184 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013185 }
13186 }
13187 return SDValue();
13188}
13189
13190
Evan Cheng0b0cd912009-03-28 05:57:29 +000013191/// PerformMulCombine - Optimize a single multiply with constant into two
13192/// in order to implement it with two cheaper instructions, e.g.
13193/// LEA + SHL, LEA + LEA.
13194static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
13195 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000013196 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
13197 return SDValue();
13198
Owen Andersone50ed302009-08-10 22:56:29 +000013199 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000013200 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000013201 return SDValue();
13202
13203 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
13204 if (!C)
13205 return SDValue();
13206 uint64_t MulAmt = C->getZExtValue();
13207 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
13208 return SDValue();
13209
13210 uint64_t MulAmt1 = 0;
13211 uint64_t MulAmt2 = 0;
13212 if ((MulAmt % 9) == 0) {
13213 MulAmt1 = 9;
13214 MulAmt2 = MulAmt / 9;
13215 } else if ((MulAmt % 5) == 0) {
13216 MulAmt1 = 5;
13217 MulAmt2 = MulAmt / 5;
13218 } else if ((MulAmt % 3) == 0) {
13219 MulAmt1 = 3;
13220 MulAmt2 = MulAmt / 3;
13221 }
13222 if (MulAmt2 &&
13223 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
13224 DebugLoc DL = N->getDebugLoc();
13225
13226 if (isPowerOf2_64(MulAmt2) &&
13227 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
13228 // If second multiplifer is pow2, issue it first. We want the multiply by
13229 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
13230 // is an add.
13231 std::swap(MulAmt1, MulAmt2);
13232
13233 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000013234 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013235 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000013236 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000013237 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013238 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000013239 DAG.getConstant(MulAmt1, VT));
13240
Eric Christopherfd179292009-08-27 18:07:15 +000013241 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013242 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000013243 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000013244 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013245 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000013246 DAG.getConstant(MulAmt2, VT));
13247
13248 // Do not add new nodes to DAG combiner worklist.
13249 DCI.CombineTo(N, NewMul, false);
13250 }
13251 return SDValue();
13252}
13253
Evan Chengad9c0a32009-12-15 00:53:42 +000013254static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
13255 SDValue N0 = N->getOperand(0);
13256 SDValue N1 = N->getOperand(1);
13257 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13258 EVT VT = N0.getValueType();
13259
13260 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13261 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013262 if (VT.isInteger() && !VT.isVector() &&
13263 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000013264 N0.getOperand(1).getOpcode() == ISD::Constant) {
13265 SDValue N00 = N0.getOperand(0);
13266 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13267 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13268 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13269 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13270 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13271 APInt ShAmt = N1C->getAPIntValue();
13272 Mask = Mask.shl(ShAmt);
13273 if (Mask != 0)
13274 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13275 N00, DAG.getConstant(Mask, VT));
13276 }
13277 }
13278
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013279
13280 // Hardware support for vector shifts is sparse which makes us scalarize the
13281 // vector operations in many cases. Also, on sandybridge ADD is faster than
13282 // shl.
13283 // (shl V, 1) -> add V,V
13284 if (isSplatVector(N1.getNode())) {
13285 assert(N0.getValueType().isVector() && "Invalid vector shift type");
13286 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
13287 // We shift all of the values by one. In many cases we do not have
13288 // hardware support for this operation. This is better expressed as an ADD
13289 // of two values.
13290 if (N1C && (1 == N1C->getZExtValue())) {
13291 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
13292 }
13293 }
13294
Evan Chengad9c0a32009-12-15 00:53:42 +000013295 return SDValue();
13296}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013297
Nate Begeman740ab032009-01-26 00:52:55 +000013298/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13299/// when possible.
13300static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13301 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013302 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013303 if (N->getOpcode() == ISD::SHL) {
13304 SDValue V = PerformSHLCombine(N, DAG);
13305 if (V.getNode()) return V;
13306 }
Evan Chengad9c0a32009-12-15 00:53:42 +000013307
Nate Begeman740ab032009-01-26 00:52:55 +000013308 // On X86 with SSE2 support, we can transform this to a vector shift if
13309 // all elements are shifted by the same amount. We can't do this in legalize
13310 // because the a constant vector is typically transformed to a constant pool
13311 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013312 if (!Subtarget->hasXMMInt())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013313 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013314
Owen Anderson825b72b2009-08-11 20:47:22 +000013315 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013316 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013317
Mon P Wang3becd092009-01-28 08:12:05 +000013318 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013319 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013320 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013321 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013322 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13323 unsigned NumElts = VT.getVectorNumElements();
13324 unsigned i = 0;
13325 for (; i != NumElts; ++i) {
13326 SDValue Arg = ShAmtOp.getOperand(i);
13327 if (Arg.getOpcode() == ISD::UNDEF) continue;
13328 BaseShAmt = Arg;
13329 break;
13330 }
13331 for (; i != NumElts; ++i) {
13332 SDValue Arg = ShAmtOp.getOperand(i);
13333 if (Arg.getOpcode() == ISD::UNDEF) continue;
13334 if (Arg != BaseShAmt) {
13335 return SDValue();
13336 }
13337 }
13338 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013339 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013340 SDValue InVec = ShAmtOp.getOperand(0);
13341 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13342 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13343 unsigned i = 0;
13344 for (; i != NumElts; ++i) {
13345 SDValue Arg = InVec.getOperand(i);
13346 if (Arg.getOpcode() == ISD::UNDEF) continue;
13347 BaseShAmt = Arg;
13348 break;
13349 }
13350 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13351 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013352 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013353 if (C->getZExtValue() == SplatIdx)
13354 BaseShAmt = InVec.getOperand(1);
13355 }
13356 }
13357 if (BaseShAmt.getNode() == 0)
13358 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13359 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013360 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013361 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013362
Mon P Wangefa42202009-09-03 19:56:25 +000013363 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013364 if (EltVT.bitsGT(MVT::i32))
13365 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13366 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013367 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013368
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013369 // The shift amount is identical so we can do a vector shift.
13370 SDValue ValOp = N->getOperand(0);
13371 switch (N->getOpcode()) {
13372 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013373 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013374 break;
13375 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013376 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013377 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013378 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013379 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013380 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013381 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013382 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013383 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013384 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013385 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013386 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013387 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013388 break;
13389 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013390 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013391 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013392 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013393 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013394 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013395 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013396 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013397 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013398 break;
13399 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013400 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013401 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013402 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013403 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013404 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013405 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013406 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013407 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013408 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013409 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013410 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013411 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013412 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013413 }
13414 return SDValue();
13415}
13416
Nate Begemanb65c1752010-12-17 22:55:37 +000013417
Stuart Hastings865f0932011-06-03 23:53:54 +000013418// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13419// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13420// and friends. Likewise for OR -> CMPNEQSS.
13421static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13422 TargetLowering::DAGCombinerInfo &DCI,
13423 const X86Subtarget *Subtarget) {
13424 unsigned opcode;
13425
13426 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13427 // we're requiring SSE2 for both.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013428 if (Subtarget->hasXMMInt() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013429 SDValue N0 = N->getOperand(0);
13430 SDValue N1 = N->getOperand(1);
13431 SDValue CMP0 = N0->getOperand(1);
13432 SDValue CMP1 = N1->getOperand(1);
13433 DebugLoc DL = N->getDebugLoc();
13434
13435 // The SETCCs should both refer to the same CMP.
13436 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13437 return SDValue();
13438
13439 SDValue CMP00 = CMP0->getOperand(0);
13440 SDValue CMP01 = CMP0->getOperand(1);
13441 EVT VT = CMP00.getValueType();
13442
13443 if (VT == MVT::f32 || VT == MVT::f64) {
13444 bool ExpectingFlags = false;
13445 // Check for any users that want flags:
13446 for (SDNode::use_iterator UI = N->use_begin(),
13447 UE = N->use_end();
13448 !ExpectingFlags && UI != UE; ++UI)
13449 switch (UI->getOpcode()) {
13450 default:
13451 case ISD::BR_CC:
13452 case ISD::BRCOND:
13453 case ISD::SELECT:
13454 ExpectingFlags = true;
13455 break;
13456 case ISD::CopyToReg:
13457 case ISD::SIGN_EXTEND:
13458 case ISD::ZERO_EXTEND:
13459 case ISD::ANY_EXTEND:
13460 break;
13461 }
13462
13463 if (!ExpectingFlags) {
13464 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13465 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13466
13467 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13468 X86::CondCode tmp = cc0;
13469 cc0 = cc1;
13470 cc1 = tmp;
13471 }
13472
13473 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13474 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13475 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13476 X86ISD::NodeType NTOperator = is64BitFP ?
13477 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13478 // FIXME: need symbolic constants for these magic numbers.
13479 // See X86ATTInstPrinter.cpp:printSSECC().
13480 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13481 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13482 DAG.getConstant(x86cc, MVT::i8));
13483 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13484 OnesOrZeroesF);
13485 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13486 DAG.getConstant(1, MVT::i32));
13487 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13488 return OneBitOfTruth;
13489 }
13490 }
13491 }
13492 }
13493 return SDValue();
13494}
13495
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013496/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13497/// so it can be folded inside ANDNP.
13498static bool CanFoldXORWithAllOnes(const SDNode *N) {
13499 EVT VT = N->getValueType(0);
13500
13501 // Match direct AllOnes for 128 and 256-bit vectors
13502 if (ISD::isBuildVectorAllOnes(N))
13503 return true;
13504
13505 // Look through a bit convert.
13506 if (N->getOpcode() == ISD::BITCAST)
13507 N = N->getOperand(0).getNode();
13508
13509 // Sometimes the operand may come from a insert_subvector building a 256-bit
13510 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013511 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013512 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13513 SDValue V1 = N->getOperand(0);
13514 SDValue V2 = N->getOperand(1);
13515
13516 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13517 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13518 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13519 ISD::isBuildVectorAllOnes(V2.getNode()))
13520 return true;
13521 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013522
13523 return false;
13524}
13525
Nate Begemanb65c1752010-12-17 22:55:37 +000013526static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13527 TargetLowering::DAGCombinerInfo &DCI,
13528 const X86Subtarget *Subtarget) {
13529 if (DCI.isBeforeLegalizeOps())
13530 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013531
Stuart Hastings865f0932011-06-03 23:53:54 +000013532 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13533 if (R.getNode())
13534 return R;
13535
Craig Topper54a11172011-10-14 07:06:56 +000013536 EVT VT = N->getValueType(0);
13537
Craig Topperb4c94572011-10-21 06:55:01 +000013538 // Create ANDN, BLSI, and BLSR instructions
13539 // BLSI is X & (-X)
13540 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000013541 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
13542 SDValue N0 = N->getOperand(0);
13543 SDValue N1 = N->getOperand(1);
13544 DebugLoc DL = N->getDebugLoc();
13545
13546 // Check LHS for not
13547 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
13548 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
13549 // Check RHS for not
13550 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
13551 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
13552
Craig Topperb4c94572011-10-21 06:55:01 +000013553 // Check LHS for neg
13554 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
13555 isZero(N0.getOperand(0)))
13556 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
13557
13558 // Check RHS for neg
13559 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
13560 isZero(N1.getOperand(0)))
13561 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
13562
13563 // Check LHS for X-1
13564 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13565 isAllOnes(N0.getOperand(1)))
13566 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
13567
13568 // Check RHS for X-1
13569 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13570 isAllOnes(N1.getOperand(1)))
13571 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
13572
Craig Topper54a11172011-10-14 07:06:56 +000013573 return SDValue();
13574 }
13575
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013576 // Want to form ANDNP nodes:
13577 // 1) In the hopes of then easily combining them with OR and AND nodes
13578 // to form PBLEND/PSIGN.
13579 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013580 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013581 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013582
Nate Begemanb65c1752010-12-17 22:55:37 +000013583 SDValue N0 = N->getOperand(0);
13584 SDValue N1 = N->getOperand(1);
13585 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013586
Nate Begemanb65c1752010-12-17 22:55:37 +000013587 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013588 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013589 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13590 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013591 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013592
13593 // Check RHS for vnot
13594 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013595 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13596 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013597 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013598
Nate Begemanb65c1752010-12-17 22:55:37 +000013599 return SDValue();
13600}
13601
Evan Cheng760d1942010-01-04 21:22:48 +000013602static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013603 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013604 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013605 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013606 return SDValue();
13607
Stuart Hastings865f0932011-06-03 23:53:54 +000013608 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13609 if (R.getNode())
13610 return R;
13611
Evan Cheng760d1942010-01-04 21:22:48 +000013612 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000013613 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000013614 return SDValue();
13615
Evan Cheng760d1942010-01-04 21:22:48 +000013616 SDValue N0 = N->getOperand(0);
13617 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013618
Nate Begemanb65c1752010-12-17 22:55:37 +000013619 // look for psign/blend
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013620 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013621 if (VT == MVT::v2i64) {
13622 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013623 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000013624 std::swap(N0, N1);
13625 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013626 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013627 SDValue Mask = N1.getOperand(0);
13628 SDValue X = N1.getOperand(1);
13629 SDValue Y;
13630 if (N0.getOperand(0) == Mask)
13631 Y = N0.getOperand(1);
13632 if (N0.getOperand(1) == Mask)
13633 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013634
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013635 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000013636 if (!Y.getNode())
13637 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013638
Nate Begemanb65c1752010-12-17 22:55:37 +000013639 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13640 if (Mask.getOpcode() != ISD::BITCAST ||
13641 X.getOpcode() != ISD::BITCAST ||
13642 Y.getOpcode() != ISD::BITCAST)
13643 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013644
Nate Begemanb65c1752010-12-17 22:55:37 +000013645 // Look through mask bitcast.
13646 Mask = Mask.getOperand(0);
13647 EVT MaskVT = Mask.getValueType();
13648
13649 // Validate that the Mask operand is a vector sra node. The sra node
13650 // will be an intrinsic.
13651 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13652 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013653
Nate Begemanb65c1752010-12-17 22:55:37 +000013654 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13655 // there is no psrai.b
13656 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13657 case Intrinsic::x86_sse2_psrai_w:
13658 case Intrinsic::x86_sse2_psrai_d:
13659 break;
13660 default: return SDValue();
13661 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013662
Nate Begemanb65c1752010-12-17 22:55:37 +000013663 // Check that the SRA is all signbits.
13664 SDValue SraC = Mask.getOperand(2);
13665 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13666 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13667 if ((SraAmt + 1) != EltBits)
13668 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013669
Nate Begemanb65c1752010-12-17 22:55:37 +000013670 DebugLoc DL = N->getDebugLoc();
13671
13672 // Now we know we at least have a plendvb with the mask val. See if
13673 // we can form a psignb/w/d.
13674 // psign = x.type == y.type == mask.type && y = sub(0, x);
13675 X = X.getOperand(0);
13676 Y = Y.getOperand(0);
13677 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13678 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
13679 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
13680 unsigned Opc = 0;
13681 switch (EltBits) {
13682 case 8: Opc = X86ISD::PSIGNB; break;
13683 case 16: Opc = X86ISD::PSIGNW; break;
13684 case 32: Opc = X86ISD::PSIGND; break;
13685 default: break;
13686 }
13687 if (Opc) {
13688 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
13689 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
13690 }
13691 }
13692 // PBLENDVB only available on SSE 4.1
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013693 if (!(Subtarget->hasSSE41() || Subtarget->hasAVX()))
Nate Begemanb65c1752010-12-17 22:55:37 +000013694 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013695
Nate Begemanb65c1752010-12-17 22:55:37 +000013696 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
13697 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
13698 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000013699 Mask = DAG.getNode(ISD::VSELECT, DL, MVT::v16i8, Mask, X, Y);
Nate Begemanb65c1752010-12-17 22:55:37 +000013700 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
13701 }
13702 }
13703 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013704
Nate Begemanb65c1752010-12-17 22:55:37 +000013705 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013706 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13707 std::swap(N0, N1);
13708 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13709 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013710 if (!N0.hasOneUse() || !N1.hasOneUse())
13711 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013712
13713 SDValue ShAmt0 = N0.getOperand(1);
13714 if (ShAmt0.getValueType() != MVT::i8)
13715 return SDValue();
13716 SDValue ShAmt1 = N1.getOperand(1);
13717 if (ShAmt1.getValueType() != MVT::i8)
13718 return SDValue();
13719 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13720 ShAmt0 = ShAmt0.getOperand(0);
13721 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13722 ShAmt1 = ShAmt1.getOperand(0);
13723
13724 DebugLoc DL = N->getDebugLoc();
13725 unsigned Opc = X86ISD::SHLD;
13726 SDValue Op0 = N0.getOperand(0);
13727 SDValue Op1 = N1.getOperand(0);
13728 if (ShAmt0.getOpcode() == ISD::SUB) {
13729 Opc = X86ISD::SHRD;
13730 std::swap(Op0, Op1);
13731 std::swap(ShAmt0, ShAmt1);
13732 }
13733
Evan Cheng8b1190a2010-04-28 01:18:01 +000013734 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013735 if (ShAmt1.getOpcode() == ISD::SUB) {
13736 SDValue Sum = ShAmt1.getOperand(0);
13737 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013738 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13739 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13740 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13741 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013742 return DAG.getNode(Opc, DL, VT,
13743 Op0, Op1,
13744 DAG.getNode(ISD::TRUNCATE, DL,
13745 MVT::i8, ShAmt0));
13746 }
13747 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13748 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13749 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013750 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013751 return DAG.getNode(Opc, DL, VT,
13752 N0.getOperand(0), N1.getOperand(0),
13753 DAG.getNode(ISD::TRUNCATE, DL,
13754 MVT::i8, ShAmt0));
13755 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013756
Evan Cheng760d1942010-01-04 21:22:48 +000013757 return SDValue();
13758}
13759
Craig Topperb4c94572011-10-21 06:55:01 +000013760static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
13761 TargetLowering::DAGCombinerInfo &DCI,
13762 const X86Subtarget *Subtarget) {
13763 if (DCI.isBeforeLegalizeOps())
13764 return SDValue();
13765
13766 EVT VT = N->getValueType(0);
13767
13768 if (VT != MVT::i32 && VT != MVT::i64)
13769 return SDValue();
13770
13771 // Create BLSMSK instructions by finding X ^ (X-1)
13772 SDValue N0 = N->getOperand(0);
13773 SDValue N1 = N->getOperand(1);
13774 DebugLoc DL = N->getDebugLoc();
13775
13776 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13777 isAllOnes(N0.getOperand(1)))
13778 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
13779
13780 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13781 isAllOnes(N1.getOperand(1)))
13782 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
13783
13784 return SDValue();
13785}
13786
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013787/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
13788static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
13789 const X86Subtarget *Subtarget) {
13790 LoadSDNode *Ld = cast<LoadSDNode>(N);
13791 EVT RegVT = Ld->getValueType(0);
13792 EVT MemVT = Ld->getMemoryVT();
13793 DebugLoc dl = Ld->getDebugLoc();
13794 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13795
13796 ISD::LoadExtType Ext = Ld->getExtensionType();
13797
Nadav Rotemca6f2962011-09-18 19:00:23 +000013798 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013799 // shuffle. We need SSE4 for the shuffles.
13800 // TODO: It is possible to support ZExt by zeroing the undef values
13801 // during the shuffle phase or after the shuffle.
13802 if (RegVT.isVector() && Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
13803 assert(MemVT != RegVT && "Cannot extend to the same type");
13804 assert(MemVT.isVector() && "Must load a vector from memory");
13805
13806 unsigned NumElems = RegVT.getVectorNumElements();
13807 unsigned RegSz = RegVT.getSizeInBits();
13808 unsigned MemSz = MemVT.getSizeInBits();
13809 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000013810 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013811 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
13812
13813 // Attempt to load the original value using a single load op.
13814 // Find a scalar type which is equal to the loaded word size.
13815 MVT SclrLoadTy = MVT::i8;
13816 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13817 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13818 MVT Tp = (MVT::SimpleValueType)tp;
13819 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() == MemSz) {
13820 SclrLoadTy = Tp;
13821 break;
13822 }
13823 }
13824
13825 // Proceed if a load word is found.
13826 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
13827
13828 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
13829 RegSz/SclrLoadTy.getSizeInBits());
13830
13831 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
13832 RegSz/MemVT.getScalarType().getSizeInBits());
13833 // Can't shuffle using an illegal type.
13834 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13835
13836 // Perform a single load.
13837 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
13838 Ld->getBasePtr(),
13839 Ld->getPointerInfo(), Ld->isVolatile(),
13840 Ld->isNonTemporal(), Ld->getAlignment());
13841
13842 // Insert the word loaded into a vector.
13843 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
13844 LoadUnitVecVT, ScalarLoad);
13845
13846 // Bitcast the loaded value to a vector of the original element type, in
13847 // the size of the target vector type.
13848 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, ScalarInVector);
13849 unsigned SizeRatio = RegSz/MemSz;
13850
13851 // Redistribute the loaded elements into the different locations.
13852 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13853 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
13854
13855 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
13856 DAG.getUNDEF(SlicedVec.getValueType()),
13857 ShuffleVec.data());
13858
13859 // Bitcast to the requested type.
13860 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
13861 // Replace the original load with the new sequence
13862 // and return the new chain.
13863 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
13864 return SDValue(ScalarLoad.getNode(), 1);
13865 }
13866
13867 return SDValue();
13868}
13869
Chris Lattner149a4e52008-02-22 02:09:43 +000013870/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013871static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000013872 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000013873 StoreSDNode *St = cast<StoreSDNode>(N);
13874 EVT VT = St->getValue().getValueType();
13875 EVT StVT = St->getMemoryVT();
13876 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000013877 SDValue StoredVal = St->getOperand(1);
13878 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13879
13880 // If we are saving a concatination of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000013881 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
13882 // 128-bit ones. If in the future the cost becomes only one memory access the
13883 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000013884 if (VT.getSizeInBits() == 256 &&
13885 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
13886 StoredVal.getNumOperands() == 2) {
13887
13888 SDValue Value0 = StoredVal.getOperand(0);
13889 SDValue Value1 = StoredVal.getOperand(1);
13890
13891 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
13892 SDValue Ptr0 = St->getBasePtr();
13893 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
13894
13895 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
13896 St->getPointerInfo(), St->isVolatile(),
13897 St->isNonTemporal(), St->getAlignment());
13898 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
13899 St->getPointerInfo(), St->isVolatile(),
13900 St->isNonTemporal(), St->getAlignment());
13901 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
13902 }
Nadav Rotem614061b2011-08-10 19:30:14 +000013903
13904 // Optimize trunc store (of multiple scalars) to shuffle and store.
13905 // First, pack all of the elements in one place. Next, store to memory
13906 // in fewer chunks.
13907 if (St->isTruncatingStore() && VT.isVector()) {
13908 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13909 unsigned NumElems = VT.getVectorNumElements();
13910 assert(StVT != VT && "Cannot truncate to the same type");
13911 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
13912 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
13913
13914 // From, To sizes and ElemCount must be pow of two
13915 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013916 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000013917 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013918 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013919
Nadav Rotem614061b2011-08-10 19:30:14 +000013920 unsigned SizeRatio = FromSz / ToSz;
13921
13922 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
13923
13924 // Create a type on which we perform the shuffle
13925 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
13926 StVT.getScalarType(), NumElems*SizeRatio);
13927
13928 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
13929
13930 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
13931 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13932 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
13933
13934 // Can't shuffle using an illegal type
13935 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13936
13937 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
13938 DAG.getUNDEF(WideVec.getValueType()),
13939 ShuffleVec.data());
13940 // At this point all of the data is stored at the bottom of the
13941 // register. We now need to save it to mem.
13942
13943 // Find the largest store unit
13944 MVT StoreType = MVT::i8;
13945 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13946 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13947 MVT Tp = (MVT::SimpleValueType)tp;
13948 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
13949 StoreType = Tp;
13950 }
13951
13952 // Bitcast the original vector into a vector of store-size units
13953 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
13954 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
13955 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
13956 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
13957 SmallVector<SDValue, 8> Chains;
13958 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
13959 TLI.getPointerTy());
13960 SDValue Ptr = St->getBasePtr();
13961
13962 // Perform one or more big stores into memory.
13963 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
13964 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
13965 StoreType, ShuffWide,
13966 DAG.getIntPtrConstant(i));
13967 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
13968 St->getPointerInfo(), St->isVolatile(),
13969 St->isNonTemporal(), St->getAlignment());
13970 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
13971 Chains.push_back(Ch);
13972 }
13973
13974 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
13975 Chains.size());
13976 }
13977
13978
Chris Lattner149a4e52008-02-22 02:09:43 +000013979 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
13980 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000013981 // A preferable solution to the general problem is to figure out the right
13982 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000013983
13984 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000013985 if (VT.getSizeInBits() != 64)
13986 return SDValue();
13987
Devang Patel578efa92009-06-05 21:57:13 +000013988 const Function *F = DAG.getMachineFunction().getFunction();
13989 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000013990 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013991 && Subtarget->hasXMMInt();
Evan Cheng536e6672009-03-12 05:59:15 +000013992 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000013993 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000013994 isa<LoadSDNode>(St->getValue()) &&
13995 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
13996 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013997 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013998 LoadSDNode *Ld = 0;
13999 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000014000 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000014001 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014002 // Must be a store of a load. We currently handle two cases: the load
14003 // is a direct child, and it's under an intervening TokenFactor. It is
14004 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000014005 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000014006 Ld = cast<LoadSDNode>(St->getChain());
14007 else if (St->getValue().hasOneUse() &&
14008 ChainVal->getOpcode() == ISD::TokenFactor) {
14009 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014010 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000014011 TokenFactorIndex = i;
14012 Ld = cast<LoadSDNode>(St->getValue());
14013 } else
14014 Ops.push_back(ChainVal->getOperand(i));
14015 }
14016 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000014017
Evan Cheng536e6672009-03-12 05:59:15 +000014018 if (!Ld || !ISD::isNormalLoad(Ld))
14019 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014020
Evan Cheng536e6672009-03-12 05:59:15 +000014021 // If this is not the MMX case, i.e. we are just turning i64 load/store
14022 // into f64 load/store, avoid the transformation if there are multiple
14023 // uses of the loaded value.
14024 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
14025 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014026
Evan Cheng536e6672009-03-12 05:59:15 +000014027 DebugLoc LdDL = Ld->getDebugLoc();
14028 DebugLoc StDL = N->getDebugLoc();
14029 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
14030 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
14031 // pair instead.
14032 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014033 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000014034 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
14035 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000014036 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014037 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000014038 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000014039 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000014040 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000014041 Ops.size());
14042 }
Evan Cheng536e6672009-03-12 05:59:15 +000014043 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014044 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014045 St->isVolatile(), St->isNonTemporal(),
14046 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000014047 }
Evan Cheng536e6672009-03-12 05:59:15 +000014048
14049 // Otherwise, lower to two pairs of 32-bit loads / stores.
14050 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014051 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
14052 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014053
Owen Anderson825b72b2009-08-11 20:47:22 +000014054 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014055 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014056 Ld->isVolatile(), Ld->isNonTemporal(),
14057 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000014058 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014059 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000014060 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000014061 MinAlign(Ld->getAlignment(), 4));
14062
14063 SDValue NewChain = LoLd.getValue(1);
14064 if (TokenFactorIndex != -1) {
14065 Ops.push_back(LoLd);
14066 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000014067 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000014068 Ops.size());
14069 }
14070
14071 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014072 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
14073 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014074
14075 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014076 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014077 St->isVolatile(), St->isNonTemporal(),
14078 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014079 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014080 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000014081 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000014082 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000014083 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000014084 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000014085 }
Dan Gohman475871a2008-07-27 21:46:04 +000014086 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000014087}
14088
Duncan Sands17470be2011-09-22 20:15:48 +000014089/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
14090/// and return the operands for the horizontal operation in LHS and RHS. A
14091/// horizontal operation performs the binary operation on successive elements
14092/// of its first operand, then on successive elements of its second operand,
14093/// returning the resulting values in a vector. For example, if
14094/// A = < float a0, float a1, float a2, float a3 >
14095/// and
14096/// B = < float b0, float b1, float b2, float b3 >
14097/// then the result of doing a horizontal operation on A and B is
14098/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
14099/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
14100/// A horizontal-op B, for some already available A and B, and if so then LHS is
14101/// set to A, RHS to B, and the routine returns 'true'.
14102/// Note that the binary operation should have the property that if one of the
14103/// operands is UNDEF then the result is UNDEF.
14104static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool isCommutative) {
14105 // Look for the following pattern: if
14106 // A = < float a0, float a1, float a2, float a3 >
14107 // B = < float b0, float b1, float b2, float b3 >
14108 // and
14109 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
14110 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
14111 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
14112 // which is A horizontal-op B.
14113
14114 // At least one of the operands should be a vector shuffle.
14115 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
14116 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
14117 return false;
14118
14119 EVT VT = LHS.getValueType();
14120 unsigned N = VT.getVectorNumElements();
14121
14122 // View LHS in the form
14123 // LHS = VECTOR_SHUFFLE A, B, LMask
14124 // If LHS is not a shuffle then pretend it is the shuffle
14125 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
14126 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
14127 // type VT.
14128 SDValue A, B;
14129 SmallVector<int, 8> LMask(N);
14130 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14131 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
14132 A = LHS.getOperand(0);
14133 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
14134 B = LHS.getOperand(1);
14135 cast<ShuffleVectorSDNode>(LHS.getNode())->getMask(LMask);
14136 } else {
14137 if (LHS.getOpcode() != ISD::UNDEF)
14138 A = LHS;
14139 for (unsigned i = 0; i != N; ++i)
14140 LMask[i] = i;
14141 }
14142
14143 // Likewise, view RHS in the form
14144 // RHS = VECTOR_SHUFFLE C, D, RMask
14145 SDValue C, D;
14146 SmallVector<int, 8> RMask(N);
14147 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14148 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
14149 C = RHS.getOperand(0);
14150 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
14151 D = RHS.getOperand(1);
14152 cast<ShuffleVectorSDNode>(RHS.getNode())->getMask(RMask);
14153 } else {
14154 if (RHS.getOpcode() != ISD::UNDEF)
14155 C = RHS;
14156 for (unsigned i = 0; i != N; ++i)
14157 RMask[i] = i;
14158 }
14159
14160 // Check that the shuffles are both shuffling the same vectors.
14161 if (!(A == C && B == D) && !(A == D && B == C))
14162 return false;
14163
14164 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
14165 if (!A.getNode() && !B.getNode())
14166 return false;
14167
14168 // If A and B occur in reverse order in RHS, then "swap" them (which means
14169 // rewriting the mask).
14170 if (A != C)
14171 for (unsigned i = 0; i != N; ++i) {
14172 unsigned Idx = RMask[i];
14173 if (Idx < N)
14174 RMask[i] += N;
14175 else if (Idx < 2*N)
14176 RMask[i] -= N;
14177 }
14178
14179 // At this point LHS and RHS are equivalent to
14180 // LHS = VECTOR_SHUFFLE A, B, LMask
14181 // RHS = VECTOR_SHUFFLE A, B, RMask
14182 // Check that the masks correspond to performing a horizontal operation.
14183 for (unsigned i = 0; i != N; ++i) {
14184 unsigned LIdx = LMask[i], RIdx = RMask[i];
14185
14186 // Ignore any UNDEF components.
14187 if (LIdx >= 2*N || RIdx >= 2*N || (!A.getNode() && (LIdx < N || RIdx < N))
14188 || (!B.getNode() && (LIdx >= N || RIdx >= N)))
14189 continue;
14190
14191 // Check that successive elements are being operated on. If not, this is
14192 // not a horizontal operation.
14193 if (!(LIdx == 2*i && RIdx == 2*i + 1) &&
14194 !(isCommutative && LIdx == 2*i + 1 && RIdx == 2*i))
14195 return false;
14196 }
14197
14198 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
14199 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
14200 return true;
14201}
14202
14203/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
14204static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
14205 const X86Subtarget *Subtarget) {
14206 EVT VT = N->getValueType(0);
14207 SDValue LHS = N->getOperand(0);
14208 SDValue RHS = N->getOperand(1);
14209
14210 // Try to synthesize horizontal adds from adds of shuffles.
14211 if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
14212 (VT == MVT::v4f32 || VT == MVT::v2f64) &&
14213 isHorizontalBinOp(LHS, RHS, true))
14214 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
14215 return SDValue();
14216}
14217
14218/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
14219static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
14220 const X86Subtarget *Subtarget) {
14221 EVT VT = N->getValueType(0);
14222 SDValue LHS = N->getOperand(0);
14223 SDValue RHS = N->getOperand(1);
14224
14225 // Try to synthesize horizontal subs from subs of shuffles.
14226 if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
14227 (VT == MVT::v4f32 || VT == MVT::v2f64) &&
14228 isHorizontalBinOp(LHS, RHS, false))
14229 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
14230 return SDValue();
14231}
14232
Chris Lattner6cf73262008-01-25 06:14:17 +000014233/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
14234/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014235static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000014236 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
14237 // F[X]OR(0.0, x) -> x
14238 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000014239 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14240 if (C->getValueAPF().isPosZero())
14241 return N->getOperand(1);
14242 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14243 if (C->getValueAPF().isPosZero())
14244 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000014245 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014246}
14247
14248/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014249static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000014250 // FAND(0.0, x) -> 0.0
14251 // FAND(x, 0.0) -> 0.0
14252 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14253 if (C->getValueAPF().isPosZero())
14254 return N->getOperand(0);
14255 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14256 if (C->getValueAPF().isPosZero())
14257 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000014258 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014259}
14260
Dan Gohmane5af2d32009-01-29 01:59:02 +000014261static SDValue PerformBTCombine(SDNode *N,
14262 SelectionDAG &DAG,
14263 TargetLowering::DAGCombinerInfo &DCI) {
14264 // BT ignores high bits in the bit index operand.
14265 SDValue Op1 = N->getOperand(1);
14266 if (Op1.hasOneUse()) {
14267 unsigned BitWidth = Op1.getValueSizeInBits();
14268 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
14269 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014270 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
14271 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000014272 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000014273 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
14274 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
14275 DCI.CommitTargetLoweringOpt(TLO);
14276 }
14277 return SDValue();
14278}
Chris Lattner83e6c992006-10-04 06:57:07 +000014279
Eli Friedman7a5e5552009-06-07 06:52:44 +000014280static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
14281 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014282 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000014283 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000014284 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000014285 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000014286 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000014287 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014288 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014289 }
14290 return SDValue();
14291}
14292
Evan Cheng2e489c42009-12-16 00:53:11 +000014293static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
14294 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
14295 // (and (i32 x86isd::setcc_carry), 1)
14296 // This eliminates the zext. This transformation is necessary because
14297 // ISD::SETCC is always legalized to i8.
14298 DebugLoc dl = N->getDebugLoc();
14299 SDValue N0 = N->getOperand(0);
14300 EVT VT = N->getValueType(0);
14301 if (N0.getOpcode() == ISD::AND &&
14302 N0.hasOneUse() &&
14303 N0.getOperand(0).hasOneUse()) {
14304 SDValue N00 = N0.getOperand(0);
14305 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
14306 return SDValue();
14307 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
14308 if (!C || C->getZExtValue() != 1)
14309 return SDValue();
14310 return DAG.getNode(ISD::AND, dl, VT,
14311 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
14312 N00.getOperand(0), N00.getOperand(1)),
14313 DAG.getConstant(1, VT));
14314 }
14315
14316 return SDValue();
14317}
14318
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014319// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
14320static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
14321 unsigned X86CC = N->getConstantOperandVal(0);
14322 SDValue EFLAG = N->getOperand(1);
14323 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014324
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014325 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
14326 // a zext and produces an all-ones bit which is more useful than 0/1 in some
14327 // cases.
14328 if (X86CC == X86::COND_B)
14329 return DAG.getNode(ISD::AND, DL, MVT::i8,
14330 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
14331 DAG.getConstant(X86CC, MVT::i8), EFLAG),
14332 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014333
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014334 return SDValue();
14335}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014336
Benjamin Kramer1396c402011-06-18 11:09:41 +000014337static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
14338 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014339 SDValue Op0 = N->getOperand(0);
14340 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
14341 // a 32-bit target where SSE doesn't support i64->FP operations.
14342 if (Op0.getOpcode() == ISD::LOAD) {
14343 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
14344 EVT VT = Ld->getValueType(0);
14345 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
14346 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
14347 !XTLI->getSubtarget()->is64Bit() &&
14348 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000014349 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
14350 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014351 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
14352 return FILDChain;
14353 }
14354 }
14355 return SDValue();
14356}
14357
Chris Lattner23a01992010-12-20 01:37:09 +000014358// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
14359static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
14360 X86TargetLowering::DAGCombinerInfo &DCI) {
14361 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
14362 // the result is either zero or one (depending on the input carry bit).
14363 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
14364 if (X86::isZeroNode(N->getOperand(0)) &&
14365 X86::isZeroNode(N->getOperand(1)) &&
14366 // We don't have a good way to replace an EFLAGS use, so only do this when
14367 // dead right now.
14368 SDValue(N, 1).use_empty()) {
14369 DebugLoc DL = N->getDebugLoc();
14370 EVT VT = N->getValueType(0);
14371 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
14372 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
14373 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
14374 DAG.getConstant(X86::COND_B,MVT::i8),
14375 N->getOperand(2)),
14376 DAG.getConstant(1, VT));
14377 return DCI.CombineTo(N, Res1, CarryOut);
14378 }
14379
14380 return SDValue();
14381}
14382
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014383// fold (add Y, (sete X, 0)) -> adc 0, Y
14384// (add Y, (setne X, 0)) -> sbb -1, Y
14385// (sub (sete X, 0), Y) -> sbb 0, Y
14386// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014387static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014388 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014389
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014390 // Look through ZExts.
14391 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
14392 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
14393 return SDValue();
14394
14395 SDValue SetCC = Ext.getOperand(0);
14396 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
14397 return SDValue();
14398
14399 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
14400 if (CC != X86::COND_E && CC != X86::COND_NE)
14401 return SDValue();
14402
14403 SDValue Cmp = SetCC.getOperand(1);
14404 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000014405 !X86::isZeroNode(Cmp.getOperand(1)) ||
14406 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014407 return SDValue();
14408
14409 SDValue CmpOp0 = Cmp.getOperand(0);
14410 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
14411 DAG.getConstant(1, CmpOp0.getValueType()));
14412
14413 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
14414 if (CC == X86::COND_NE)
14415 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
14416 DL, OtherVal.getValueType(), OtherVal,
14417 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
14418 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
14419 DL, OtherVal.getValueType(), OtherVal,
14420 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
14421}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014422
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014423static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG) {
14424 SDValue Op0 = N->getOperand(0);
14425 SDValue Op1 = N->getOperand(1);
14426
14427 // X86 can't encode an immediate LHS of a sub. See if we can push the
14428 // negation into a preceding instruction.
14429 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014430 // If the RHS of the sub is a XOR with one use and a constant, invert the
14431 // immediate. Then add one to the LHS of the sub so we can turn
14432 // X-Y -> X+~Y+1, saving one register.
14433 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
14434 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000014435 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014436 EVT VT = Op0.getValueType();
14437 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
14438 Op1.getOperand(0),
14439 DAG.getConstant(~XorC, VT));
14440 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000014441 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014442 }
14443 }
14444
14445 return OptimizeConditionalInDecrement(N, DAG);
14446}
14447
Dan Gohman475871a2008-07-27 21:46:04 +000014448SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000014449 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014450 SelectionDAG &DAG = DCI.DAG;
14451 switch (N->getOpcode()) {
14452 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014453 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014454 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000014455 case ISD::VSELECT:
Chris Lattneraf723b92008-01-25 05:46:26 +000014456 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014457 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014458 case ISD::ADD: return OptimizeConditionalInDecrement(N, DAG);
14459 case ISD::SUB: return PerformSubCombine(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000014460 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000014461 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000014462 case ISD::SHL:
14463 case ISD::SRA:
14464 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000014465 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000014466 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000014467 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014468 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000014469 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014470 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000014471 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
14472 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000014473 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000014474 case X86ISD::FOR: return PerformFORCombine(N, DAG);
14475 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000014476 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014477 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000014478 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014479 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014480 case X86ISD::SHUFPS: // Handle all target specific shuffles
14481 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000014482 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014483 case X86ISD::PUNPCKHBW:
14484 case X86ISD::PUNPCKHWD:
14485 case X86ISD::PUNPCKHDQ:
14486 case X86ISD::PUNPCKHQDQ:
14487 case X86ISD::UNPCKHPS:
14488 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +000014489 case X86ISD::VUNPCKHPSY:
14490 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014491 case X86ISD::PUNPCKLBW:
14492 case X86ISD::PUNPCKLWD:
14493 case X86ISD::PUNPCKLDQ:
14494 case X86ISD::PUNPCKLQDQ:
14495 case X86ISD::UNPCKLPS:
14496 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000014497 case X86ISD::VUNPCKLPSY:
14498 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014499 case X86ISD::MOVHLPS:
14500 case X86ISD::MOVLHPS:
14501 case X86ISD::PSHUFD:
14502 case X86ISD::PSHUFHW:
14503 case X86ISD::PSHUFLW:
14504 case X86ISD::MOVSS:
14505 case X86ISD::MOVSD:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000014506 case X86ISD::VPERMILPS:
14507 case X86ISD::VPERMILPSY:
14508 case X86ISD::VPERMILPD:
14509 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000014510 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014511 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014512 }
14513
Dan Gohman475871a2008-07-27 21:46:04 +000014514 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014515}
14516
Evan Chenge5b51ac2010-04-17 06:13:15 +000014517/// isTypeDesirableForOp - Return true if the target has native support for
14518/// the specified value type and it is 'desirable' to use the type for the
14519/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14520/// instruction encodings are longer and some i16 instructions are slow.
14521bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14522 if (!isTypeLegal(VT))
14523 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014524 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014525 return true;
14526
14527 switch (Opc) {
14528 default:
14529 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014530 case ISD::LOAD:
14531 case ISD::SIGN_EXTEND:
14532 case ISD::ZERO_EXTEND:
14533 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014534 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014535 case ISD::SRL:
14536 case ISD::SUB:
14537 case ISD::ADD:
14538 case ISD::MUL:
14539 case ISD::AND:
14540 case ISD::OR:
14541 case ISD::XOR:
14542 return false;
14543 }
14544}
14545
14546/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014547/// beneficial for dag combiner to promote the specified node. If true, it
14548/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014549bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014550 EVT VT = Op.getValueType();
14551 if (VT != MVT::i16)
14552 return false;
14553
Evan Cheng4c26e932010-04-19 19:29:22 +000014554 bool Promote = false;
14555 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014556 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014557 default: break;
14558 case ISD::LOAD: {
14559 LoadSDNode *LD = cast<LoadSDNode>(Op);
14560 // If the non-extending load has a single use and it's not live out, then it
14561 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014562 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14563 Op.hasOneUse()*/) {
14564 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14565 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14566 // The only case where we'd want to promote LOAD (rather then it being
14567 // promoted as an operand is when it's only use is liveout.
14568 if (UI->getOpcode() != ISD::CopyToReg)
14569 return false;
14570 }
14571 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014572 Promote = true;
14573 break;
14574 }
14575 case ISD::SIGN_EXTEND:
14576 case ISD::ZERO_EXTEND:
14577 case ISD::ANY_EXTEND:
14578 Promote = true;
14579 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014580 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014581 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014582 SDValue N0 = Op.getOperand(0);
14583 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014584 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014585 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014586 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014587 break;
14588 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014589 case ISD::ADD:
14590 case ISD::MUL:
14591 case ISD::AND:
14592 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014593 case ISD::XOR:
14594 Commute = true;
14595 // fallthrough
14596 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014597 SDValue N0 = Op.getOperand(0);
14598 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014599 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014600 return false;
14601 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014602 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014603 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014604 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014605 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014606 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014607 }
14608 }
14609
14610 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014611 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014612}
14613
Evan Cheng60c07e12006-07-05 22:17:51 +000014614//===----------------------------------------------------------------------===//
14615// X86 Inline Assembly Support
14616//===----------------------------------------------------------------------===//
14617
Chris Lattnerb8105652009-07-20 17:51:36 +000014618bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14619 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014620
14621 std::string AsmStr = IA->getAsmString();
14622
14623 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014624 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014625 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014626
14627 switch (AsmPieces.size()) {
14628 default: return false;
14629 case 1:
14630 AsmStr = AsmPieces[0];
14631 AsmPieces.clear();
14632 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
14633
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014634 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000014635 // we will turn this bswap into something that will be lowered to logical ops
14636 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14637 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014638 // bswap $0
14639 if (AsmPieces.size() == 2 &&
14640 (AsmPieces[0] == "bswap" ||
14641 AsmPieces[0] == "bswapq" ||
14642 AsmPieces[0] == "bswapl") &&
14643 (AsmPieces[1] == "$0" ||
14644 AsmPieces[1] == "${0:q}")) {
14645 // No need to check constraints, nothing other than the equivalent of
14646 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014647 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014648 if (!Ty || Ty->getBitWidth() % 16 != 0)
14649 return false;
14650 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014651 }
14652 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014653 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014654 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014655 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014656 AsmPieces[1] == "$$8," &&
14657 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014658 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14659 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014660 const std::string &ConstraintsStr = IA->getConstraintString();
14661 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014662 std::sort(AsmPieces.begin(), AsmPieces.end());
14663 if (AsmPieces.size() == 4 &&
14664 AsmPieces[0] == "~{cc}" &&
14665 AsmPieces[1] == "~{dirflag}" &&
14666 AsmPieces[2] == "~{flags}" &&
14667 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014668 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014669 if (!Ty || Ty->getBitWidth() % 16 != 0)
14670 return false;
14671 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014672 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014673 }
14674 break;
14675 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014676 if (CI->getType()->isIntegerTy(32) &&
14677 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14678 SmallVector<StringRef, 4> Words;
14679 SplitString(AsmPieces[0], Words, " \t,");
14680 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14681 Words[2] == "${0:w}") {
14682 Words.clear();
14683 SplitString(AsmPieces[1], Words, " \t,");
14684 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14685 Words[2] == "$0") {
14686 Words.clear();
14687 SplitString(AsmPieces[2], Words, " \t,");
14688 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14689 Words[2] == "${0:w}") {
14690 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014691 const std::string &ConstraintsStr = IA->getConstraintString();
14692 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014693 std::sort(AsmPieces.begin(), AsmPieces.end());
14694 if (AsmPieces.size() == 4 &&
14695 AsmPieces[0] == "~{cc}" &&
14696 AsmPieces[1] == "~{dirflag}" &&
14697 AsmPieces[2] == "~{flags}" &&
14698 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014699 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014700 if (!Ty || Ty->getBitWidth() % 16 != 0)
14701 return false;
14702 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014703 }
14704 }
14705 }
14706 }
14707 }
Evan Cheng55d42002011-01-08 01:24:27 +000014708
14709 if (CI->getType()->isIntegerTy(64)) {
14710 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14711 if (Constraints.size() >= 2 &&
14712 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14713 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14714 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14715 SmallVector<StringRef, 4> Words;
14716 SplitString(AsmPieces[0], Words, " \t");
14717 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014718 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014719 SplitString(AsmPieces[1], Words, " \t");
14720 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14721 Words.clear();
14722 SplitString(AsmPieces[2], Words, " \t,");
14723 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14724 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014725 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014726 if (!Ty || Ty->getBitWidth() % 16 != 0)
14727 return false;
14728 return IntrinsicLowering::LowerToByteSwap(CI);
14729 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014730 }
14731 }
14732 }
14733 }
14734 break;
14735 }
14736 return false;
14737}
14738
14739
14740
Chris Lattnerf4dff842006-07-11 02:54:03 +000014741/// getConstraintType - Given a constraint letter, return the type of
14742/// constraint it is for this target.
14743X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014744X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14745 if (Constraint.size() == 1) {
14746 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014747 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014748 case 'q':
14749 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014750 case 'f':
14751 case 't':
14752 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014753 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014754 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014755 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014756 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014757 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014758 case 'a':
14759 case 'b':
14760 case 'c':
14761 case 'd':
14762 case 'S':
14763 case 'D':
14764 case 'A':
14765 return C_Register;
14766 case 'I':
14767 case 'J':
14768 case 'K':
14769 case 'L':
14770 case 'M':
14771 case 'N':
14772 case 'G':
14773 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014774 case 'e':
14775 case 'Z':
14776 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014777 default:
14778 break;
14779 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014780 }
Chris Lattner4234f572007-03-25 02:14:49 +000014781 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014782}
14783
John Thompson44ab89e2010-10-29 17:29:13 +000014784/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014785/// This object must already have been set up with the operand type
14786/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014787TargetLowering::ConstraintWeight
14788 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014789 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014790 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014791 Value *CallOperandVal = info.CallOperandVal;
14792 // If we don't have a value, we can't do a match,
14793 // but allow it at the lowest weight.
14794 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014795 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014796 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014797 // Look at the constraint type.
14798 switch (*constraint) {
14799 default:
John Thompson44ab89e2010-10-29 17:29:13 +000014800 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
14801 case 'R':
14802 case 'q':
14803 case 'Q':
14804 case 'a':
14805 case 'b':
14806 case 'c':
14807 case 'd':
14808 case 'S':
14809 case 'D':
14810 case 'A':
14811 if (CallOperandVal->getType()->isIntegerTy())
14812 weight = CW_SpecificReg;
14813 break;
14814 case 'f':
14815 case 't':
14816 case 'u':
14817 if (type->isFloatingPointTy())
14818 weight = CW_SpecificReg;
14819 break;
14820 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000014821 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000014822 weight = CW_SpecificReg;
14823 break;
14824 case 'x':
14825 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014826 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000014827 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014828 break;
14829 case 'I':
14830 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
14831 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000014832 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014833 }
14834 break;
John Thompson44ab89e2010-10-29 17:29:13 +000014835 case 'J':
14836 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14837 if (C->getZExtValue() <= 63)
14838 weight = CW_Constant;
14839 }
14840 break;
14841 case 'K':
14842 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14843 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
14844 weight = CW_Constant;
14845 }
14846 break;
14847 case 'L':
14848 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14849 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
14850 weight = CW_Constant;
14851 }
14852 break;
14853 case 'M':
14854 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14855 if (C->getZExtValue() <= 3)
14856 weight = CW_Constant;
14857 }
14858 break;
14859 case 'N':
14860 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14861 if (C->getZExtValue() <= 0xff)
14862 weight = CW_Constant;
14863 }
14864 break;
14865 case 'G':
14866 case 'C':
14867 if (dyn_cast<ConstantFP>(CallOperandVal)) {
14868 weight = CW_Constant;
14869 }
14870 break;
14871 case 'e':
14872 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14873 if ((C->getSExtValue() >= -0x80000000LL) &&
14874 (C->getSExtValue() <= 0x7fffffffLL))
14875 weight = CW_Constant;
14876 }
14877 break;
14878 case 'Z':
14879 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14880 if (C->getZExtValue() <= 0xffffffff)
14881 weight = CW_Constant;
14882 }
14883 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014884 }
14885 return weight;
14886}
14887
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014888/// LowerXConstraint - try to replace an X constraint, which matches anything,
14889/// with another that has more specific requirements based on the type of the
14890/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000014891const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000014892LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000014893 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
14894 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000014895 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014896 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000014897 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014898 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000014899 return "x";
14900 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014901
Chris Lattner5e764232008-04-26 23:02:14 +000014902 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014903}
14904
Chris Lattner48884cd2007-08-25 00:47:38 +000014905/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
14906/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000014907void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000014908 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000014909 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000014910 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000014911 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000014912
Eric Christopher100c8332011-06-02 23:16:42 +000014913 // Only support length 1 constraints for now.
14914 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000014915
Eric Christopher100c8332011-06-02 23:16:42 +000014916 char ConstraintLetter = Constraint[0];
14917 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014918 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000014919 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000014920 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014921 if (C->getZExtValue() <= 31) {
14922 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014923 break;
14924 }
Devang Patel84f7fd22007-03-17 00:13:28 +000014925 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014926 return;
Evan Cheng364091e2008-09-22 23:57:37 +000014927 case 'J':
14928 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014929 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000014930 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14931 break;
14932 }
14933 }
14934 return;
14935 case 'K':
14936 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014937 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000014938 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14939 break;
14940 }
14941 }
14942 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000014943 case 'N':
14944 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014945 if (C->getZExtValue() <= 255) {
14946 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014947 break;
14948 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000014949 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014950 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000014951 case 'e': {
14952 // 32-bit signed value
14953 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014954 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14955 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014956 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014957 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000014958 break;
14959 }
14960 // FIXME gcc accepts some relocatable values here too, but only in certain
14961 // memory models; it's complicated.
14962 }
14963 return;
14964 }
14965 case 'Z': {
14966 // 32-bit unsigned value
14967 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014968 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14969 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014970 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14971 break;
14972 }
14973 }
14974 // FIXME gcc accepts some relocatable values here too, but only in certain
14975 // memory models; it's complicated.
14976 return;
14977 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014978 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014979 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000014980 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014981 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014982 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000014983 break;
14984 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014985
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014986 // In any sort of PIC mode addresses need to be computed at runtime by
14987 // adding in a register or some sort of table lookup. These can't
14988 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000014989 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014990 return;
14991
Chris Lattnerdc43a882007-05-03 16:52:29 +000014992 // If we are in non-pic codegen mode, we allow the address of a global (with
14993 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000014994 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014995 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000014996
Chris Lattner49921962009-05-08 18:23:14 +000014997 // Match either (GA), (GA+C), (GA+C1+C2), etc.
14998 while (1) {
14999 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
15000 Offset += GA->getOffset();
15001 break;
15002 } else if (Op.getOpcode() == ISD::ADD) {
15003 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15004 Offset += C->getZExtValue();
15005 Op = Op.getOperand(0);
15006 continue;
15007 }
15008 } else if (Op.getOpcode() == ISD::SUB) {
15009 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15010 Offset += -C->getZExtValue();
15011 Op = Op.getOperand(0);
15012 continue;
15013 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015014 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015015
Chris Lattner49921962009-05-08 18:23:14 +000015016 // Otherwise, this isn't something we can handle, reject it.
15017 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015018 }
Eric Christopherfd179292009-08-27 18:07:15 +000015019
Dan Gohman46510a72010-04-15 01:51:59 +000015020 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015021 // If we require an extra load to get this address, as in PIC mode, we
15022 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000015023 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
15024 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015025 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000015026
Devang Patel0d881da2010-07-06 22:08:15 +000015027 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
15028 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000015029 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015030 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015031 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015032
Gabor Greifba36cb52008-08-28 21:40:38 +000015033 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000015034 Ops.push_back(Result);
15035 return;
15036 }
Dale Johannesen1784d162010-06-25 21:55:36 +000015037 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015038}
15039
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015040std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000015041X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000015042 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000015043 // First, see if this is a constraint that directly corresponds to an LLVM
15044 // register class.
15045 if (Constraint.size() == 1) {
15046 // GCC Constraint Letters
15047 switch (Constraint[0]) {
15048 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000015049 // TODO: Slight differences here in allocation order and leaving
15050 // RIP in the class. Do they matter any more here than they do
15051 // in the normal allocation?
15052 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
15053 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015054 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015055 return std::make_pair(0U, X86::GR32RegisterClass);
15056 else if (VT == MVT::i16)
15057 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015058 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015059 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015060 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000015061 return std::make_pair(0U, X86::GR64RegisterClass);
15062 break;
15063 }
15064 // 32-bit fallthrough
15065 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015066 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015067 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
15068 else if (VT == MVT::i16)
15069 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015070 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015071 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
15072 else if (VT == MVT::i64)
15073 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
15074 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015075 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000015076 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015077 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000015078 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015079 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000015080 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000015081 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000015082 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000015083 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015084 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015085 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015086 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
15087 if (VT == MVT::i16)
15088 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
15089 if (VT == MVT::i32 || !Subtarget->is64Bit())
15090 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
15091 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015092 case 'f': // FP Stack registers.
15093 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
15094 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000015095 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015096 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015097 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015098 return std::make_pair(0U, X86::RFP64RegisterClass);
15099 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000015100 case 'y': // MMX_REGS if MMX allowed.
15101 if (!Subtarget->hasMMX()) break;
15102 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015103 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015104 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015105 // FALL THROUGH.
15106 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015107 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000015108
Owen Anderson825b72b2009-08-11 20:47:22 +000015109 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000015110 default: break;
15111 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015112 case MVT::f32:
15113 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000015114 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015115 case MVT::f64:
15116 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000015117 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015118 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015119 case MVT::v16i8:
15120 case MVT::v8i16:
15121 case MVT::v4i32:
15122 case MVT::v2i64:
15123 case MVT::v4f32:
15124 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000015125 return std::make_pair(0U, X86::VR128RegisterClass);
15126 }
Chris Lattnerad043e82007-04-09 05:11:28 +000015127 break;
15128 }
15129 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015130
Chris Lattnerf76d1802006-07-31 23:26:50 +000015131 // Use the default implementation in TargetLowering to convert the register
15132 // constraint into a member of a register class.
15133 std::pair<unsigned, const TargetRegisterClass*> Res;
15134 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000015135
15136 // Not found as a standard register?
15137 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015138 // Map st(0) -> st(7) -> ST0
15139 if (Constraint.size() == 7 && Constraint[0] == '{' &&
15140 tolower(Constraint[1]) == 's' &&
15141 tolower(Constraint[2]) == 't' &&
15142 Constraint[3] == '(' &&
15143 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
15144 Constraint[5] == ')' &&
15145 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000015146
Chris Lattner56d77c72009-09-13 22:41:48 +000015147 Res.first = X86::ST0+Constraint[4]-'0';
15148 Res.second = X86::RFP80RegisterClass;
15149 return Res;
15150 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015151
Chris Lattner56d77c72009-09-13 22:41:48 +000015152 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015153 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000015154 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000015155 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015156 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000015157 }
Chris Lattner56d77c72009-09-13 22:41:48 +000015158
15159 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015160 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015161 Res.first = X86::EFLAGS;
15162 Res.second = X86::CCRRegisterClass;
15163 return Res;
15164 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015165
Dale Johannesen330169f2008-11-13 21:52:36 +000015166 // 'A' means EAX + EDX.
15167 if (Constraint == "A") {
15168 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000015169 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015170 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000015171 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000015172 return Res;
15173 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015174
Chris Lattnerf76d1802006-07-31 23:26:50 +000015175 // Otherwise, check to see if this is a register class of the wrong value
15176 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
15177 // turn into {ax},{dx}.
15178 if (Res.second->hasType(VT))
15179 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015180
Chris Lattnerf76d1802006-07-31 23:26:50 +000015181 // All of the single-register GCC register classes map their values onto
15182 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
15183 // really want an 8-bit or 32-bit register, map to the appropriate register
15184 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000015185 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015186 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015187 unsigned DestReg = 0;
15188 switch (Res.first) {
15189 default: break;
15190 case X86::AX: DestReg = X86::AL; break;
15191 case X86::DX: DestReg = X86::DL; break;
15192 case X86::CX: DestReg = X86::CL; break;
15193 case X86::BX: DestReg = X86::BL; break;
15194 }
15195 if (DestReg) {
15196 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015197 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015198 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015199 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015200 unsigned DestReg = 0;
15201 switch (Res.first) {
15202 default: break;
15203 case X86::AX: DestReg = X86::EAX; break;
15204 case X86::DX: DestReg = X86::EDX; break;
15205 case X86::CX: DestReg = X86::ECX; break;
15206 case X86::BX: DestReg = X86::EBX; break;
15207 case X86::SI: DestReg = X86::ESI; break;
15208 case X86::DI: DestReg = X86::EDI; break;
15209 case X86::BP: DestReg = X86::EBP; break;
15210 case X86::SP: DestReg = X86::ESP; break;
15211 }
15212 if (DestReg) {
15213 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015214 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015215 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015216 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015217 unsigned DestReg = 0;
15218 switch (Res.first) {
15219 default: break;
15220 case X86::AX: DestReg = X86::RAX; break;
15221 case X86::DX: DestReg = X86::RDX; break;
15222 case X86::CX: DestReg = X86::RCX; break;
15223 case X86::BX: DestReg = X86::RBX; break;
15224 case X86::SI: DestReg = X86::RSI; break;
15225 case X86::DI: DestReg = X86::RDI; break;
15226 case X86::BP: DestReg = X86::RBP; break;
15227 case X86::SP: DestReg = X86::RSP; break;
15228 }
15229 if (DestReg) {
15230 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015231 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015232 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000015233 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000015234 } else if (Res.second == X86::FR32RegisterClass ||
15235 Res.second == X86::FR64RegisterClass ||
15236 Res.second == X86::VR128RegisterClass) {
15237 // Handle references to XMM physical registers that got mapped into the
15238 // wrong class. This can happen with constraints like {xmm0} where the
15239 // target independent register mapper will just pick the first match it can
15240 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000015241 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015242 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000015243 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015244 Res.second = X86::FR64RegisterClass;
15245 else if (X86::VR128RegisterClass->hasType(VT))
15246 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000015247 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015248
Chris Lattnerf76d1802006-07-31 23:26:50 +000015249 return Res;
15250}