blob: 0839ecd1755d08af6c0a19794ba07e813c83011b [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000041#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000044#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000045#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000048#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000050#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000053#include "llvm/Support/raw_ostream.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000054#include "llvm/Target/TargetOptions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000055using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000056using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000057
Evan Chengb1712452010-01-27 06:25:16 +000058STATISTIC(NumTailCalls, "Number of tail calls");
59
Evan Cheng10e86422008-04-25 19:11:04 +000060// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000061static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000062 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000063
David Greenea5f26012011-02-07 19:36:54 +000064static SDValue Insert128BitVector(SDValue Result,
65 SDValue Vec,
66 SDValue Idx,
67 SelectionDAG &DAG,
68 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000069
David Greenea5f26012011-02-07 19:36:54 +000070static SDValue Extract128BitVector(SDValue Vec,
71 SDValue Idx,
72 SelectionDAG &DAG,
73 DebugLoc dl);
74
75/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
76/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000077/// simple subregister reference. Idx is an index in the 128 bits we
78/// want. It need not be aligned to a 128-bit bounday. That makes
79/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000080static SDValue Extract128BitVector(SDValue Vec,
81 SDValue Idx,
82 SelectionDAG &DAG,
83 DebugLoc dl) {
84 EVT VT = Vec.getValueType();
85 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000086 EVT ElVT = VT.getVectorElementType();
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000087 int Factor = VT.getSizeInBits()/128;
88 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
89 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000090
91 // Extract from UNDEF is UNDEF.
92 if (Vec.getOpcode() == ISD::UNDEF)
93 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
94
95 if (isa<ConstantSDNode>(Idx)) {
96 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
97
98 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
99 // we can match to VEXTRACTF128.
100 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
101
102 // This is the index of the first element of the 128-bit chunk
103 // we want.
104 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
105 * ElemsPerChunk);
106
107 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000108 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
109 VecIdx);
110
111 return Result;
112 }
113
114 return SDValue();
115}
116
117/// Generate a DAG to put 128-bits into a vector > 128 bits. This
118/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000119/// simple superregister reference. Idx is an index in the 128 bits
120/// we want. It need not be aligned to a 128-bit bounday. That makes
121/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000122static SDValue Insert128BitVector(SDValue Result,
123 SDValue Vec,
124 SDValue Idx,
125 SelectionDAG &DAG,
126 DebugLoc dl) {
127 if (isa<ConstantSDNode>(Idx)) {
128 EVT VT = Vec.getValueType();
129 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
130
131 EVT ElVT = VT.getVectorElementType();
David Greenea5f26012011-02-07 19:36:54 +0000132 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
David Greenea5f26012011-02-07 19:36:54 +0000133 EVT ResultVT = Result.getValueType();
134
135 // Insert the relevant 128 bits.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000136 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000137
138 // This is the index of the first element of the 128-bit chunk
139 // we want.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000140 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
David Greenea5f26012011-02-07 19:36:54 +0000141 * ElemsPerChunk);
142
143 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000144 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
145 VecIdx);
146 return Result;
147 }
148
149 return SDValue();
150}
151
Chris Lattnerf0144122009-07-28 03:13:23 +0000152static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000153 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
154 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000155
Evan Cheng2bffee22011-02-01 01:14:13 +0000156 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000157 if (is64Bit)
158 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000159 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000160 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000161
Evan Cheng203576a2011-07-20 19:50:42 +0000162 if (Subtarget->isTargetELF())
163 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000164 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000165 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000166 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000167}
168
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000169X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000170 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000171 Subtarget = &TM.getSubtarget<X86Subtarget>();
Bruno Cardoso Lopesaed890b2011-08-01 21:54:05 +0000172 X86ScalarSSEf64 = Subtarget->hasXMMInt() || Subtarget->hasAVX();
173 X86ScalarSSEf32 = Subtarget->hasXMM() || Subtarget->hasAVX();
Evan Cheng25ab6902006-09-08 06:48:29 +0000174 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000175
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000176 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000177 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000178
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000179 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000180 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000181
182 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000183 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000184 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
185 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000186
Eric Christopherde5e1012011-03-11 01:05:58 +0000187 // For 64-bit since we have so many registers use the ILP scheduler, for
188 // 32-bit code use the register pressure specific scheduling.
189 if (Subtarget->is64Bit())
190 setSchedulingPreference(Sched::ILP);
191 else
192 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000193 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000194
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000195 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000196 // Setup Windows compiler runtime calls.
197 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000198 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000199 setLibcallName(RTLIB::SREM_I64, "_allrem");
200 setLibcallName(RTLIB::UREM_I64, "_aullrem");
201 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000202 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000203 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000204 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000205 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000206 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
207 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
208 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000209 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
210 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000211 }
212
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000213 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000214 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000215 setUseUnderscoreSetJmp(false);
216 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000217 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000218 // MS runtime is weird: it exports _setjmp, but longjmp!
219 setUseUnderscoreSetJmp(true);
220 setUseUnderscoreLongJmp(false);
221 } else {
222 setUseUnderscoreSetJmp(true);
223 setUseUnderscoreLongJmp(true);
224 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000225
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000226 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000228 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000230 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000232
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000234
Scott Michelfdc40a02009-02-17 22:15:04 +0000235 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000237 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000239 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
241 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000242
243 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
245 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
246 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
247 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
248 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
249 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000250
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000251 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
252 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000253 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
254 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
255 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000256
Evan Cheng25ab6902006-09-08 06:48:29 +0000257 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
259 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000260 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000261 // We have an algorithm for SSE2->double, and we turn this into a
262 // 64-bit FILD followed by conditional FADD for other targets.
263 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000264 // We have an algorithm for SSE2, and we turn this into a 64-bit
265 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000266 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000267 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000268
269 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
270 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
272 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000273
Devang Patel6a784892009-06-05 18:48:29 +0000274 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000275 // SSE has no i16 to fp conversion, only i32
276 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000278 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000280 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
282 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000283 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000284 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
286 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000287 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000288
Dale Johannesen73328d12007-09-19 23:55:34 +0000289 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
290 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
292 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000293
Evan Cheng02568ff2006-01-30 22:13:22 +0000294 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
295 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
297 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000298
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000299 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000301 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000303 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
305 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000306 }
307
308 // Handle FP_TO_UINT by promoting the destination to a larger signed
309 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
311 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
312 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000313
Evan Cheng25ab6902006-09-08 06:48:29 +0000314 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
316 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000317 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000318 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000319 // Expand FP_TO_UINT into a select.
320 // FIXME: We would like to use a Custom expander here eventually to do
321 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000323 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000324 // With SSE3 we can use fisttpll to convert to a signed i64; without
325 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000327 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000328
Chris Lattner399610a2006-12-05 18:22:22 +0000329 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000330 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000331 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
332 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000333 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000334 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000335 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000336 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000337 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000338 }
Chris Lattner21f66852005-12-23 05:15:23 +0000339
Dan Gohmanb00ee212008-02-18 19:34:53 +0000340 // Scalar integer divide and remainder are lowered to use operations that
341 // produce two results, to match the available instructions. This exposes
342 // the two-result form to trivial CSE, which is able to combine x/y and x%y
343 // into a single instruction.
344 //
345 // Scalar integer multiply-high is also lowered to use two-result
346 // operations, to match the available instructions. However, plain multiply
347 // (low) operations are left as Legal, as there are single-result
348 // instructions for this in x86. Using the two-result multiply instructions
349 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000350 for (unsigned i = 0, e = 4; i != e; ++i) {
351 MVT VT = IntVTs[i];
352 setOperationAction(ISD::MULHS, VT, Expand);
353 setOperationAction(ISD::MULHU, VT, Expand);
354 setOperationAction(ISD::SDIV, VT, Expand);
355 setOperationAction(ISD::UDIV, VT, Expand);
356 setOperationAction(ISD::SREM, VT, Expand);
357 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000358
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000359 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000360 setOperationAction(ISD::ADDC, VT, Custom);
361 setOperationAction(ISD::ADDE, VT, Custom);
362 setOperationAction(ISD::SUBC, VT, Custom);
363 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000364 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000365
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
367 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
368 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
369 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000370 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
375 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
376 setOperationAction(ISD::FREM , MVT::f32 , Expand);
377 setOperationAction(ISD::FREM , MVT::f64 , Expand);
378 setOperationAction(ISD::FREM , MVT::f80 , Expand);
379 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000380
Owen Anderson825b72b2009-08-11 20:47:22 +0000381 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
382 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000383 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
384 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
386 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000387 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000388 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
389 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000390 }
391
Benjamin Kramer1292c222010-12-04 20:32:23 +0000392 if (Subtarget->hasPOPCNT()) {
393 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
394 } else {
395 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
396 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
397 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
398 if (Subtarget->is64Bit())
399 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
400 }
401
Owen Anderson825b72b2009-08-11 20:47:22 +0000402 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
403 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000404
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000405 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000406 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000407 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000408 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000409 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
411 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
412 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
413 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
414 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000415 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
417 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
418 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
419 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000420 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000422 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000423 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000425
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000426 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
428 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
429 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
430 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000431 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000432 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
433 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000434 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000435 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
437 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
438 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
439 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000440 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000441 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000442 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
444 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
445 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000446 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000447 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
448 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
449 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000450 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000451
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000452 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000454
Eric Christopher9a9d2752010-07-22 02:48:34 +0000455 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000456 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000457
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000458 // On X86 and X86-64, atomic operations are lowered to locked instructions.
459 // Locked instructions, in turn, have implicit fence semantics (all memory
460 // operations are flushed before issuing the locked instruction, and they
461 // are not buffered), so we can fold away the common pattern of
462 // fence-atomic-fence.
463 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000464
Mon P Wang63307c32008-05-05 19:05:59 +0000465 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000466 for (unsigned i = 0, e = 4; i != e; ++i) {
467 MVT VT = IntVTs[i];
468 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
469 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000470 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000471 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000472
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000473 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000474 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
476 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
477 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
478 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
479 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
480 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
481 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000482 }
483
Eli Friedman43f51ae2011-08-26 21:21:21 +0000484 if (Subtarget->hasCmpxchg16b()) {
485 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
486 }
487
Evan Cheng3c992d22006-03-07 02:02:57 +0000488 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000489 if (!Subtarget->isTargetDarwin() &&
490 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000491 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000492 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000493 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000494
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
496 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
497 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
498 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000499 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000500 setExceptionPointerRegister(X86::RAX);
501 setExceptionSelectorRegister(X86::RDX);
502 } else {
503 setExceptionPointerRegister(X86::EAX);
504 setExceptionSelectorRegister(X86::EDX);
505 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000506 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
507 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000508
Duncan Sands4a544a72011-09-06 13:37:06 +0000509 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
510 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000511
Owen Anderson825b72b2009-08-11 20:47:22 +0000512 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000513
Nate Begemanacc398c2006-01-25 18:21:52 +0000514 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 setOperationAction(ISD::VASTART , MVT::Other, Custom);
516 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000517 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000518 setOperationAction(ISD::VAARG , MVT::Other, Custom);
519 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000520 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 setOperationAction(ISD::VAARG , MVT::Other, Expand);
522 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000523 }
Evan Chengae642192007-03-02 23:16:35 +0000524
Owen Anderson825b72b2009-08-11 20:47:22 +0000525 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
526 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000527
528 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
529 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
530 MVT::i64 : MVT::i32, Custom);
531 else if (EnableSegmentedStacks)
532 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
533 MVT::i64 : MVT::i32, Custom);
534 else
535 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
536 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000537
Evan Chengc7ce29b2009-02-13 22:36:38 +0000538 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000539 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000540 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
542 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000543
Evan Cheng223547a2006-01-31 22:28:30 +0000544 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000545 setOperationAction(ISD::FABS , MVT::f64, Custom);
546 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000547
548 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000549 setOperationAction(ISD::FNEG , MVT::f64, Custom);
550 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000551
Evan Cheng68c47cb2007-01-05 07:55:56 +0000552 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
554 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000555
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000556 // Lower this to FGETSIGNx86 plus an AND.
557 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
558 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
559
Evan Chengd25e9e82006-02-02 00:28:23 +0000560 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000561 setOperationAction(ISD::FSIN , MVT::f64, Expand);
562 setOperationAction(ISD::FCOS , MVT::f64, Expand);
563 setOperationAction(ISD::FSIN , MVT::f32, Expand);
564 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000565
Chris Lattnera54aa942006-01-29 06:26:08 +0000566 // Expand FP immediates into loads from the stack, except for the special
567 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000568 addLegalFPImmediate(APFloat(+0.0)); // xorpd
569 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000570 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000571 // Use SSE for f32, x87 for f64.
572 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
574 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000575
576 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000577 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000578
579 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000581
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000583
584 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000585 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
586 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000587
588 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000589 setOperationAction(ISD::FSIN , MVT::f32, Expand);
590 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000591
Nate Begemane1795842008-02-14 08:57:00 +0000592 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000593 addLegalFPImmediate(APFloat(+0.0f)); // xorps
594 addLegalFPImmediate(APFloat(+0.0)); // FLD0
595 addLegalFPImmediate(APFloat(+1.0)); // FLD1
596 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
597 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
598
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000599 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
601 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000603 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000604 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000605 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
607 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000608
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
610 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
611 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
612 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000613
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000614 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
616 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000617 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000618 addLegalFPImmediate(APFloat(+0.0)); // FLD0
619 addLegalFPImmediate(APFloat(+1.0)); // FLD1
620 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
621 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000622 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
623 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
624 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
625 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000626 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000627
Cameron Zwarich33390842011-07-08 21:39:21 +0000628 // We don't support FMA.
629 setOperationAction(ISD::FMA, MVT::f64, Expand);
630 setOperationAction(ISD::FMA, MVT::f32, Expand);
631
Dale Johannesen59a58732007-08-05 18:49:15 +0000632 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000633 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
635 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
636 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000637 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000638 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000639 addLegalFPImmediate(TmpFlt); // FLD0
640 TmpFlt.changeSign();
641 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000642
643 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000644 APFloat TmpFlt2(+1.0);
645 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
646 &ignored);
647 addLegalFPImmediate(TmpFlt2); // FLD1
648 TmpFlt2.changeSign();
649 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
650 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000651
Evan Chengc7ce29b2009-02-13 22:36:38 +0000652 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000653 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
654 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000655 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000656
657 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000658 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000659
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000660 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
662 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
663 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000664
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 setOperationAction(ISD::FLOG, MVT::f80, Expand);
666 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
667 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
668 setOperationAction(ISD::FEXP, MVT::f80, Expand);
669 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000670
Mon P Wangf007a8b2008-11-06 05:31:54 +0000671 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000672 // (for widening) or expand (for scalarization). Then we will selectively
673 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
675 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
676 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
677 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
678 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
679 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
680 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
681 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
682 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
683 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
684 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
685 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
686 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000691 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000692 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
693 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000715 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000716 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000725 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000726 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000730 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000731 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
732 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
733 setTruncStoreAction((MVT::SimpleValueType)VT,
734 (MVT::SimpleValueType)InnerVT, Expand);
735 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
736 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
737 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000738 }
739
Evan Chengc7ce29b2009-02-13 22:36:38 +0000740 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
741 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000742 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000743 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000744 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000745 }
746
Dale Johannesen0488fb62010-09-30 23:57:10 +0000747 // MMX-sized vectors (other than x86mmx) are expected to be expanded
748 // into smaller operations.
749 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
750 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
751 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
752 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
753 setOperationAction(ISD::AND, MVT::v8i8, Expand);
754 setOperationAction(ISD::AND, MVT::v4i16, Expand);
755 setOperationAction(ISD::AND, MVT::v2i32, Expand);
756 setOperationAction(ISD::AND, MVT::v1i64, Expand);
757 setOperationAction(ISD::OR, MVT::v8i8, Expand);
758 setOperationAction(ISD::OR, MVT::v4i16, Expand);
759 setOperationAction(ISD::OR, MVT::v2i32, Expand);
760 setOperationAction(ISD::OR, MVT::v1i64, Expand);
761 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
762 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
763 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
764 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
765 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
766 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
767 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
768 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
769 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
770 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
771 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
772 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
773 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000774 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
775 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
776 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
777 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000778
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000779 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000780 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000781
Owen Anderson825b72b2009-08-11 20:47:22 +0000782 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
783 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
784 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
785 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
786 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
787 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
788 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
789 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
790 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
791 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
792 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000793 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000794 }
795
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000796 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000797 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000798
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000799 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
800 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000801 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
802 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
803 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
804 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000805
Owen Anderson825b72b2009-08-11 20:47:22 +0000806 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
807 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
808 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
809 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
810 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
811 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
812 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
813 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
814 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
815 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
816 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
817 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
818 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
819 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
820 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
821 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000822
Duncan Sands28b77e92011-09-06 19:07:46 +0000823 setOperationAction(ISD::SETCC, MVT::v2f64, Custom);
824 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
825 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
826 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000827
Owen Anderson825b72b2009-08-11 20:47:22 +0000828 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
829 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
830 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
831 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
832 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000833
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000834 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
835 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
836 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
837 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
838 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
839
Evan Cheng2c3ae372006-04-12 21:21:57 +0000840 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000841 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
842 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000843 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000844 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000845 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000846 // Do not attempt to custom lower non-128-bit vectors
847 if (!VT.is128BitVector())
848 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000849 setOperationAction(ISD::BUILD_VECTOR,
850 VT.getSimpleVT().SimpleTy, Custom);
851 setOperationAction(ISD::VECTOR_SHUFFLE,
852 VT.getSimpleVT().SimpleTy, Custom);
853 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
854 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000855 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000856
Owen Anderson825b72b2009-08-11 20:47:22 +0000857 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
858 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
859 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
860 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
861 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
862 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000863
Nate Begemancdd1eec2008-02-12 22:51:28 +0000864 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000865 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
866 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000867 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000868
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000869 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000870 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
871 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000872 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000873
874 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000875 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000876 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000877
Owen Andersond6662ad2009-08-10 20:46:15 +0000878 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000880 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000881 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000882 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000883 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000884 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000885 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000886 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000887 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000888 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000889
Owen Anderson825b72b2009-08-11 20:47:22 +0000890 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000891
Evan Cheng2c3ae372006-04-12 21:21:57 +0000892 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
894 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
895 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
896 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000897
Owen Anderson825b72b2009-08-11 20:47:22 +0000898 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
899 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000900 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000901
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000902 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000903 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
904 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
905 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
906 setOperationAction(ISD::FRINT, MVT::f32, Legal);
907 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
908 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
909 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
910 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
911 setOperationAction(ISD::FRINT, MVT::f64, Legal);
912 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
913
Nate Begeman14d12ca2008-02-11 04:19:36 +0000914 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000915 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000916
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000917 // Can turn SHL into an integer multiply.
918 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000919 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000920
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000921 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
922 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
923 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
924 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
925 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000926
Nate Begeman14d12ca2008-02-11 04:19:36 +0000927 // i8 and i16 vectors are custom , because the source register and source
928 // source memory operand types are not the same width. f32 vectors are
929 // custom since the immediate controlling the insert encodes additional
930 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000931 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
932 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
933 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
934 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000935
Owen Anderson825b72b2009-08-11 20:47:22 +0000936 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
937 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
938 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
939 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000940
941 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000942 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
943 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000944 }
945 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000946
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000947 if (Subtarget->hasSSE2() || Subtarget->hasAVX()) {
Nadav Rotem43012222011-05-11 08:12:09 +0000948 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
949 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
950 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000951 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000952
953 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
954 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
955 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
956
957 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
958 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
959 }
960
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000961 if (Subtarget->hasSSE42() || Subtarget->hasAVX())
Duncan Sands28b77e92011-09-06 19:07:46 +0000962 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000963
David Greene9b9838d2009-06-29 16:47:10 +0000964 if (!UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000965 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
966 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
967 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
968 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
969 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
970 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000971
Owen Anderson825b72b2009-08-11 20:47:22 +0000972 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000973 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
974 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000975
Owen Anderson825b72b2009-08-11 20:47:22 +0000976 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
977 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
978 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
979 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
980 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
981 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000982
Owen Anderson825b72b2009-08-11 20:47:22 +0000983 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
984 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
985 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
986 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
987 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
988 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000989
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000990 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
991 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +0000992 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000993
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +0000994 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
995 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
996 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
997 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
998 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
999 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
1000
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001001 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1002 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1003 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1004 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1005
1006 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1007 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1008 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1009 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1010
1011 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
1012 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
1013
Duncan Sands28b77e92011-09-06 19:07:46 +00001014 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1015 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1016 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1017 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001018
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001019 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1020 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1021 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1022
Nadav Rotemfbad25e2011-09-11 15:02:23 +00001023 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1024 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1025 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1026 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001027
Craig Topper13894fa2011-08-24 06:14:18 +00001028 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1029 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1030 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1031 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1032
1033 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1034 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1035 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1036 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1037
1038 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1039 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1040 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1041 // Don't lower v32i8 because there is no 128-bit byte mul
1042
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001043 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001044 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001045 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1046 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1047 EVT VT = SVT;
1048
1049 // Extract subvector is special because the value type
1050 // (result) is 128-bit but the source is 256-bit wide.
1051 if (VT.is128BitVector())
1052 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1053
1054 // Do not attempt to custom lower other non-256-bit vectors
1055 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001056 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001057
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001058 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1059 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1060 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1061 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001062 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001063 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001064 }
1065
David Greene54d8eba2011-01-27 22:38:56 +00001066 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001067 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1068 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1069 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001070
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001071 // Do not attempt to promote non-256-bit vectors
1072 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001073 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001074
1075 setOperationAction(ISD::AND, SVT, Promote);
1076 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1077 setOperationAction(ISD::OR, SVT, Promote);
1078 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1079 setOperationAction(ISD::XOR, SVT, Promote);
1080 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1081 setOperationAction(ISD::LOAD, SVT, Promote);
1082 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1083 setOperationAction(ISD::SELECT, SVT, Promote);
1084 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001085 }
David Greene9b9838d2009-06-29 16:47:10 +00001086 }
1087
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001088 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1089 // of this type with custom code.
1090 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1091 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1092 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1093 }
1094
Evan Cheng6be2c582006-04-05 23:38:46 +00001095 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001096 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001097
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001098
Eli Friedman962f5492010-06-02 19:35:46 +00001099 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1100 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001101 //
Eli Friedman962f5492010-06-02 19:35:46 +00001102 // FIXME: We really should do custom legalization for addition and
1103 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1104 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001105 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1106 // Add/Sub/Mul with overflow operations are custom lowered.
1107 MVT VT = IntVTs[i];
1108 setOperationAction(ISD::SADDO, VT, Custom);
1109 setOperationAction(ISD::UADDO, VT, Custom);
1110 setOperationAction(ISD::SSUBO, VT, Custom);
1111 setOperationAction(ISD::USUBO, VT, Custom);
1112 setOperationAction(ISD::SMULO, VT, Custom);
1113 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001114 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001115
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001116 // There are no 8-bit 3-address imul/mul instructions
1117 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1118 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001119
Evan Chengd54f2d52009-03-31 19:38:51 +00001120 if (!Subtarget->is64Bit()) {
1121 // These libcalls are not available in 32-bit.
1122 setLibcallName(RTLIB::SHL_I128, 0);
1123 setLibcallName(RTLIB::SRL_I128, 0);
1124 setLibcallName(RTLIB::SRA_I128, 0);
1125 }
1126
Evan Cheng206ee9d2006-07-07 08:33:52 +00001127 // We have target-specific dag combine patterns for the following nodes:
1128 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001129 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001130 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001131 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001132 setTargetDAGCombine(ISD::SHL);
1133 setTargetDAGCombine(ISD::SRA);
1134 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001135 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001136 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001137 setTargetDAGCombine(ISD::ADD);
1138 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001139 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001140 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001141 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001142 if (Subtarget->is64Bit())
1143 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001144
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001145 computeRegisterProperties();
1146
Evan Cheng05219282011-01-06 06:52:41 +00001147 // On Darwin, -Os means optimize for size without hurting performance,
1148 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001149 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001150 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001151 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001152 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1153 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1154 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001155 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001156 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001157
1158 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001159}
1160
Scott Michel5b8f82e2008-03-10 15:42:14 +00001161
Duncan Sands28b77e92011-09-06 19:07:46 +00001162EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1163 if (!VT.isVector()) return MVT::i8;
1164 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001165}
1166
1167
Evan Cheng29286502008-01-23 23:17:41 +00001168/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1169/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001170static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001171 if (MaxAlign == 16)
1172 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001173 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001174 if (VTy->getBitWidth() == 128)
1175 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001176 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001177 unsigned EltAlign = 0;
1178 getMaxByValAlign(ATy->getElementType(), EltAlign);
1179 if (EltAlign > MaxAlign)
1180 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001181 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001182 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1183 unsigned EltAlign = 0;
1184 getMaxByValAlign(STy->getElementType(i), EltAlign);
1185 if (EltAlign > MaxAlign)
1186 MaxAlign = EltAlign;
1187 if (MaxAlign == 16)
1188 break;
1189 }
1190 }
1191 return;
1192}
1193
1194/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1195/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001196/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1197/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001198unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001199 if (Subtarget->is64Bit()) {
1200 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001201 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001202 if (TyAlign > 8)
1203 return TyAlign;
1204 return 8;
1205 }
1206
Evan Cheng29286502008-01-23 23:17:41 +00001207 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001208 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001209 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001210 return Align;
1211}
Chris Lattner2b02a442007-02-25 08:29:00 +00001212
Evan Chengf0df0312008-05-15 08:39:06 +00001213/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001214/// and store operations as a result of memset, memcpy, and memmove
1215/// lowering. If DstAlign is zero that means it's safe to destination
1216/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1217/// means there isn't a need to check it against alignment requirement,
1218/// probably because the source does not need to be loaded. If
1219/// 'NonScalarIntSafe' is true, that means it's safe to return a
1220/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1221/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1222/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001223/// It returns EVT::Other if the type should be determined using generic
1224/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001225EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001226X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1227 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001228 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001229 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001230 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001231 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1232 // linux. This is because the stack realignment code can't handle certain
1233 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001234 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001235 if (NonScalarIntSafe &&
1236 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001237 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001238 (Subtarget->isUnalignedMemAccessFast() ||
1239 ((DstAlign == 0 || DstAlign >= 16) &&
1240 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001241 Subtarget->getStackAlignment() >= 16) {
1242 if (Subtarget->hasSSE2())
1243 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001244 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001245 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001246 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001247 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001248 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001249 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001250 // Do not use f64 to lower memcpy if source is string constant. It's
1251 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001252 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001253 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001254 }
Evan Chengf0df0312008-05-15 08:39:06 +00001255 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001256 return MVT::i64;
1257 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001258}
1259
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001260/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1261/// current function. The returned value is a member of the
1262/// MachineJumpTableInfo::JTEntryKind enum.
1263unsigned X86TargetLowering::getJumpTableEncoding() const {
1264 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1265 // symbol.
1266 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1267 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001268 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001269
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001270 // Otherwise, use the normal jump table encoding heuristics.
1271 return TargetLowering::getJumpTableEncoding();
1272}
1273
Chris Lattnerc64daab2010-01-26 05:02:42 +00001274const MCExpr *
1275X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1276 const MachineBasicBlock *MBB,
1277 unsigned uid,MCContext &Ctx) const{
1278 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1279 Subtarget->isPICStyleGOT());
1280 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1281 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001282 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1283 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001284}
1285
Evan Chengcc415862007-11-09 01:32:10 +00001286/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1287/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001288SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001289 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001290 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001291 // This doesn't have DebugLoc associated with it, but is not really the
1292 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001293 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001294 return Table;
1295}
1296
Chris Lattner589c6f62010-01-26 06:28:43 +00001297/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1298/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1299/// MCExpr.
1300const MCExpr *X86TargetLowering::
1301getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1302 MCContext &Ctx) const {
1303 // X86-64 uses RIP relative addressing based on the jump table label.
1304 if (Subtarget->isPICStyleRIPRel())
1305 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1306
1307 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001308 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001309}
1310
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001311// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001312std::pair<const TargetRegisterClass*, uint8_t>
1313X86TargetLowering::findRepresentativeClass(EVT VT) const{
1314 const TargetRegisterClass *RRC = 0;
1315 uint8_t Cost = 1;
1316 switch (VT.getSimpleVT().SimpleTy) {
1317 default:
1318 return TargetLowering::findRepresentativeClass(VT);
1319 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1320 RRC = (Subtarget->is64Bit()
1321 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1322 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001323 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001324 RRC = X86::VR64RegisterClass;
1325 break;
1326 case MVT::f32: case MVT::f64:
1327 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1328 case MVT::v4f32: case MVT::v2f64:
1329 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1330 case MVT::v4f64:
1331 RRC = X86::VR128RegisterClass;
1332 break;
1333 }
1334 return std::make_pair(RRC, Cost);
1335}
1336
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001337bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1338 unsigned &Offset) const {
1339 if (!Subtarget->isTargetLinux())
1340 return false;
1341
1342 if (Subtarget->is64Bit()) {
1343 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1344 Offset = 0x28;
1345 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1346 AddressSpace = 256;
1347 else
1348 AddressSpace = 257;
1349 } else {
1350 // %gs:0x14 on i386
1351 Offset = 0x14;
1352 AddressSpace = 256;
1353 }
1354 return true;
1355}
1356
1357
Chris Lattner2b02a442007-02-25 08:29:00 +00001358//===----------------------------------------------------------------------===//
1359// Return Value Calling Convention Implementation
1360//===----------------------------------------------------------------------===//
1361
Chris Lattner59ed56b2007-02-28 04:55:35 +00001362#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001363
Michael J. Spencerec38de22010-10-10 22:04:20 +00001364bool
Eric Christopher471e4222011-06-08 23:55:35 +00001365X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1366 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001367 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001368 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001369 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001370 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001371 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001372 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001373}
1374
Dan Gohman98ca4f22009-08-05 01:29:28 +00001375SDValue
1376X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001377 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001378 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001379 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001380 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001381 MachineFunction &MF = DAG.getMachineFunction();
1382 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001383
Chris Lattner9774c912007-02-27 05:28:59 +00001384 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001385 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001386 RVLocs, *DAG.getContext());
1387 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001388
Evan Chengdcea1632010-02-04 02:40:39 +00001389 // Add the regs to the liveout set for the function.
1390 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1391 for (unsigned i = 0; i != RVLocs.size(); ++i)
1392 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1393 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001394
Dan Gohman475871a2008-07-27 21:46:04 +00001395 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001396
Dan Gohman475871a2008-07-27 21:46:04 +00001397 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001398 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1399 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001400 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1401 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001402
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001403 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001404 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1405 CCValAssign &VA = RVLocs[i];
1406 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001407 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001408 EVT ValVT = ValToCopy.getValueType();
1409
Dale Johannesenc4510512010-09-24 19:05:48 +00001410 // If this is x86-64, and we disabled SSE, we can't return FP values,
1411 // or SSE or MMX vectors.
1412 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1413 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001414 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001415 report_fatal_error("SSE register return with SSE disabled");
1416 }
1417 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1418 // llvm-gcc has never done it right and no one has noticed, so this
1419 // should be OK for now.
1420 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001421 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001422 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001423
Chris Lattner447ff682008-03-11 03:23:40 +00001424 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1425 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001426 if (VA.getLocReg() == X86::ST0 ||
1427 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001428 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1429 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001430 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001431 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001432 RetOps.push_back(ValToCopy);
1433 // Don't emit a copytoreg.
1434 continue;
1435 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001436
Evan Cheng242b38b2009-02-23 09:03:22 +00001437 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1438 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001439 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001440 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001441 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001442 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001443 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1444 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001445 // If we don't have SSE2 available, convert to v4f32 so the generated
1446 // register is legal.
1447 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001448 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001449 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001450 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001451 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001452
Dale Johannesendd64c412009-02-04 00:33:20 +00001453 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001454 Flag = Chain.getValue(1);
1455 }
Dan Gohman61a92132008-04-21 23:59:07 +00001456
1457 // The x86-64 ABI for returning structs by value requires that we copy
1458 // the sret argument into %rax for the return. We saved the argument into
1459 // a virtual register in the entry block, so now we copy the value out
1460 // and into %rax.
1461 if (Subtarget->is64Bit() &&
1462 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1463 MachineFunction &MF = DAG.getMachineFunction();
1464 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1465 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001466 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001467 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001468 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001469
Dale Johannesendd64c412009-02-04 00:33:20 +00001470 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001471 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001472
1473 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001474 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001475 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001476
Chris Lattner447ff682008-03-11 03:23:40 +00001477 RetOps[0] = Chain; // Update chain.
1478
1479 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001480 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001481 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001482
1483 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001484 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001485}
1486
Evan Cheng3d2125c2010-11-30 23:55:39 +00001487bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1488 if (N->getNumValues() != 1)
1489 return false;
1490 if (!N->hasNUsesOfValue(1, 0))
1491 return false;
1492
1493 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001494 if (Copy->getOpcode() != ISD::CopyToReg &&
1495 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001496 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001497
1498 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001499 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001500 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001501 if (UI->getOpcode() != X86ISD::RET_FLAG)
1502 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001503 HasRet = true;
1504 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001505
Evan Cheng1bf891a2010-12-01 22:59:46 +00001506 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001507}
1508
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001509EVT
1510X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001511 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001512 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001513 // TODO: Is this also valid on 32-bit?
1514 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001515 ReturnMVT = MVT::i8;
1516 else
1517 ReturnMVT = MVT::i32;
1518
1519 EVT MinVT = getRegisterType(Context, ReturnMVT);
1520 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001521}
1522
Dan Gohman98ca4f22009-08-05 01:29:28 +00001523/// LowerCallResult - Lower the result values of a call into the
1524/// appropriate copies out of appropriate physical registers.
1525///
1526SDValue
1527X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001528 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001529 const SmallVectorImpl<ISD::InputArg> &Ins,
1530 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001531 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001532
Chris Lattnere32bbf62007-02-28 07:09:55 +00001533 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001534 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001535 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001536 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1537 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001538 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001539
Chris Lattner3085e152007-02-25 08:59:22 +00001540 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001541 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001542 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001543 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001544
Torok Edwin3f142c32009-02-01 18:15:56 +00001545 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001546 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001547 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001548 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001549 }
1550
Evan Cheng79fb3b42009-02-20 20:43:02 +00001551 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001552
1553 // If this is a call to a function that returns an fp value on the floating
1554 // point stack, we must guarantee the the value is popped from the stack, so
1555 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001556 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001557 // instead.
1558 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1559 // If we prefer to use the value in xmm registers, copy it out as f80 and
1560 // use a truncate to move it from fp stack reg to xmm reg.
1561 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001562 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001563 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1564 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001565 Val = Chain.getValue(0);
1566
1567 // Round the f80 to the right size, which also moves it to the appropriate
1568 // xmm register.
1569 if (CopyVT != VA.getValVT())
1570 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1571 // This truncation won't change the value.
1572 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001573 } else {
1574 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1575 CopyVT, InFlag).getValue(1);
1576 Val = Chain.getValue(0);
1577 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001578 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001579 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001580 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001581
Dan Gohman98ca4f22009-08-05 01:29:28 +00001582 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001583}
1584
1585
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001586//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001587// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001588//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001589// StdCall calling convention seems to be standard for many Windows' API
1590// routines and around. It differs from C calling convention just a little:
1591// callee should clean up the stack, not caller. Symbols should be also
1592// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001593// For info on fast calling convention see Fast Calling Convention (tail call)
1594// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001595
Dan Gohman98ca4f22009-08-05 01:29:28 +00001596/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001597/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001598static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1599 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001600 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001601
Dan Gohman98ca4f22009-08-05 01:29:28 +00001602 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001603}
1604
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001605/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001606/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001607static bool
1608ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1609 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001610 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001611
Dan Gohman98ca4f22009-08-05 01:29:28 +00001612 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001613}
1614
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001615/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1616/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001617/// the specific parameter attribute. The copy will be passed as a byval
1618/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001619static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001620CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001621 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1622 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001623 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001624
Dale Johannesendd64c412009-02-04 00:33:20 +00001625 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001626 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001627 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001628}
1629
Chris Lattner29689432010-03-11 00:22:57 +00001630/// IsTailCallConvention - Return true if the calling convention is one that
1631/// supports tail call optimization.
1632static bool IsTailCallConvention(CallingConv::ID CC) {
1633 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1634}
1635
Evan Cheng485fafc2011-03-21 01:19:09 +00001636bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1637 if (!CI->isTailCall())
1638 return false;
1639
1640 CallSite CS(CI);
1641 CallingConv::ID CalleeCC = CS.getCallingConv();
1642 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1643 return false;
1644
1645 return true;
1646}
1647
Evan Cheng0c439eb2010-01-27 00:07:07 +00001648/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1649/// a tailcall target by changing its ABI.
1650static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001651 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001652}
1653
Dan Gohman98ca4f22009-08-05 01:29:28 +00001654SDValue
1655X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001656 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001657 const SmallVectorImpl<ISD::InputArg> &Ins,
1658 DebugLoc dl, SelectionDAG &DAG,
1659 const CCValAssign &VA,
1660 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001661 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001662 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001663 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001664 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001665 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001666 EVT ValVT;
1667
1668 // If value is passed by pointer we have address passed instead of the value
1669 // itself.
1670 if (VA.getLocInfo() == CCValAssign::Indirect)
1671 ValVT = VA.getLocVT();
1672 else
1673 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001674
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001675 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001676 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001677 // In case of tail call optimization mark all arguments mutable. Since they
1678 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001679 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001680 unsigned Bytes = Flags.getByValSize();
1681 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1682 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001683 return DAG.getFrameIndex(FI, getPointerTy());
1684 } else {
1685 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001686 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001687 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1688 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001689 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001690 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001691 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001692}
1693
Dan Gohman475871a2008-07-27 21:46:04 +00001694SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001695X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001696 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001697 bool isVarArg,
1698 const SmallVectorImpl<ISD::InputArg> &Ins,
1699 DebugLoc dl,
1700 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001701 SmallVectorImpl<SDValue> &InVals)
1702 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001703 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001704 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001705
Gordon Henriksen86737662008-01-05 16:56:59 +00001706 const Function* Fn = MF.getFunction();
1707 if (Fn->hasExternalLinkage() &&
1708 Subtarget->isTargetCygMing() &&
1709 Fn->getName() == "main")
1710 FuncInfo->setForceFramePointer(true);
1711
Evan Cheng1bc78042006-04-26 01:20:17 +00001712 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001713 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001714 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001715
Chris Lattner29689432010-03-11 00:22:57 +00001716 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1717 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001718
Chris Lattner638402b2007-02-28 07:00:42 +00001719 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001720 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001721 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001722 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001723
1724 // Allocate shadow area for Win64
1725 if (IsWin64) {
1726 CCInfo.AllocateStack(32, 8);
1727 }
1728
Duncan Sands45907662010-10-31 13:21:44 +00001729 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001730
Chris Lattnerf39f7712007-02-28 05:46:49 +00001731 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001732 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001733 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1734 CCValAssign &VA = ArgLocs[i];
1735 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1736 // places.
1737 assert(VA.getValNo() != LastVal &&
1738 "Don't support value assigned to multiple locs yet");
1739 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001740
Chris Lattnerf39f7712007-02-28 05:46:49 +00001741 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001742 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001743 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001744 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001745 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001746 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001747 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001748 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001749 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001750 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001751 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001752 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1753 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001754 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001755 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001756 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001757 RC = X86::VR64RegisterClass;
1758 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001759 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001760
Devang Patel68e6bee2011-02-21 23:21:26 +00001761 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001762 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001763
Chris Lattnerf39f7712007-02-28 05:46:49 +00001764 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1765 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1766 // right size.
1767 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001768 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001769 DAG.getValueType(VA.getValVT()));
1770 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001771 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001772 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001773 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001774 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001775
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001776 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001777 // Handle MMX values passed in XMM regs.
1778 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001779 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1780 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001781 } else
1782 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001783 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001784 } else {
1785 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001786 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001787 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001788
1789 // If value is passed via pointer - do a load.
1790 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001791 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1792 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001793
Dan Gohman98ca4f22009-08-05 01:29:28 +00001794 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001795 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001796
Dan Gohman61a92132008-04-21 23:59:07 +00001797 // The x86-64 ABI for returning structs by value requires that we copy
1798 // the sret argument into %rax for the return. Save the argument into
1799 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001800 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001801 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1802 unsigned Reg = FuncInfo->getSRetReturnReg();
1803 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001804 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001805 FuncInfo->setSRetReturnReg(Reg);
1806 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001807 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001808 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001809 }
1810
Chris Lattnerf39f7712007-02-28 05:46:49 +00001811 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001812 // Align stack specially for tail calls.
1813 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001814 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001815
Evan Cheng1bc78042006-04-26 01:20:17 +00001816 // If the function takes variable number of arguments, make a frame index for
1817 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001818 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001819 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1820 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001821 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001822 }
1823 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001824 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1825
1826 // FIXME: We should really autogenerate these arrays
1827 static const unsigned GPR64ArgRegsWin64[] = {
1828 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001829 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001830 static const unsigned GPR64ArgRegs64Bit[] = {
1831 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1832 };
1833 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001834 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1835 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1836 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001837 const unsigned *GPR64ArgRegs;
1838 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001839
1840 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001841 // The XMM registers which might contain var arg parameters are shadowed
1842 // in their paired GPR. So we only need to save the GPR to their home
1843 // slots.
1844 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001845 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001846 } else {
1847 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1848 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001849
1850 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001851 }
1852 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1853 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001854
Devang Patel578efa92009-06-05 21:57:13 +00001855 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001856 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001857 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001858 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001859 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001860 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001861 // Kernel mode asks for SSE to be disabled, so don't push them
1862 // on the stack.
1863 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001864
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001865 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001866 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001867 // Get to the caller-allocated home save location. Add 8 to account
1868 // for the return address.
1869 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001870 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001871 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001872 // Fixup to set vararg frame on shadow area (4 x i64).
1873 if (NumIntRegs < 4)
1874 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001875 } else {
1876 // For X86-64, if there are vararg parameters that are passed via
1877 // registers, then we must store them to their spots on the stack so they
1878 // may be loaded by deferencing the result of va_next.
1879 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1880 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1881 FuncInfo->setRegSaveFrameIndex(
1882 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001883 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001884 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001885
Gordon Henriksen86737662008-01-05 16:56:59 +00001886 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001887 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001888 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1889 getPointerTy());
1890 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001891 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001892 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1893 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001894 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001895 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001896 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001897 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001898 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001899 MachinePointerInfo::getFixedStack(
1900 FuncInfo->getRegSaveFrameIndex(), Offset),
1901 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001902 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001903 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001904 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001905
Dan Gohmanface41a2009-08-16 21:24:25 +00001906 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1907 // Now store the XMM (fp + vector) parameter registers.
1908 SmallVector<SDValue, 11> SaveXMMOps;
1909 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001910
Devang Patel68e6bee2011-02-21 23:21:26 +00001911 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001912 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1913 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001914
Dan Gohman1e93df62010-04-17 14:41:14 +00001915 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1916 FuncInfo->getRegSaveFrameIndex()));
1917 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1918 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001919
Dan Gohmanface41a2009-08-16 21:24:25 +00001920 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001921 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001922 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001923 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1924 SaveXMMOps.push_back(Val);
1925 }
1926 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1927 MVT::Other,
1928 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001929 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001930
1931 if (!MemOps.empty())
1932 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1933 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001934 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001935 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001936
Gordon Henriksen86737662008-01-05 16:56:59 +00001937 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001938 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001939 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001940 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001941 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001942 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001943 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001944 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001945 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001946
Gordon Henriksen86737662008-01-05 16:56:59 +00001947 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001948 // RegSaveFrameIndex is X86-64 only.
1949 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001950 if (CallConv == CallingConv::X86_FastCall ||
1951 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001952 // fastcc functions can't have varargs.
1953 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001954 }
Evan Cheng25caf632006-05-23 21:06:34 +00001955
Rafael Espindola76927d752011-08-30 19:39:58 +00001956 FuncInfo->setArgumentStackSize(StackSize);
1957
Dan Gohman98ca4f22009-08-05 01:29:28 +00001958 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001959}
1960
Dan Gohman475871a2008-07-27 21:46:04 +00001961SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001962X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1963 SDValue StackPtr, SDValue Arg,
1964 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001965 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001966 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001967 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001968 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001969 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001970 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001971 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001972
1973 return DAG.getStore(Chain, dl, Arg, PtrOff,
1974 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001975 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001976}
1977
Bill Wendling64e87322009-01-16 19:25:27 +00001978/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001979/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001980SDValue
1981X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001982 SDValue &OutRetAddr, SDValue Chain,
1983 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001984 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001985 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001986 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001987 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001988
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001989 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001990 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1991 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001992 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001993}
1994
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001995/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001996/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001997static SDValue
1998EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001999 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002000 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002001 // Store the return address to the appropriate stack slot.
2002 if (!FPDiff) return Chain;
2003 // Calculate the new stack slot for the return address.
2004 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002005 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002006 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002007 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002008 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002009 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002010 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002011 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002012 return Chain;
2013}
2014
Dan Gohman98ca4f22009-08-05 01:29:28 +00002015SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002016X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002017 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002018 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002019 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002020 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002021 const SmallVectorImpl<ISD::InputArg> &Ins,
2022 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002023 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002024 MachineFunction &MF = DAG.getMachineFunction();
2025 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002026 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002027 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002028 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002029
Evan Cheng5f941932010-02-05 02:21:12 +00002030 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002031 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002032 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2033 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002034 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002035
2036 // Sibcalls are automatically detected tailcalls which do not require
2037 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002038 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002039 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002040
2041 if (isTailCall)
2042 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002043 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002044
Chris Lattner29689432010-03-11 00:22:57 +00002045 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2046 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002047
Chris Lattner638402b2007-02-28 07:00:42 +00002048 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002049 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002050 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002051 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002052
2053 // Allocate shadow area for Win64
2054 if (IsWin64) {
2055 CCInfo.AllocateStack(32, 8);
2056 }
2057
Duncan Sands45907662010-10-31 13:21:44 +00002058 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002059
Chris Lattner423c5f42007-02-28 05:31:48 +00002060 // Get a count of how many bytes are to be pushed on the stack.
2061 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002062 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002063 // This is a sibcall. The memory operands are available in caller's
2064 // own caller's stack.
2065 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002066 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002067 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002068
Gordon Henriksen86737662008-01-05 16:56:59 +00002069 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002070 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002071 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002072 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002073 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2074 FPDiff = NumBytesCallerPushed - NumBytes;
2075
2076 // Set the delta of movement of the returnaddr stackslot.
2077 // But only set if delta is greater than previous delta.
2078 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2079 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2080 }
2081
Evan Chengf22f9b32010-02-06 03:28:46 +00002082 if (!IsSibcall)
2083 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002084
Dan Gohman475871a2008-07-27 21:46:04 +00002085 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002086 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002087 if (isTailCall && FPDiff)
2088 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2089 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002090
Dan Gohman475871a2008-07-27 21:46:04 +00002091 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2092 SmallVector<SDValue, 8> MemOpChains;
2093 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002094
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002095 // Walk the register/memloc assignments, inserting copies/loads. In the case
2096 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002097 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2098 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002099 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002100 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002101 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002102 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002103
Chris Lattner423c5f42007-02-28 05:31:48 +00002104 // Promote the value if needed.
2105 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002106 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002107 case CCValAssign::Full: break;
2108 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002109 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002110 break;
2111 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002112 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002113 break;
2114 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002115 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2116 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002117 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002118 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2119 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002120 } else
2121 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2122 break;
2123 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002124 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002125 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002126 case CCValAssign::Indirect: {
2127 // Store the argument.
2128 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002129 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002130 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002131 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002132 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002133 Arg = SpillSlot;
2134 break;
2135 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002136 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002137
Chris Lattner423c5f42007-02-28 05:31:48 +00002138 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002139 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2140 if (isVarArg && IsWin64) {
2141 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2142 // shadow reg if callee is a varargs function.
2143 unsigned ShadowReg = 0;
2144 switch (VA.getLocReg()) {
2145 case X86::XMM0: ShadowReg = X86::RCX; break;
2146 case X86::XMM1: ShadowReg = X86::RDX; break;
2147 case X86::XMM2: ShadowReg = X86::R8; break;
2148 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002149 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002150 if (ShadowReg)
2151 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002152 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002153 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002154 assert(VA.isMemLoc());
2155 if (StackPtr.getNode() == 0)
2156 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2157 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2158 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002159 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002160 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002161
Evan Cheng32fe1032006-05-25 00:59:30 +00002162 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002163 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002164 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002165
Evan Cheng347d5f72006-04-28 21:29:37 +00002166 // Build a sequence of copy-to-reg nodes chained together with token chain
2167 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002168 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002169 // Tail call byval lowering might overwrite argument registers so in case of
2170 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002171 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002172 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002173 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002174 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002175 InFlag = Chain.getValue(1);
2176 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002177
Chris Lattner88e1fd52009-07-09 04:24:46 +00002178 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002179 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2180 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002181 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002182 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2183 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002184 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002185 InFlag);
2186 InFlag = Chain.getValue(1);
2187 } else {
2188 // If we are tail calling and generating PIC/GOT style code load the
2189 // address of the callee into ECX. The value in ecx is used as target of
2190 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2191 // for tail calls on PIC/GOT architectures. Normally we would just put the
2192 // address of GOT into ebx and then call target@PLT. But for tail calls
2193 // ebx would be restored (since ebx is callee saved) before jumping to the
2194 // target@PLT.
2195
2196 // Note: The actual moving to ECX is done further down.
2197 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2198 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2199 !G->getGlobal()->hasProtectedVisibility())
2200 Callee = LowerGlobalAddress(Callee, DAG);
2201 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002202 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002203 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002204 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002205
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002206 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002207 // From AMD64 ABI document:
2208 // For calls that may call functions that use varargs or stdargs
2209 // (prototype-less calls or calls to functions containing ellipsis (...) in
2210 // the declaration) %al is used as hidden argument to specify the number
2211 // of SSE registers used. The contents of %al do not need to match exactly
2212 // the number of registers, but must be an ubound on the number of SSE
2213 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002214
Gordon Henriksen86737662008-01-05 16:56:59 +00002215 // Count the number of XMM registers allocated.
2216 static const unsigned XMMArgRegs[] = {
2217 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2218 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2219 };
2220 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002221 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002222 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002223
Dale Johannesendd64c412009-02-04 00:33:20 +00002224 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002225 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002226 InFlag = Chain.getValue(1);
2227 }
2228
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002229
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002230 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002231 if (isTailCall) {
2232 // Force all the incoming stack arguments to be loaded from the stack
2233 // before any new outgoing arguments are stored to the stack, because the
2234 // outgoing stack slots may alias the incoming argument stack slots, and
2235 // the alias isn't otherwise explicit. This is slightly more conservative
2236 // than necessary, because it means that each store effectively depends
2237 // on every argument instead of just those arguments it would clobber.
2238 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2239
Dan Gohman475871a2008-07-27 21:46:04 +00002240 SmallVector<SDValue, 8> MemOpChains2;
2241 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002242 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002243 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002244 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002245 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002246 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2247 CCValAssign &VA = ArgLocs[i];
2248 if (VA.isRegLoc())
2249 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002250 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002251 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002252 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002253 // Create frame index.
2254 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002255 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002256 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002257 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002258
Duncan Sands276dcbd2008-03-21 09:14:45 +00002259 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002260 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002261 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002262 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002263 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002264 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002265 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002266
Dan Gohman98ca4f22009-08-05 01:29:28 +00002267 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2268 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002269 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002270 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002271 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002272 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002273 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002274 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002275 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002276 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002277 }
2278 }
2279
2280 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002281 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002282 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002283
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002284 // Copy arguments to their registers.
2285 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002286 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002287 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002288 InFlag = Chain.getValue(1);
2289 }
Dan Gohman475871a2008-07-27 21:46:04 +00002290 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002291
Gordon Henriksen86737662008-01-05 16:56:59 +00002292 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002293 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002294 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002295 }
2296
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002297 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2298 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2299 // In the 64-bit large code model, we have to make all calls
2300 // through a register, since the call instruction's 32-bit
2301 // pc-relative offset may not be large enough to hold the whole
2302 // address.
2303 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002304 // If the callee is a GlobalAddress node (quite common, every direct call
2305 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2306 // it.
2307
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002308 // We should use extra load for direct calls to dllimported functions in
2309 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002310 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002311 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002312 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002313 bool ExtraLoad = false;
2314 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002315
Chris Lattner48a7d022009-07-09 05:02:21 +00002316 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2317 // external symbols most go through the PLT in PIC mode. If the symbol
2318 // has hidden or protected visibility, or if it is static or local, then
2319 // we don't need to use the PLT - we can directly call it.
2320 if (Subtarget->isTargetELF() &&
2321 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002322 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002323 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002324 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002325 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002326 (!Subtarget->getTargetTriple().isMacOSX() ||
2327 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002328 // PC-relative references to external symbols should go through $stub,
2329 // unless we're building with the leopard linker or later, which
2330 // automatically synthesizes these stubs.
2331 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002332 } else if (Subtarget->isPICStyleRIPRel() &&
2333 isa<Function>(GV) &&
2334 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2335 // If the function is marked as non-lazy, generate an indirect call
2336 // which loads from the GOT directly. This avoids runtime overhead
2337 // at the cost of eager binding (and one extra byte of encoding).
2338 OpFlags = X86II::MO_GOTPCREL;
2339 WrapperKind = X86ISD::WrapperRIP;
2340 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002341 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002342
Devang Patel0d881da2010-07-06 22:08:15 +00002343 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002344 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002345
2346 // Add a wrapper if needed.
2347 if (WrapperKind != ISD::DELETED_NODE)
2348 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2349 // Add extra indirection if needed.
2350 if (ExtraLoad)
2351 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2352 MachinePointerInfo::getGOT(),
2353 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002354 }
Bill Wendling056292f2008-09-16 21:48:12 +00002355 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002356 unsigned char OpFlags = 0;
2357
Evan Cheng1bf891a2010-12-01 22:59:46 +00002358 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2359 // external symbols should go through the PLT.
2360 if (Subtarget->isTargetELF() &&
2361 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2362 OpFlags = X86II::MO_PLT;
2363 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002364 (!Subtarget->getTargetTriple().isMacOSX() ||
2365 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002366 // PC-relative references to external symbols should go through $stub,
2367 // unless we're building with the leopard linker or later, which
2368 // automatically synthesizes these stubs.
2369 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002370 }
Eric Christopherfd179292009-08-27 18:07:15 +00002371
Chris Lattner48a7d022009-07-09 05:02:21 +00002372 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2373 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002374 }
2375
Chris Lattnerd96d0722007-02-25 06:40:16 +00002376 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002377 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002378 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002379
Evan Chengf22f9b32010-02-06 03:28:46 +00002380 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002381 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2382 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002383 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002384 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002385
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002386 Ops.push_back(Chain);
2387 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002388
Dan Gohman98ca4f22009-08-05 01:29:28 +00002389 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002390 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002391
Gordon Henriksen86737662008-01-05 16:56:59 +00002392 // Add argument registers to the end of the list so that they are known live
2393 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002394 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2395 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2396 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002397
Evan Cheng586ccac2008-03-18 23:36:35 +00002398 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002399 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002400 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2401
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002402 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002403 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002404 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002405
Gabor Greifba36cb52008-08-28 21:40:38 +00002406 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002407 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002408
Dan Gohman98ca4f22009-08-05 01:29:28 +00002409 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002410 // We used to do:
2411 //// If this is the first return lowered for this function, add the regs
2412 //// to the liveout set for the function.
2413 // This isn't right, although it's probably harmless on x86; liveouts
2414 // should be computed from returns not tail calls. Consider a void
2415 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002416 return DAG.getNode(X86ISD::TC_RETURN, dl,
2417 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002418 }
2419
Dale Johannesenace16102009-02-03 19:33:06 +00002420 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002421 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002422
Chris Lattner2d297092006-05-23 18:50:38 +00002423 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002424 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002425 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002426 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002427 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002428 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002429 // pops the hidden struct pointer, so we have to push it back.
2430 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002431 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002432 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002433 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002434
Gordon Henriksenae636f82008-01-03 16:47:34 +00002435 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002436 if (!IsSibcall) {
2437 Chain = DAG.getCALLSEQ_END(Chain,
2438 DAG.getIntPtrConstant(NumBytes, true),
2439 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2440 true),
2441 InFlag);
2442 InFlag = Chain.getValue(1);
2443 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002444
Chris Lattner3085e152007-02-25 08:59:22 +00002445 // Handle result values, copying them out of physregs into vregs that we
2446 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002447 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2448 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002449}
2450
Evan Cheng25ab6902006-09-08 06:48:29 +00002451
2452//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002453// Fast Calling Convention (tail call) implementation
2454//===----------------------------------------------------------------------===//
2455
2456// Like std call, callee cleans arguments, convention except that ECX is
2457// reserved for storing the tail called function address. Only 2 registers are
2458// free for argument passing (inreg). Tail call optimization is performed
2459// provided:
2460// * tailcallopt is enabled
2461// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002462// On X86_64 architecture with GOT-style position independent code only local
2463// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002464// To keep the stack aligned according to platform abi the function
2465// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2466// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002467// If a tail called function callee has more arguments than the caller the
2468// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002469// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002470// original REtADDR, but before the saved framepointer or the spilled registers
2471// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2472// stack layout:
2473// arg1
2474// arg2
2475// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002476// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002477// move area ]
2478// (possible EBP)
2479// ESI
2480// EDI
2481// local1 ..
2482
2483/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2484/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002485unsigned
2486X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2487 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002488 MachineFunction &MF = DAG.getMachineFunction();
2489 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002490 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002491 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002492 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002493 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002494 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002495 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2496 // Number smaller than 12 so just add the difference.
2497 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2498 } else {
2499 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002500 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002501 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002502 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002503 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002504}
2505
Evan Cheng5f941932010-02-05 02:21:12 +00002506/// MatchingStackOffset - Return true if the given stack call argument is
2507/// already available in the same position (relatively) of the caller's
2508/// incoming argument stack.
2509static
2510bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2511 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2512 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002513 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2514 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002515 if (Arg.getOpcode() == ISD::CopyFromReg) {
2516 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002517 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002518 return false;
2519 MachineInstr *Def = MRI->getVRegDef(VR);
2520 if (!Def)
2521 return false;
2522 if (!Flags.isByVal()) {
2523 if (!TII->isLoadFromStackSlot(Def, FI))
2524 return false;
2525 } else {
2526 unsigned Opcode = Def->getOpcode();
2527 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2528 Def->getOperand(1).isFI()) {
2529 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002530 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002531 } else
2532 return false;
2533 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002534 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2535 if (Flags.isByVal())
2536 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002537 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002538 // define @foo(%struct.X* %A) {
2539 // tail call @bar(%struct.X* byval %A)
2540 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002541 return false;
2542 SDValue Ptr = Ld->getBasePtr();
2543 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2544 if (!FINode)
2545 return false;
2546 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002547 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002548 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002549 FI = FINode->getIndex();
2550 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002551 } else
2552 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002553
Evan Cheng4cae1332010-03-05 08:38:04 +00002554 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002555 if (!MFI->isFixedObjectIndex(FI))
2556 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002557 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002558}
2559
Dan Gohman98ca4f22009-08-05 01:29:28 +00002560/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2561/// for tail call optimization. Targets which want to do tail call
2562/// optimization should implement this function.
2563bool
2564X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002565 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002566 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002567 bool isCalleeStructRet,
2568 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002569 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002570 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002571 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002572 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002573 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002574 CalleeCC != CallingConv::C)
2575 return false;
2576
Evan Cheng7096ae42010-01-29 06:45:59 +00002577 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002578 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002579 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002580 CallingConv::ID CallerCC = CallerF->getCallingConv();
2581 bool CCMatch = CallerCC == CalleeCC;
2582
Dan Gohman1797ed52010-02-08 20:27:50 +00002583 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002584 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002585 return true;
2586 return false;
2587 }
2588
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002589 // Look for obvious safe cases to perform tail call optimization that do not
2590 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002591
Evan Cheng2c12cb42010-03-26 16:26:03 +00002592 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2593 // emit a special epilogue.
2594 if (RegInfo->needsStackRealignment(MF))
2595 return false;
2596
Evan Chenga375d472010-03-15 18:54:48 +00002597 // Also avoid sibcall optimization if either caller or callee uses struct
2598 // return semantics.
2599 if (isCalleeStructRet || isCallerStructRet)
2600 return false;
2601
Chad Rosier2416da32011-06-24 21:15:36 +00002602 // An stdcall caller is expected to clean up its arguments; the callee
2603 // isn't going to do that.
2604 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2605 return false;
2606
Chad Rosier871f6642011-05-18 19:59:50 +00002607 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002608 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002609 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002610
2611 // Optimizing for varargs on Win64 is unlikely to be safe without
2612 // additional testing.
2613 if (Subtarget->isTargetWin64())
2614 return false;
2615
Chad Rosier871f6642011-05-18 19:59:50 +00002616 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002617 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2618 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002619
Chad Rosier871f6642011-05-18 19:59:50 +00002620 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2621 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2622 if (!ArgLocs[i].isRegLoc())
2623 return false;
2624 }
2625
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002626 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2627 // Therefore if it's not used by the call it is not safe to optimize this into
2628 // a sibcall.
2629 bool Unused = false;
2630 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2631 if (!Ins[i].Used) {
2632 Unused = true;
2633 break;
2634 }
2635 }
2636 if (Unused) {
2637 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002638 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2639 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002640 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002641 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002642 CCValAssign &VA = RVLocs[i];
2643 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2644 return false;
2645 }
2646 }
2647
Evan Cheng13617962010-04-30 01:12:32 +00002648 // If the calling conventions do not match, then we'd better make sure the
2649 // results are returned in the same way as what the caller expects.
2650 if (!CCMatch) {
2651 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002652 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2653 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002654 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2655
2656 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002657 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2658 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002659 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2660
2661 if (RVLocs1.size() != RVLocs2.size())
2662 return false;
2663 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2664 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2665 return false;
2666 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2667 return false;
2668 if (RVLocs1[i].isRegLoc()) {
2669 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2670 return false;
2671 } else {
2672 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2673 return false;
2674 }
2675 }
2676 }
2677
Evan Chenga6bff982010-01-30 01:22:00 +00002678 // If the callee takes no arguments then go on to check the results of the
2679 // call.
2680 if (!Outs.empty()) {
2681 // Check if stack adjustment is needed. For now, do not do this if any
2682 // argument is passed on the stack.
2683 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002684 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2685 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002686
2687 // Allocate shadow area for Win64
2688 if (Subtarget->isTargetWin64()) {
2689 CCInfo.AllocateStack(32, 8);
2690 }
2691
Duncan Sands45907662010-10-31 13:21:44 +00002692 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002693 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002694 MachineFunction &MF = DAG.getMachineFunction();
2695 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2696 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002697
2698 // Check if the arguments are already laid out in the right way as
2699 // the caller's fixed stack objects.
2700 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002701 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2702 const X86InstrInfo *TII =
2703 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002704 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2705 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002706 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002707 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002708 if (VA.getLocInfo() == CCValAssign::Indirect)
2709 return false;
2710 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002711 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2712 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002713 return false;
2714 }
2715 }
2716 }
Evan Cheng9c044672010-05-29 01:35:22 +00002717
2718 // If the tailcall address may be in a register, then make sure it's
2719 // possible to register allocate for it. In 32-bit, the call address can
2720 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002721 // callee-saved registers are restored. These happen to be the same
2722 // registers used to pass 'inreg' arguments so watch out for those.
2723 if (!Subtarget->is64Bit() &&
2724 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002725 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002726 unsigned NumInRegs = 0;
2727 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2728 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002729 if (!VA.isRegLoc())
2730 continue;
2731 unsigned Reg = VA.getLocReg();
2732 switch (Reg) {
2733 default: break;
2734 case X86::EAX: case X86::EDX: case X86::ECX:
2735 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002736 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002737 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002738 }
2739 }
2740 }
Evan Chenga6bff982010-01-30 01:22:00 +00002741 }
Evan Chengb1712452010-01-27 06:25:16 +00002742
Evan Cheng86809cc2010-02-03 03:28:02 +00002743 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002744}
2745
Dan Gohman3df24e62008-09-03 23:12:08 +00002746FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002747X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2748 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002749}
2750
2751
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002752//===----------------------------------------------------------------------===//
2753// Other Lowering Hooks
2754//===----------------------------------------------------------------------===//
2755
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002756static bool MayFoldLoad(SDValue Op) {
2757 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2758}
2759
2760static bool MayFoldIntoStore(SDValue Op) {
2761 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2762}
2763
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002764static bool isTargetShuffle(unsigned Opcode) {
2765 switch(Opcode) {
2766 default: return false;
2767 case X86ISD::PSHUFD:
2768 case X86ISD::PSHUFHW:
2769 case X86ISD::PSHUFLW:
2770 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002771 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002772 case X86ISD::SHUFPS:
2773 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002774 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002775 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002776 case X86ISD::MOVLPS:
2777 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002778 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002779 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002780 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002781 case X86ISD::MOVSS:
2782 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002783 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002784 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002785 case X86ISD::VUNPCKLPSY:
2786 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002787 case X86ISD::PUNPCKLWD:
2788 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002789 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002790 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002791 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002792 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002793 case X86ISD::VUNPCKHPSY:
2794 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002795 case X86ISD::PUNPCKHWD:
2796 case X86ISD::PUNPCKHBW:
2797 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002798 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002799 case X86ISD::VPERMILPS:
2800 case X86ISD::VPERMILPSY:
2801 case X86ISD::VPERMILPD:
2802 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002803 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002804 return true;
2805 }
2806 return false;
2807}
2808
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002809static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002810 SDValue V1, SelectionDAG &DAG) {
2811 switch(Opc) {
2812 default: llvm_unreachable("Unknown x86 shuffle node");
2813 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002814 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002815 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002816 return DAG.getNode(Opc, dl, VT, V1);
2817 }
2818
2819 return SDValue();
2820}
2821
2822static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002823 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002824 switch(Opc) {
2825 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002826 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002827 case X86ISD::PSHUFHW:
2828 case X86ISD::PSHUFLW:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002829 case X86ISD::VPERMILPS:
2830 case X86ISD::VPERMILPSY:
2831 case X86ISD::VPERMILPD:
2832 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002833 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2834 }
2835
2836 return SDValue();
2837}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002838
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002839static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2840 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2841 switch(Opc) {
2842 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002843 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002844 case X86ISD::SHUFPD:
2845 case X86ISD::SHUFPS:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002846 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002847 return DAG.getNode(Opc, dl, VT, V1, V2,
2848 DAG.getConstant(TargetMask, MVT::i8));
2849 }
2850 return SDValue();
2851}
2852
2853static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2854 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2855 switch(Opc) {
2856 default: llvm_unreachable("Unknown x86 shuffle node");
2857 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002858 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002859 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002860 case X86ISD::MOVLPS:
2861 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002862 case X86ISD::MOVSS:
2863 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002864 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002865 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002866 case X86ISD::VUNPCKLPSY:
2867 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002868 case X86ISD::PUNPCKLWD:
2869 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002870 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002871 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002872 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002873 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002874 case X86ISD::VUNPCKHPSY:
2875 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002876 case X86ISD::PUNPCKHWD:
2877 case X86ISD::PUNPCKHBW:
2878 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002879 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002880 return DAG.getNode(Opc, dl, VT, V1, V2);
2881 }
2882 return SDValue();
2883}
2884
Dan Gohmand858e902010-04-17 15:26:15 +00002885SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002886 MachineFunction &MF = DAG.getMachineFunction();
2887 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2888 int ReturnAddrIndex = FuncInfo->getRAIndex();
2889
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002890 if (ReturnAddrIndex == 0) {
2891 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002892 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002893 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002894 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002895 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002896 }
2897
Evan Cheng25ab6902006-09-08 06:48:29 +00002898 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002899}
2900
2901
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002902bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2903 bool hasSymbolicDisplacement) {
2904 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002905 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002906 return false;
2907
2908 // If we don't have a symbolic displacement - we don't have any extra
2909 // restrictions.
2910 if (!hasSymbolicDisplacement)
2911 return true;
2912
2913 // FIXME: Some tweaks might be needed for medium code model.
2914 if (M != CodeModel::Small && M != CodeModel::Kernel)
2915 return false;
2916
2917 // For small code model we assume that latest object is 16MB before end of 31
2918 // bits boundary. We may also accept pretty large negative constants knowing
2919 // that all objects are in the positive half of address space.
2920 if (M == CodeModel::Small && Offset < 16*1024*1024)
2921 return true;
2922
2923 // For kernel code model we know that all object resist in the negative half
2924 // of 32bits address space. We may not accept negative offsets, since they may
2925 // be just off and we may accept pretty large positive ones.
2926 if (M == CodeModel::Kernel && Offset > 0)
2927 return true;
2928
2929 return false;
2930}
2931
Evan Chengef41ff62011-06-23 17:54:54 +00002932/// isCalleePop - Determines whether the callee is required to pop its
2933/// own arguments. Callee pop is necessary to support tail calls.
2934bool X86::isCalleePop(CallingConv::ID CallingConv,
2935 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2936 if (IsVarArg)
2937 return false;
2938
2939 switch (CallingConv) {
2940 default:
2941 return false;
2942 case CallingConv::X86_StdCall:
2943 return !is64Bit;
2944 case CallingConv::X86_FastCall:
2945 return !is64Bit;
2946 case CallingConv::X86_ThisCall:
2947 return !is64Bit;
2948 case CallingConv::Fast:
2949 return TailCallOpt;
2950 case CallingConv::GHC:
2951 return TailCallOpt;
2952 }
2953}
2954
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002955/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2956/// specific condition code, returning the condition code and the LHS/RHS of the
2957/// comparison to make.
2958static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2959 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002960 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002961 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2962 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2963 // X > -1 -> X == 0, jump !sign.
2964 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002965 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002966 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2967 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002968 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002969 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002970 // X < 1 -> X <= 0
2971 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002972 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002973 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002974 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002975
Evan Chengd9558e02006-01-06 00:43:03 +00002976 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002977 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002978 case ISD::SETEQ: return X86::COND_E;
2979 case ISD::SETGT: return X86::COND_G;
2980 case ISD::SETGE: return X86::COND_GE;
2981 case ISD::SETLT: return X86::COND_L;
2982 case ISD::SETLE: return X86::COND_LE;
2983 case ISD::SETNE: return X86::COND_NE;
2984 case ISD::SETULT: return X86::COND_B;
2985 case ISD::SETUGT: return X86::COND_A;
2986 case ISD::SETULE: return X86::COND_BE;
2987 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002988 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002989 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002990
Chris Lattner4c78e022008-12-23 23:42:27 +00002991 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002992
Chris Lattner4c78e022008-12-23 23:42:27 +00002993 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002994 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2995 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002996 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2997 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002998 }
2999
Chris Lattner4c78e022008-12-23 23:42:27 +00003000 switch (SetCCOpcode) {
3001 default: break;
3002 case ISD::SETOLT:
3003 case ISD::SETOLE:
3004 case ISD::SETUGT:
3005 case ISD::SETUGE:
3006 std::swap(LHS, RHS);
3007 break;
3008 }
3009
3010 // On a floating point condition, the flags are set as follows:
3011 // ZF PF CF op
3012 // 0 | 0 | 0 | X > Y
3013 // 0 | 0 | 1 | X < Y
3014 // 1 | 0 | 0 | X == Y
3015 // 1 | 1 | 1 | unordered
3016 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003017 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003018 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003019 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003020 case ISD::SETOLT: // flipped
3021 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003022 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003023 case ISD::SETOLE: // flipped
3024 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003025 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003026 case ISD::SETUGT: // flipped
3027 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003028 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003029 case ISD::SETUGE: // flipped
3030 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003031 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003032 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003033 case ISD::SETNE: return X86::COND_NE;
3034 case ISD::SETUO: return X86::COND_P;
3035 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003036 case ISD::SETOEQ:
3037 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003038 }
Evan Chengd9558e02006-01-06 00:43:03 +00003039}
3040
Evan Cheng4a460802006-01-11 00:33:36 +00003041/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3042/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003043/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003044static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003045 switch (X86CC) {
3046 default:
3047 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003048 case X86::COND_B:
3049 case X86::COND_BE:
3050 case X86::COND_E:
3051 case X86::COND_P:
3052 case X86::COND_A:
3053 case X86::COND_AE:
3054 case X86::COND_NE:
3055 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003056 return true;
3057 }
3058}
3059
Evan Chengeb2f9692009-10-27 19:56:55 +00003060/// isFPImmLegal - Returns true if the target can instruction select the
3061/// specified FP immediate natively. If false, the legalizer will
3062/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003063bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003064 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3065 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3066 return true;
3067 }
3068 return false;
3069}
3070
Nate Begeman9008ca62009-04-27 18:41:29 +00003071/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3072/// the specified range (L, H].
3073static bool isUndefOrInRange(int Val, int Low, int Hi) {
3074 return (Val < 0) || (Val >= Low && Val < Hi);
3075}
3076
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003077/// isUndefOrInRange - Return true if every element in Mask, begining
3078/// from position Pos and ending in Pos+Size, falls within the specified
3079/// range (L, L+Pos]. or is undef.
3080static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3081 int Pos, int Size, int Low, int Hi) {
3082 for (int i = Pos, e = Pos+Size; i != e; ++i)
3083 if (!isUndefOrInRange(Mask[i], Low, Hi))
3084 return false;
3085 return true;
3086}
3087
Nate Begeman9008ca62009-04-27 18:41:29 +00003088/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3089/// specified value.
3090static bool isUndefOrEqual(int Val, int CmpVal) {
3091 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003092 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003093 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003094}
3095
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003096/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3097/// from position Pos and ending in Pos+Size, falls within the specified
3098/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003099static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3100 int Pos, int Size, int Low) {
3101 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3102 if (!isUndefOrEqual(Mask[i], Low))
3103 return false;
3104 return true;
3105}
3106
Nate Begeman9008ca62009-04-27 18:41:29 +00003107/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3108/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3109/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003110static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003111 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003112 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003113 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003114 return (Mask[0] < 2 && Mask[1] < 2);
3115 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003116}
3117
Nate Begeman9008ca62009-04-27 18:41:29 +00003118bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003119 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003120 N->getMask(M);
3121 return ::isPSHUFDMask(M, N->getValueType(0));
3122}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003123
Nate Begeman9008ca62009-04-27 18:41:29 +00003124/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3125/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003126static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003127 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003128 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003129
Nate Begeman9008ca62009-04-27 18:41:29 +00003130 // Lower quadword copied in order or undef.
3131 for (int i = 0; i != 4; ++i)
3132 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003133 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003134
Evan Cheng506d3df2006-03-29 23:07:14 +00003135 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003136 for (int i = 4; i != 8; ++i)
3137 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003138 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003139
Evan Cheng506d3df2006-03-29 23:07:14 +00003140 return true;
3141}
3142
Nate Begeman9008ca62009-04-27 18:41:29 +00003143bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003144 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003145 N->getMask(M);
3146 return ::isPSHUFHWMask(M, N->getValueType(0));
3147}
Evan Cheng506d3df2006-03-29 23:07:14 +00003148
Nate Begeman9008ca62009-04-27 18:41:29 +00003149/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3150/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003151static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003152 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003153 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003154
Rafael Espindola15684b22009-04-24 12:40:33 +00003155 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003156 for (int i = 4; i != 8; ++i)
3157 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003158 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003159
Rafael Espindola15684b22009-04-24 12:40:33 +00003160 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003161 for (int i = 0; i != 4; ++i)
3162 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003163 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003164
Rafael Espindola15684b22009-04-24 12:40:33 +00003165 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003166}
3167
Nate Begeman9008ca62009-04-27 18:41:29 +00003168bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003169 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003170 N->getMask(M);
3171 return ::isPSHUFLWMask(M, N->getValueType(0));
3172}
3173
Nate Begemana09008b2009-10-19 02:17:23 +00003174/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3175/// is suitable for input to PALIGNR.
3176static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3177 bool hasSSSE3) {
3178 int i, e = VT.getVectorNumElements();
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003179 if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
3180 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003181
Nate Begemana09008b2009-10-19 02:17:23 +00003182 // Do not handle v2i64 / v2f64 shuffles with palignr.
3183 if (e < 4 || !hasSSSE3)
3184 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003185
Nate Begemana09008b2009-10-19 02:17:23 +00003186 for (i = 0; i != e; ++i)
3187 if (Mask[i] >= 0)
3188 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003189
Nate Begemana09008b2009-10-19 02:17:23 +00003190 // All undef, not a palignr.
3191 if (i == e)
3192 return false;
3193
Eli Friedman63f8dde2011-07-25 21:36:45 +00003194 // Make sure we're shifting in the right direction.
3195 if (Mask[i] <= i)
3196 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003197
3198 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003199
Nate Begemana09008b2009-10-19 02:17:23 +00003200 // Check the rest of the elements to see if they are consecutive.
3201 for (++i; i != e; ++i) {
3202 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003203 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003204 return false;
3205 }
3206 return true;
3207}
3208
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003209/// isVSHUFPSYMask - Return true if the specified VECTOR_SHUFFLE operand
3210/// specifies a shuffle of elements that is suitable for input to 256-bit
3211/// VSHUFPSY.
3212static bool isVSHUFPSYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3213 const X86Subtarget *Subtarget) {
3214 int NumElems = VT.getVectorNumElements();
3215
3216 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3217 return false;
3218
3219 if (NumElems != 8)
3220 return false;
3221
3222 // VSHUFPSY divides the resulting vector into 4 chunks.
3223 // The sources are also splitted into 4 chunks, and each destination
3224 // chunk must come from a different source chunk.
3225 //
3226 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3227 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3228 //
3229 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3230 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3231 //
3232 int QuarterSize = NumElems/4;
3233 int HalfSize = QuarterSize*2;
3234 for (int i = 0; i < QuarterSize; ++i)
3235 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3236 return false;
3237 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3238 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3239 return false;
3240
3241 // The mask of the second half must be the same as the first but with
3242 // the appropriate offsets. This works in the same way as VPERMILPS
3243 // works with masks.
3244 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3245 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3246 return false;
3247 int FstHalfIdx = i-HalfSize;
3248 if (Mask[FstHalfIdx] < 0)
3249 continue;
3250 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3251 return false;
3252 }
3253 for (int i = QuarterSize*3; i < NumElems; ++i) {
3254 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3255 return false;
3256 int FstHalfIdx = i-HalfSize;
3257 if (Mask[FstHalfIdx] < 0)
3258 continue;
3259 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3260 return false;
3261
3262 }
3263
3264 return true;
3265}
3266
3267/// getShuffleVSHUFPSYImmediate - Return the appropriate immediate to shuffle
3268/// the specified VECTOR_MASK mask with VSHUFPSY instruction.
3269static unsigned getShuffleVSHUFPSYImmediate(SDNode *N) {
3270 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3271 EVT VT = SVOp->getValueType(0);
3272 int NumElems = VT.getVectorNumElements();
3273
3274 assert(NumElems == 8 && VT.getSizeInBits() == 256 &&
3275 "Only supports v8i32 and v8f32 types");
3276
3277 int HalfSize = NumElems/2;
3278 unsigned Mask = 0;
3279 for (int i = 0; i != NumElems ; ++i) {
3280 if (SVOp->getMaskElt(i) < 0)
3281 continue;
3282 // The mask of the first half must be equal to the second one.
3283 unsigned Shamt = (i%HalfSize)*2;
3284 unsigned Elt = SVOp->getMaskElt(i) % HalfSize;
3285 Mask |= Elt << Shamt;
3286 }
3287
3288 return Mask;
3289}
3290
3291/// isVSHUFPDYMask - Return true if the specified VECTOR_SHUFFLE operand
3292/// specifies a shuffle of elements that is suitable for input to 256-bit
3293/// VSHUFPDY. This shuffle doesn't have the same restriction as the PS
3294/// version and the mask of the second half isn't binded with the first
3295/// one.
3296static bool isVSHUFPDYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3297 const X86Subtarget *Subtarget) {
3298 int NumElems = VT.getVectorNumElements();
3299
3300 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3301 return false;
3302
3303 if (NumElems != 4)
3304 return false;
3305
3306 // VSHUFPSY divides the resulting vector into 4 chunks.
3307 // The sources are also splitted into 4 chunks, and each destination
3308 // chunk must come from a different source chunk.
3309 //
3310 // SRC1 => X3 X2 X1 X0
3311 // SRC2 => Y3 Y2 Y1 Y0
3312 //
3313 // DST => Y2..Y3, X2..X3, Y1..Y0, X1..X0
3314 //
3315 int QuarterSize = NumElems/4;
3316 int HalfSize = QuarterSize*2;
3317 for (int i = 0; i < QuarterSize; ++i)
3318 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3319 return false;
3320 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3321 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3322 return false;
3323 for (int i = QuarterSize*2; i < QuarterSize*3; ++i)
3324 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3325 return false;
3326 for (int i = QuarterSize*3; i < NumElems; ++i)
3327 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3328 return false;
3329
3330 return true;
3331}
3332
3333/// getShuffleVSHUFPDYImmediate - Return the appropriate immediate to shuffle
3334/// the specified VECTOR_MASK mask with VSHUFPDY instruction.
3335static unsigned getShuffleVSHUFPDYImmediate(SDNode *N) {
3336 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3337 EVT VT = SVOp->getValueType(0);
3338 int NumElems = VT.getVectorNumElements();
3339
3340 assert(NumElems == 4 && VT.getSizeInBits() == 256 &&
3341 "Only supports v4i64 and v4f64 types");
3342
3343 int HalfSize = NumElems/2;
3344 unsigned Mask = 0;
3345 for (int i = 0; i != NumElems ; ++i) {
3346 if (SVOp->getMaskElt(i) < 0)
3347 continue;
3348 int Elt = SVOp->getMaskElt(i) % HalfSize;
3349 Mask |= Elt << i;
3350 }
3351
3352 return Mask;
3353}
3354
Evan Cheng14aed5e2006-03-24 01:18:28 +00003355/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003356/// specifies a shuffle of elements that is suitable for input to 128-bit
3357/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003358static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003359 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003360
3361 if (VT.getSizeInBits() != 128)
3362 return false;
3363
Nate Begeman9008ca62009-04-27 18:41:29 +00003364 if (NumElems != 2 && NumElems != 4)
3365 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003366
Nate Begeman9008ca62009-04-27 18:41:29 +00003367 int Half = NumElems / 2;
3368 for (int i = 0; i < Half; ++i)
3369 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003370 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003371 for (int i = Half; i < NumElems; ++i)
3372 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003373 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003374
Evan Cheng14aed5e2006-03-24 01:18:28 +00003375 return true;
3376}
3377
Nate Begeman9008ca62009-04-27 18:41:29 +00003378bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3379 SmallVector<int, 8> M;
3380 N->getMask(M);
3381 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003382}
3383
Evan Cheng213d2cf2007-05-17 18:45:50 +00003384/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003385/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3386/// half elements to come from vector 1 (which would equal the dest.) and
3387/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003388static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003389 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003390
3391 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003392 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003393
Nate Begeman9008ca62009-04-27 18:41:29 +00003394 int Half = NumElems / 2;
3395 for (int i = 0; i < Half; ++i)
3396 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003397 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003398 for (int i = Half; i < NumElems; ++i)
3399 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003400 return false;
3401 return true;
3402}
3403
Nate Begeman9008ca62009-04-27 18:41:29 +00003404static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3405 SmallVector<int, 8> M;
3406 N->getMask(M);
3407 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003408}
3409
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003410/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3411/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003412bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003413 EVT VT = N->getValueType(0);
3414 unsigned NumElems = VT.getVectorNumElements();
3415
3416 if (VT.getSizeInBits() != 128)
3417 return false;
3418
3419 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003420 return false;
3421
Evan Cheng2064a2b2006-03-28 06:50:32 +00003422 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003423 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3424 isUndefOrEqual(N->getMaskElt(1), 7) &&
3425 isUndefOrEqual(N->getMaskElt(2), 2) &&
3426 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003427}
3428
Nate Begeman0b10b912009-11-07 23:17:15 +00003429/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3430/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3431/// <2, 3, 2, 3>
3432bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003433 EVT VT = N->getValueType(0);
3434 unsigned NumElems = VT.getVectorNumElements();
3435
3436 if (VT.getSizeInBits() != 128)
3437 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003438
Nate Begeman0b10b912009-11-07 23:17:15 +00003439 if (NumElems != 4)
3440 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003441
Nate Begeman0b10b912009-11-07 23:17:15 +00003442 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003443 isUndefOrEqual(N->getMaskElt(1), 3) &&
3444 isUndefOrEqual(N->getMaskElt(2), 2) &&
3445 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003446}
3447
Evan Cheng5ced1d82006-04-06 23:23:56 +00003448/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3449/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003450bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3451 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003452
Evan Cheng5ced1d82006-04-06 23:23:56 +00003453 if (NumElems != 2 && NumElems != 4)
3454 return false;
3455
Evan Chengc5cdff22006-04-07 21:53:05 +00003456 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003457 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003458 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003459
Evan Chengc5cdff22006-04-07 21:53:05 +00003460 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003461 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003462 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003463
3464 return true;
3465}
3466
Nate Begeman0b10b912009-11-07 23:17:15 +00003467/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3468/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3469bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003470 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003471
David Greenea20244d2011-03-02 17:23:43 +00003472 if ((NumElems != 2 && NumElems != 4)
3473 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003474 return false;
3475
Evan Chengc5cdff22006-04-07 21:53:05 +00003476 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003477 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003478 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003479
Nate Begeman9008ca62009-04-27 18:41:29 +00003480 for (unsigned i = 0; i < NumElems/2; ++i)
3481 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003482 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003483
3484 return true;
3485}
3486
Evan Cheng0038e592006-03-28 00:39:58 +00003487/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3488/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003489static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003490 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003491 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003492
3493 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3494 "Unsupported vector type for unpckh");
3495
3496 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng0038e592006-03-28 00:39:58 +00003497 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003498
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003499 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3500 // independently on 128-bit lanes.
3501 unsigned NumLanes = VT.getSizeInBits()/128;
3502 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003503
3504 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003505 unsigned End = NumLaneElts;
3506 for (unsigned s = 0; s < NumLanes; ++s) {
3507 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003508 i != End;
3509 i += 2, ++j) {
3510 int BitI = Mask[i];
3511 int BitI1 = Mask[i+1];
3512 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003513 return false;
David Greenea20244d2011-03-02 17:23:43 +00003514 if (V2IsSplat) {
3515 if (!isUndefOrEqual(BitI1, NumElts))
3516 return false;
3517 } else {
3518 if (!isUndefOrEqual(BitI1, j + NumElts))
3519 return false;
3520 }
Evan Cheng39623da2006-04-20 08:58:49 +00003521 }
David Greenea20244d2011-03-02 17:23:43 +00003522 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003523 Start += NumLaneElts;
3524 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003525 }
David Greenea20244d2011-03-02 17:23:43 +00003526
Evan Cheng0038e592006-03-28 00:39:58 +00003527 return true;
3528}
3529
Nate Begeman9008ca62009-04-27 18:41:29 +00003530bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3531 SmallVector<int, 8> M;
3532 N->getMask(M);
3533 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003534}
3535
Evan Cheng4fcb9222006-03-28 02:43:26 +00003536/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3537/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003538static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003539 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003540 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003541
3542 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3543 "Unsupported vector type for unpckh");
3544
3545 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003546 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003547
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003548 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3549 // independently on 128-bit lanes.
3550 unsigned NumLanes = VT.getSizeInBits()/128;
3551 unsigned NumLaneElts = NumElts/NumLanes;
3552
3553 unsigned Start = 0;
3554 unsigned End = NumLaneElts;
3555 for (unsigned l = 0; l != NumLanes; ++l) {
3556 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3557 i != End; i += 2, ++j) {
3558 int BitI = Mask[i];
3559 int BitI1 = Mask[i+1];
3560 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003561 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003562 if (V2IsSplat) {
3563 if (isUndefOrEqual(BitI1, NumElts))
3564 return false;
3565 } else {
3566 if (!isUndefOrEqual(BitI1, j+NumElts))
3567 return false;
3568 }
Evan Cheng39623da2006-04-20 08:58:49 +00003569 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003570 // Process the next 128 bits.
3571 Start += NumLaneElts;
3572 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003573 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003574 return true;
3575}
3576
Nate Begeman9008ca62009-04-27 18:41:29 +00003577bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3578 SmallVector<int, 8> M;
3579 N->getMask(M);
3580 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003581}
3582
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003583/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3584/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3585/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003586static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003587 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003588 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003589 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003590
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003591 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3592 // FIXME: Need a better way to get rid of this, there's no latency difference
3593 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3594 // the former later. We should also remove the "_undef" special mask.
3595 if (NumElems == 4 && VT.getSizeInBits() == 256)
3596 return false;
3597
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003598 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3599 // independently on 128-bit lanes.
3600 unsigned NumLanes = VT.getSizeInBits() / 128;
3601 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003602
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003603 for (unsigned s = 0; s < NumLanes; ++s) {
3604 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3605 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003606 i += 2, ++j) {
3607 int BitI = Mask[i];
3608 int BitI1 = Mask[i+1];
3609
3610 if (!isUndefOrEqual(BitI, j))
3611 return false;
3612 if (!isUndefOrEqual(BitI1, j))
3613 return false;
3614 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003615 }
David Greenea20244d2011-03-02 17:23:43 +00003616
Rafael Espindola15684b22009-04-24 12:40:33 +00003617 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003618}
3619
Nate Begeman9008ca62009-04-27 18:41:29 +00003620bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3621 SmallVector<int, 8> M;
3622 N->getMask(M);
3623 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3624}
3625
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003626/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3627/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3628/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003629static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003630 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003631 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3632 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003633
Nate Begeman9008ca62009-04-27 18:41:29 +00003634 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3635 int BitI = Mask[i];
3636 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003637 if (!isUndefOrEqual(BitI, j))
3638 return false;
3639 if (!isUndefOrEqual(BitI1, j))
3640 return false;
3641 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003642 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003643}
3644
Nate Begeman9008ca62009-04-27 18:41:29 +00003645bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3646 SmallVector<int, 8> M;
3647 N->getMask(M);
3648 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3649}
3650
Evan Cheng017dcc62006-04-21 01:05:10 +00003651/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3652/// specifies a shuffle of elements that is suitable for input to MOVSS,
3653/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003654static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003655 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003656 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003657
3658 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003659
Nate Begeman9008ca62009-04-27 18:41:29 +00003660 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003661 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003662
Nate Begeman9008ca62009-04-27 18:41:29 +00003663 for (int i = 1; i < NumElts; ++i)
3664 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003665 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003666
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003667 return true;
3668}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003669
Nate Begeman9008ca62009-04-27 18:41:29 +00003670bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3671 SmallVector<int, 8> M;
3672 N->getMask(M);
3673 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003674}
3675
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003676/// isVPERM2F128Mask - Match 256-bit shuffles where the elements are considered
3677/// as permutations between 128-bit chunks or halves. As an example: this
3678/// shuffle bellow:
3679/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3680/// The first half comes from the second half of V1 and the second half from the
3681/// the second half of V2.
3682static bool isVPERM2F128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3683 const X86Subtarget *Subtarget) {
3684 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3685 return false;
3686
3687 // The shuffle result is divided into half A and half B. In total the two
3688 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3689 // B must come from C, D, E or F.
3690 int HalfSize = VT.getVectorNumElements()/2;
3691 bool MatchA = false, MatchB = false;
3692
3693 // Check if A comes from one of C, D, E, F.
3694 for (int Half = 0; Half < 4; ++Half) {
3695 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3696 MatchA = true;
3697 break;
3698 }
3699 }
3700
3701 // Check if B comes from one of C, D, E, F.
3702 for (int Half = 0; Half < 4; ++Half) {
3703 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3704 MatchB = true;
3705 break;
3706 }
3707 }
3708
3709 return MatchA && MatchB;
3710}
3711
3712/// getShuffleVPERM2F128Immediate - Return the appropriate immediate to shuffle
3713/// the specified VECTOR_MASK mask with VPERM2F128 instructions.
3714static unsigned getShuffleVPERM2F128Immediate(SDNode *N) {
3715 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3716 EVT VT = SVOp->getValueType(0);
3717
3718 int HalfSize = VT.getVectorNumElements()/2;
3719
3720 int FstHalf = 0, SndHalf = 0;
3721 for (int i = 0; i < HalfSize; ++i) {
3722 if (SVOp->getMaskElt(i) > 0) {
3723 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3724 break;
3725 }
3726 }
3727 for (int i = HalfSize; i < HalfSize*2; ++i) {
3728 if (SVOp->getMaskElt(i) > 0) {
3729 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3730 break;
3731 }
3732 }
3733
3734 return (FstHalf | (SndHalf << 4));
3735}
3736
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003737/// isVPERMILPDMask - Return true if the specified VECTOR_SHUFFLE operand
3738/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3739/// Note that VPERMIL mask matching is different depending whether theunderlying
3740/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3741/// to the same elements of the low, but to the higher half of the source.
3742/// In VPERMILPD the two lanes could be shuffled independently of each other
3743/// with the same restriction that lanes can't be crossed.
3744static bool isVPERMILPDMask(const SmallVectorImpl<int> &Mask, EVT VT,
3745 const X86Subtarget *Subtarget) {
3746 int NumElts = VT.getVectorNumElements();
3747 int NumLanes = VT.getSizeInBits()/128;
3748
3749 if (!Subtarget->hasAVX())
3750 return false;
3751
3752 // Match any permutation of 128-bit vector with 64-bit types
3753 if (NumLanes == 1 && NumElts != 2)
3754 return false;
3755
3756 // Only match 256-bit with 32 types
3757 if (VT.getSizeInBits() == 256 && NumElts != 4)
3758 return false;
3759
3760 // The mask on the high lane is independent of the low. Both can match
3761 // any element in inside its own lane, but can't cross.
3762 int LaneSize = NumElts/NumLanes;
3763 for (int l = 0; l < NumLanes; ++l)
3764 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3765 int LaneStart = l*LaneSize;
3766 if (!isUndefOrInRange(Mask[i], LaneStart, LaneStart+LaneSize))
3767 return false;
3768 }
3769
3770 return true;
3771}
3772
3773/// isVPERMILPSMask - Return true if the specified VECTOR_SHUFFLE operand
3774/// specifies a shuffle of elements that is suitable for input to VPERMILPS*.
3775/// Note that VPERMIL mask matching is different depending whether theunderlying
3776/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3777/// to the same elements of the low, but to the higher half of the source.
3778/// In VPERMILPD the two lanes could be shuffled independently of each other
3779/// with the same restriction that lanes can't be crossed.
3780static bool isVPERMILPSMask(const SmallVectorImpl<int> &Mask, EVT VT,
3781 const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003782 unsigned NumElts = VT.getVectorNumElements();
3783 unsigned NumLanes = VT.getSizeInBits()/128;
3784
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003785 if (!Subtarget->hasAVX())
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003786 return false;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003787
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003788 // Match any permutation of 128-bit vector with 32-bit types
3789 if (NumLanes == 1 && NumElts != 4)
3790 return false;
3791
3792 // Only match 256-bit with 32 types
3793 if (VT.getSizeInBits() == 256 && NumElts != 8)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003794 return false;
3795
3796 // The mask on the high lane should be the same as the low. Actually,
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003797 // they can differ if any of the corresponding index in a lane is undef
3798 // and the other stays in range.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003799 int LaneSize = NumElts/NumLanes;
3800 for (int i = 0; i < LaneSize; ++i) {
3801 int HighElt = i+LaneSize;
Bruno Cardoso Lopes155a92a2011-08-10 01:54:17 +00003802 bool HighValid = isUndefOrInRange(Mask[HighElt], LaneSize, NumElts);
3803 bool LowValid = isUndefOrInRange(Mask[i], 0, LaneSize);
3804
3805 if (!HighValid || !LowValid)
3806 return false;
3807 if (Mask[i] < 0 || Mask[HighElt] < 0)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003808 continue;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003809 if (Mask[HighElt]-Mask[i] != LaneSize)
3810 return false;
3811 }
3812
3813 return true;
3814}
3815
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003816/// getShuffleVPERMILPSImmediate - Return the appropriate immediate to shuffle
3817/// the specified VECTOR_MASK mask with VPERMILPS* instructions.
3818static unsigned getShuffleVPERMILPSImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003819 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3820 EVT VT = SVOp->getValueType(0);
3821
3822 int NumElts = VT.getVectorNumElements();
3823 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003824 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003825
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003826 // Although the mask is equal for both lanes do it twice to get the cases
3827 // where a mask will match because the same mask element is undef on the
3828 // first half but valid on the second. This would get pathological cases
3829 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003830 unsigned Mask = 0;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003831 for (int l = 0; l < NumLanes; ++l) {
3832 for (int i = 0; i < LaneSize; ++i) {
3833 int MaskElt = SVOp->getMaskElt(i+(l*LaneSize));
3834 if (MaskElt < 0)
3835 continue;
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003836 if (MaskElt >= LaneSize)
3837 MaskElt -= LaneSize;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003838 Mask |= MaskElt << (i*2);
3839 }
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003840 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003841
3842 return Mask;
3843}
3844
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003845/// getShuffleVPERMILPDImmediate - Return the appropriate immediate to shuffle
3846/// the specified VECTOR_MASK mask with VPERMILPD* instructions.
3847static unsigned getShuffleVPERMILPDImmediate(SDNode *N) {
3848 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3849 EVT VT = SVOp->getValueType(0);
3850
3851 int NumElts = VT.getVectorNumElements();
3852 int NumLanes = VT.getSizeInBits()/128;
3853
3854 unsigned Mask = 0;
3855 int LaneSize = NumElts/NumLanes;
3856 for (int l = 0; l < NumLanes; ++l)
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003857 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3858 int MaskElt = SVOp->getMaskElt(i);
3859 if (MaskElt < 0)
3860 continue;
3861 Mask |= (MaskElt-l*LaneSize) << i;
3862 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003863
3864 return Mask;
3865}
3866
Evan Cheng017dcc62006-04-21 01:05:10 +00003867/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3868/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003869/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003870static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003871 bool V2IsSplat = false, bool V2IsUndef = false) {
3872 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003873 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003874 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003875
Nate Begeman9008ca62009-04-27 18:41:29 +00003876 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003877 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003878
Nate Begeman9008ca62009-04-27 18:41:29 +00003879 for (int i = 1; i < NumOps; ++i)
3880 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3881 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3882 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003883 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003884
Evan Cheng39623da2006-04-20 08:58:49 +00003885 return true;
3886}
3887
Nate Begeman9008ca62009-04-27 18:41:29 +00003888static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003889 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003890 SmallVector<int, 8> M;
3891 N->getMask(M);
3892 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003893}
3894
Evan Chengd9539472006-04-14 21:59:03 +00003895/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3896/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003897/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3898bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3899 const X86Subtarget *Subtarget) {
3900 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003901 return false;
3902
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003903 // The second vector must be undef
3904 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3905 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003906
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003907 EVT VT = N->getValueType(0);
3908 unsigned NumElems = VT.getVectorNumElements();
3909
3910 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3911 (VT.getSizeInBits() == 256 && NumElems != 8))
3912 return false;
3913
3914 // "i+1" is the value the indexed mask element must have
3915 for (unsigned i = 0; i < NumElems; i += 2)
3916 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3917 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003918 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003919
3920 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003921}
3922
3923/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3924/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003925/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3926bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3927 const X86Subtarget *Subtarget) {
3928 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003929 return false;
3930
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003931 // The second vector must be undef
3932 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3933 return false;
3934
3935 EVT VT = N->getValueType(0);
3936 unsigned NumElems = VT.getVectorNumElements();
3937
3938 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3939 (VT.getSizeInBits() == 256 && NumElems != 8))
3940 return false;
3941
3942 // "i" is the value the indexed mask element must have
3943 for (unsigned i = 0; i < NumElems; i += 2)
3944 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3945 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003946 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003947
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003948 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003949}
3950
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003951/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3952/// specifies a shuffle of elements that is suitable for input to 256-bit
3953/// version of MOVDDUP.
3954static bool isMOVDDUPYMask(ShuffleVectorSDNode *N,
3955 const X86Subtarget *Subtarget) {
3956 EVT VT = N->getValueType(0);
3957 int NumElts = VT.getVectorNumElements();
3958 bool V2IsUndef = N->getOperand(1).getOpcode() == ISD::UNDEF;
3959
3960 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256 ||
3961 !V2IsUndef || NumElts != 4)
3962 return false;
3963
3964 for (int i = 0; i != NumElts/2; ++i)
3965 if (!isUndefOrEqual(N->getMaskElt(i), 0))
3966 return false;
3967 for (int i = NumElts/2; i != NumElts; ++i)
3968 if (!isUndefOrEqual(N->getMaskElt(i), NumElts/2))
3969 return false;
3970 return true;
3971}
3972
Evan Cheng0b457f02008-09-25 20:50:48 +00003973/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003974/// specifies a shuffle of elements that is suitable for input to 128-bit
3975/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003976bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003977 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003978
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003979 if (VT.getSizeInBits() != 128)
3980 return false;
3981
3982 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003983 for (int i = 0; i < e; ++i)
3984 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003985 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003986 for (int i = 0; i < e; ++i)
3987 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003988 return false;
3989 return true;
3990}
3991
David Greenec38a03e2011-02-03 15:50:00 +00003992/// isVEXTRACTF128Index - Return true if the specified
3993/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3994/// suitable for input to VEXTRACTF128.
3995bool X86::isVEXTRACTF128Index(SDNode *N) {
3996 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3997 return false;
3998
3999 // The index should be aligned on a 128-bit boundary.
4000 uint64_t Index =
4001 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4002
4003 unsigned VL = N->getValueType(0).getVectorNumElements();
4004 unsigned VBits = N->getValueType(0).getSizeInBits();
4005 unsigned ElSize = VBits / VL;
4006 bool Result = (Index * ElSize) % 128 == 0;
4007
4008 return Result;
4009}
4010
David Greeneccacdc12011-02-04 16:08:29 +00004011/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4012/// operand specifies a subvector insert that is suitable for input to
4013/// VINSERTF128.
4014bool X86::isVINSERTF128Index(SDNode *N) {
4015 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4016 return false;
4017
4018 // The index should be aligned on a 128-bit boundary.
4019 uint64_t Index =
4020 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4021
4022 unsigned VL = N->getValueType(0).getVectorNumElements();
4023 unsigned VBits = N->getValueType(0).getSizeInBits();
4024 unsigned ElSize = VBits / VL;
4025 bool Result = (Index * ElSize) % 128 == 0;
4026
4027 return Result;
4028}
4029
Evan Cheng63d33002006-03-22 08:01:21 +00004030/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004031/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00004032unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004033 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4034 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4035
Evan Chengb9df0ca2006-03-22 02:53:00 +00004036 unsigned Shift = (NumOperands == 4) ? 2 : 1;
4037 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00004038 for (int i = 0; i < NumOperands; ++i) {
4039 int Val = SVOp->getMaskElt(NumOperands-i-1);
4040 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00004041 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00004042 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00004043 if (i != NumOperands - 1)
4044 Mask <<= Shift;
4045 }
Evan Cheng63d33002006-03-22 08:01:21 +00004046 return Mask;
4047}
4048
Evan Cheng506d3df2006-03-29 23:07:14 +00004049/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004050/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004051unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004052 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004053 unsigned Mask = 0;
4054 // 8 nodes, but we only care about the last 4.
4055 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004056 int Val = SVOp->getMaskElt(i);
4057 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004058 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00004059 if (i != 4)
4060 Mask <<= 2;
4061 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004062 return Mask;
4063}
4064
4065/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004066/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004067unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004068 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004069 unsigned Mask = 0;
4070 // 8 nodes, but we only care about the first 4.
4071 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004072 int Val = SVOp->getMaskElt(i);
4073 if (Val >= 0)
4074 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004075 if (i != 0)
4076 Mask <<= 2;
4077 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004078 return Mask;
4079}
4080
Nate Begemana09008b2009-10-19 02:17:23 +00004081/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4082/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4083unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4084 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4085 EVT VVT = N->getValueType(0);
4086 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4087 int Val = 0;
4088
4089 unsigned i, e;
4090 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4091 Val = SVOp->getMaskElt(i);
4092 if (Val >= 0)
4093 break;
4094 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004095 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004096 return (Val - i) * EltSize;
4097}
4098
David Greenec38a03e2011-02-03 15:50:00 +00004099/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4100/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4101/// instructions.
4102unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4103 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4104 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4105
4106 uint64_t Index =
4107 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4108
4109 EVT VecVT = N->getOperand(0).getValueType();
4110 EVT ElVT = VecVT.getVectorElementType();
4111
4112 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004113 return Index / NumElemsPerChunk;
4114}
4115
David Greeneccacdc12011-02-04 16:08:29 +00004116/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4117/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4118/// instructions.
4119unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4120 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4121 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4122
4123 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004124 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004125
4126 EVT VecVT = N->getValueType(0);
4127 EVT ElVT = VecVT.getVectorElementType();
4128
4129 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004130 return Index / NumElemsPerChunk;
4131}
4132
Evan Cheng37b73872009-07-30 08:33:02 +00004133/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4134/// constant +0.0.
4135bool X86::isZeroNode(SDValue Elt) {
4136 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004137 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004138 (isa<ConstantFPSDNode>(Elt) &&
4139 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4140}
4141
Nate Begeman9008ca62009-04-27 18:41:29 +00004142/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4143/// their permute mask.
4144static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4145 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004146 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004147 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004148 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004149
Nate Begeman5a5ca152009-04-29 05:20:52 +00004150 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004151 int idx = SVOp->getMaskElt(i);
4152 if (idx < 0)
4153 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004154 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004155 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004156 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004157 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004158 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004159 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4160 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004161}
4162
Evan Cheng779ccea2007-12-07 21:30:01 +00004163/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
4164/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00004165static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004166 unsigned NumElems = VT.getVectorNumElements();
4167 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004168 int idx = Mask[i];
4169 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004170 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004171 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004172 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004173 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004174 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004175 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004176}
4177
Evan Cheng533a0aa2006-04-19 20:35:22 +00004178/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4179/// match movhlps. The lower half elements should come from upper half of
4180/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004181/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004182static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004183 EVT VT = Op->getValueType(0);
4184 if (VT.getSizeInBits() != 128)
4185 return false;
4186 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004187 return false;
4188 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004189 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004190 return false;
4191 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004192 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004193 return false;
4194 return true;
4195}
4196
Evan Cheng5ced1d82006-04-06 23:23:56 +00004197/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004198/// is promoted to a vector. It also returns the LoadSDNode by reference if
4199/// required.
4200static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004201 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4202 return false;
4203 N = N->getOperand(0).getNode();
4204 if (!ISD::isNON_EXTLoad(N))
4205 return false;
4206 if (LD)
4207 *LD = cast<LoadSDNode>(N);
4208 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004209}
4210
Evan Cheng533a0aa2006-04-19 20:35:22 +00004211/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4212/// match movlp{s|d}. The lower half elements should come from lower half of
4213/// V1 (and in order), and the upper half elements should come from the upper
4214/// half of V2 (and in order). And since V1 will become the source of the
4215/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004216static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4217 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004218 EVT VT = Op->getValueType(0);
4219 if (VT.getSizeInBits() != 128)
4220 return false;
4221
Evan Cheng466685d2006-10-09 20:57:25 +00004222 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004223 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004224 // Is V2 is a vector load, don't do this transformation. We will try to use
4225 // load folding shufps op.
4226 if (ISD::isNON_EXTLoad(V2))
4227 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004228
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004229 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004230
Evan Cheng533a0aa2006-04-19 20:35:22 +00004231 if (NumElems != 2 && NumElems != 4)
4232 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004233 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004234 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004235 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004236 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004237 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004238 return false;
4239 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004240}
4241
Evan Cheng39623da2006-04-20 08:58:49 +00004242/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4243/// all the same.
4244static bool isSplatVector(SDNode *N) {
4245 if (N->getOpcode() != ISD::BUILD_VECTOR)
4246 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004247
Dan Gohman475871a2008-07-27 21:46:04 +00004248 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004249 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4250 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004251 return false;
4252 return true;
4253}
4254
Evan Cheng213d2cf2007-05-17 18:45:50 +00004255/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004256/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004257/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004258static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004259 SDValue V1 = N->getOperand(0);
4260 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004261 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4262 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004263 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004264 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004265 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004266 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4267 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004268 if (Opc != ISD::BUILD_VECTOR ||
4269 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004270 return false;
4271 } else if (Idx >= 0) {
4272 unsigned Opc = V1.getOpcode();
4273 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4274 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004275 if (Opc != ISD::BUILD_VECTOR ||
4276 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004277 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004278 }
4279 }
4280 return true;
4281}
4282
4283/// getZeroVector - Returns a vector of specified type with all zero elements.
4284///
Owen Andersone50ed302009-08-10 22:56:29 +00004285static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004286 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004287 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004288
Dale Johannesen0488fb62010-09-30 23:57:10 +00004289 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004290 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004291 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004292 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004293 if (HasSSE2) { // SSE2
4294 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4295 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4296 } else { // SSE1
4297 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4298 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4299 }
4300 } else if (VT.getSizeInBits() == 256) { // AVX
4301 // 256-bit logic and arithmetic instructions in AVX are
4302 // all floating-point, no support for integer ops. Default
4303 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004304 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004305 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4306 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004307 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004308 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004309}
4310
Chris Lattner8a594482007-11-25 00:24:49 +00004311/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004312/// Always build ones vectors as <4 x i32>. For 256-bit types, use two
4313/// <4 x i32> inserted in a <8 x i32> appropriately. Then bitcast to their
4314/// original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00004315static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004316 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004317 assert((VT.is128BitVector() || VT.is256BitVector())
4318 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004319
Owen Anderson825b72b2009-08-11 20:47:22 +00004320 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004321 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4322 Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004323
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004324 if (VT.is256BitVector()) {
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004325 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4326 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4327 Vec = Insert128BitVector(InsV, Vec,
4328 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4329 }
4330
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004331 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004332}
4333
Evan Cheng39623da2006-04-20 08:58:49 +00004334/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4335/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004336static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004337 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004338 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004339
Evan Cheng39623da2006-04-20 08:58:49 +00004340 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004341 SmallVector<int, 8> MaskVec;
4342 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004343
Nate Begeman5a5ca152009-04-29 05:20:52 +00004344 for (unsigned i = 0; i != NumElems; ++i) {
4345 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004346 MaskVec[i] = NumElems;
4347 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004348 }
Evan Cheng39623da2006-04-20 08:58:49 +00004349 }
Evan Cheng39623da2006-04-20 08:58:49 +00004350 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004351 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4352 SVOp->getOperand(1), &MaskVec[0]);
4353 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004354}
4355
Evan Cheng017dcc62006-04-21 01:05:10 +00004356/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4357/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004358static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004359 SDValue V2) {
4360 unsigned NumElems = VT.getVectorNumElements();
4361 SmallVector<int, 8> Mask;
4362 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004363 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004364 Mask.push_back(i);
4365 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004366}
4367
Nate Begeman9008ca62009-04-27 18:41:29 +00004368/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004369static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004370 SDValue V2) {
4371 unsigned NumElems = VT.getVectorNumElements();
4372 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004373 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004374 Mask.push_back(i);
4375 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004376 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004377 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004378}
4379
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004380/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004381static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004382 SDValue V2) {
4383 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004384 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004385 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004386 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004387 Mask.push_back(i + Half);
4388 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004389 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004390 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004391}
4392
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004393// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004394// a generic shuffle instruction because the target has no such instructions.
4395// Generate shuffles which repeat i16 and i8 several times until they can be
4396// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004397static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004398 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004399 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004400 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004401
Nate Begeman9008ca62009-04-27 18:41:29 +00004402 while (NumElems > 4) {
4403 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004404 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004405 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004406 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004407 EltNo -= NumElems/2;
4408 }
4409 NumElems >>= 1;
4410 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004411 return V;
4412}
Eric Christopherfd179292009-08-27 18:07:15 +00004413
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004414/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4415static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4416 EVT VT = V.getValueType();
4417 DebugLoc dl = V.getDebugLoc();
4418 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4419 && "Vector size not supported");
4420
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004421 if (VT.getSizeInBits() == 128) {
4422 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004423 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004424 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4425 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004426 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004427 // To use VPERMILPS to splat scalars, the second half of indicies must
4428 // refer to the higher part, which is a duplication of the lower one,
4429 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004430 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4431 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004432
4433 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4434 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4435 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004436 }
4437
4438 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4439}
4440
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004441/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004442static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4443 EVT SrcVT = SV->getValueType(0);
4444 SDValue V1 = SV->getOperand(0);
4445 DebugLoc dl = SV->getDebugLoc();
4446
4447 int EltNo = SV->getSplatIndex();
4448 int NumElems = SrcVT.getVectorNumElements();
4449 unsigned Size = SrcVT.getSizeInBits();
4450
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004451 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4452 "Unknown how to promote splat for type");
4453
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004454 // Extract the 128-bit part containing the splat element and update
4455 // the splat element index when it refers to the higher register.
4456 if (Size == 256) {
4457 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4458 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4459 if (Idx > 0)
4460 EltNo -= NumElems/2;
4461 }
4462
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004463 // All i16 and i8 vector types can't be used directly by a generic shuffle
4464 // instruction because the target has no such instruction. Generate shuffles
4465 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004466 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004467 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004468 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004469 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004470
4471 // Recreate the 256-bit vector and place the same 128-bit vector
4472 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004473 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004474 if (Size == 256) {
4475 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4476 DAG.getConstant(0, MVT::i32), DAG, dl);
4477 V1 = Insert128BitVector(InsV, V1,
4478 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4479 }
4480
4481 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004482}
4483
Evan Chengba05f722006-04-21 23:03:30 +00004484/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004485/// vector of zero or undef vector. This produces a shuffle where the low
4486/// element of V2 is swizzled into the zero/undef vector, landing at element
4487/// 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 +00004488static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00004489 bool isZero, bool HasSSE2,
4490 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004491 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004492 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00004493 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
4494 unsigned NumElems = VT.getVectorNumElements();
4495 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004496 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004497 // If this is the insertion idx, put the low elt of V2 here.
4498 MaskVec.push_back(i == Idx ? NumElems : i);
4499 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004500}
4501
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004502/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4503/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004504static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4505 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004506 if (Depth == 6)
4507 return SDValue(); // Limit search depth.
4508
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004509 SDValue V = SDValue(N, 0);
4510 EVT VT = V.getValueType();
4511 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004512
4513 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4514 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4515 Index = SV->getMaskElt(Index);
4516
4517 if (Index < 0)
4518 return DAG.getUNDEF(VT.getVectorElementType());
4519
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004520 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004521 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004522 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004523 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004524
4525 // Recurse into target specific vector shuffles to find scalars.
4526 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004527 int NumElems = VT.getVectorNumElements();
4528 SmallVector<unsigned, 16> ShuffleMask;
4529 SDValue ImmN;
4530
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004531 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004532 case X86ISD::SHUFPS:
4533 case X86ISD::SHUFPD:
4534 ImmN = N->getOperand(N->getNumOperands()-1);
4535 DecodeSHUFPSMask(NumElems,
4536 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4537 ShuffleMask);
4538 break;
4539 case X86ISD::PUNPCKHBW:
4540 case X86ISD::PUNPCKHWD:
4541 case X86ISD::PUNPCKHDQ:
4542 case X86ISD::PUNPCKHQDQ:
4543 DecodePUNPCKHMask(NumElems, ShuffleMask);
4544 break;
4545 case X86ISD::UNPCKHPS:
4546 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00004547 case X86ISD::VUNPCKHPSY:
4548 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004549 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4550 break;
4551 case X86ISD::PUNPCKLBW:
4552 case X86ISD::PUNPCKLWD:
4553 case X86ISD::PUNPCKLDQ:
4554 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004555 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004556 break;
4557 case X86ISD::UNPCKLPS:
4558 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004559 case X86ISD::VUNPCKLPSY:
4560 case X86ISD::VUNPCKLPDY:
4561 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004562 break;
4563 case X86ISD::MOVHLPS:
4564 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4565 break;
4566 case X86ISD::MOVLHPS:
4567 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4568 break;
4569 case X86ISD::PSHUFD:
4570 ImmN = N->getOperand(N->getNumOperands()-1);
4571 DecodePSHUFMask(NumElems,
4572 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4573 ShuffleMask);
4574 break;
4575 case X86ISD::PSHUFHW:
4576 ImmN = N->getOperand(N->getNumOperands()-1);
4577 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4578 ShuffleMask);
4579 break;
4580 case X86ISD::PSHUFLW:
4581 ImmN = N->getOperand(N->getNumOperands()-1);
4582 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4583 ShuffleMask);
4584 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004585 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004586 case X86ISD::MOVSD: {
4587 // The index 0 always comes from the first element of the second source,
4588 // this is why MOVSS and MOVSD are used in the first place. The other
4589 // elements come from the other positions of the first source vector.
4590 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004591 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4592 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004593 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004594 case X86ISD::VPERMILPS:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004595 ImmN = N->getOperand(N->getNumOperands()-1);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004596 DecodeVPERMILPSMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004597 ShuffleMask);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004598 break;
4599 case X86ISD::VPERMILPSY:
4600 ImmN = N->getOperand(N->getNumOperands()-1);
4601 DecodeVPERMILPSMask(8, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4602 ShuffleMask);
4603 break;
4604 case X86ISD::VPERMILPD:
4605 ImmN = N->getOperand(N->getNumOperands()-1);
4606 DecodeVPERMILPDMask(2, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4607 ShuffleMask);
4608 break;
4609 case X86ISD::VPERMILPDY:
4610 ImmN = N->getOperand(N->getNumOperands()-1);
4611 DecodeVPERMILPDMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4612 ShuffleMask);
4613 break;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004614 case X86ISD::VPERM2F128:
4615 ImmN = N->getOperand(N->getNumOperands()-1);
4616 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4617 ShuffleMask);
4618 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004619 case X86ISD::MOVDDUP:
4620 case X86ISD::MOVLHPD:
4621 case X86ISD::MOVLPD:
4622 case X86ISD::MOVLPS:
4623 case X86ISD::MOVSHDUP:
4624 case X86ISD::MOVSLDUP:
4625 case X86ISD::PALIGN:
4626 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004627 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004628 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004629 return SDValue();
4630 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004631
4632 Index = ShuffleMask[Index];
4633 if (Index < 0)
4634 return DAG.getUNDEF(VT.getVectorElementType());
4635
4636 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4637 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4638 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004639 }
4640
4641 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004642 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004643 V = V.getOperand(0);
4644 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004645 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004646
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004647 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004648 return SDValue();
4649 }
4650
4651 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4652 return (Index == 0) ? V.getOperand(0)
4653 : DAG.getUNDEF(VT.getVectorElementType());
4654
4655 if (V.getOpcode() == ISD::BUILD_VECTOR)
4656 return V.getOperand(Index);
4657
4658 return SDValue();
4659}
4660
4661/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4662/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004663/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004664static
4665unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4666 bool ZerosFromLeft, SelectionDAG &DAG) {
4667 int i = 0;
4668
4669 while (i < NumElems) {
4670 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004671 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004672 if (!(Elt.getNode() &&
4673 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4674 break;
4675 ++i;
4676 }
4677
4678 return i;
4679}
4680
4681/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4682/// MaskE correspond consecutively to elements from one of the vector operands,
4683/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4684static
4685bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4686 int OpIdx, int NumElems, unsigned &OpNum) {
4687 bool SeenV1 = false;
4688 bool SeenV2 = false;
4689
4690 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4691 int Idx = SVOp->getMaskElt(i);
4692 // Ignore undef indicies
4693 if (Idx < 0)
4694 continue;
4695
4696 if (Idx < NumElems)
4697 SeenV1 = true;
4698 else
4699 SeenV2 = true;
4700
4701 // Only accept consecutive elements from the same vector
4702 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4703 return false;
4704 }
4705
4706 OpNum = SeenV1 ? 0 : 1;
4707 return true;
4708}
4709
4710/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4711/// logical left shift of a vector.
4712static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4713 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4714 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4715 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4716 false /* check zeros from right */, DAG);
4717 unsigned OpSrc;
4718
4719 if (!NumZeros)
4720 return false;
4721
4722 // Considering the elements in the mask that are not consecutive zeros,
4723 // check if they consecutively come from only one of the source vectors.
4724 //
4725 // V1 = {X, A, B, C} 0
4726 // \ \ \ /
4727 // vector_shuffle V1, V2 <1, 2, 3, X>
4728 //
4729 if (!isShuffleMaskConsecutive(SVOp,
4730 0, // Mask Start Index
4731 NumElems-NumZeros-1, // Mask End Index
4732 NumZeros, // Where to start looking in the src vector
4733 NumElems, // Number of elements in vector
4734 OpSrc)) // Which source operand ?
4735 return false;
4736
4737 isLeft = false;
4738 ShAmt = NumZeros;
4739 ShVal = SVOp->getOperand(OpSrc);
4740 return true;
4741}
4742
4743/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4744/// logical left shift of a vector.
4745static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4746 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4747 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4748 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4749 true /* check zeros from left */, DAG);
4750 unsigned OpSrc;
4751
4752 if (!NumZeros)
4753 return false;
4754
4755 // Considering the elements in the mask that are not consecutive zeros,
4756 // check if they consecutively come from only one of the source vectors.
4757 //
4758 // 0 { A, B, X, X } = V2
4759 // / \ / /
4760 // vector_shuffle V1, V2 <X, X, 4, 5>
4761 //
4762 if (!isShuffleMaskConsecutive(SVOp,
4763 NumZeros, // Mask Start Index
4764 NumElems-1, // Mask End Index
4765 0, // Where to start looking in the src vector
4766 NumElems, // Number of elements in vector
4767 OpSrc)) // Which source operand ?
4768 return false;
4769
4770 isLeft = true;
4771 ShAmt = NumZeros;
4772 ShVal = SVOp->getOperand(OpSrc);
4773 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004774}
4775
4776/// isVectorShift - Returns true if the shuffle can be implemented as a
4777/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004778static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004779 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004780 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4781 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4782 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004783
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004784 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004785}
4786
Evan Chengc78d3b42006-04-24 18:01:45 +00004787/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4788///
Dan Gohman475871a2008-07-27 21:46:04 +00004789static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004790 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004791 SelectionDAG &DAG,
4792 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004793 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004794 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004795
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004796 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004797 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004798 bool First = true;
4799 for (unsigned i = 0; i < 16; ++i) {
4800 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4801 if (ThisIsNonZero && First) {
4802 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004803 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004804 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004805 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004806 First = false;
4807 }
4808
4809 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004810 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004811 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4812 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004813 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004814 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004815 }
4816 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004817 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4818 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4819 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004820 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004821 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004822 } else
4823 ThisElt = LastElt;
4824
Gabor Greifba36cb52008-08-28 21:40:38 +00004825 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004826 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004827 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004828 }
4829 }
4830
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004831 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004832}
4833
Bill Wendlinga348c562007-03-22 18:42:45 +00004834/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004835///
Dan Gohman475871a2008-07-27 21:46:04 +00004836static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004837 unsigned NumNonZero, unsigned NumZero,
4838 SelectionDAG &DAG,
4839 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004840 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004841 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004842
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004843 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004844 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004845 bool First = true;
4846 for (unsigned i = 0; i < 8; ++i) {
4847 bool isNonZero = (NonZeros & (1 << i)) != 0;
4848 if (isNonZero) {
4849 if (First) {
4850 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004851 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004852 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004853 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004854 First = false;
4855 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004856 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004857 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004858 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004859 }
4860 }
4861
4862 return V;
4863}
4864
Evan Chengf26ffe92008-05-29 08:22:04 +00004865/// getVShift - Return a vector logical shift node.
4866///
Owen Andersone50ed302009-08-10 22:56:29 +00004867static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004868 unsigned NumBits, SelectionDAG &DAG,
4869 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004870 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004871 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004872 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4873 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004874 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004875 DAG.getConstant(NumBits,
4876 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004877}
4878
Dan Gohman475871a2008-07-27 21:46:04 +00004879SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004880X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004881 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004882
Evan Chengc3630942009-12-09 21:00:30 +00004883 // Check if the scalar load can be widened into a vector load. And if
4884 // the address is "base + cst" see if the cst can be "absorbed" into
4885 // the shuffle mask.
4886 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4887 SDValue Ptr = LD->getBasePtr();
4888 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4889 return SDValue();
4890 EVT PVT = LD->getValueType(0);
4891 if (PVT != MVT::i32 && PVT != MVT::f32)
4892 return SDValue();
4893
4894 int FI = -1;
4895 int64_t Offset = 0;
4896 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4897 FI = FINode->getIndex();
4898 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004899 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004900 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4901 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4902 Offset = Ptr.getConstantOperandVal(1);
4903 Ptr = Ptr.getOperand(0);
4904 } else {
4905 return SDValue();
4906 }
4907
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004908 // FIXME: 256-bit vector instructions don't require a strict alignment,
4909 // improve this code to support it better.
4910 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004911 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004912 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004913 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004914 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004915 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004916 // Can't change the alignment. FIXME: It's possible to compute
4917 // the exact stack offset and reference FI + adjust offset instead.
4918 // If someone *really* cares about this. That's the way to implement it.
4919 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004920 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004921 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004922 }
4923 }
4924
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004925 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004926 // Ptr + (Offset & ~15).
4927 if (Offset < 0)
4928 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004929 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004930 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004931 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004932 if (StartOffset)
4933 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4934 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4935
4936 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004937 int NumElems = VT.getVectorNumElements();
4938
4939 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4940 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4941 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004942 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004943 false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004944
4945 // Canonicalize it to a v4i32 or v8i32 shuffle.
4946 SmallVector<int, 8> Mask;
4947 for (int i = 0; i < NumElems; ++i)
4948 Mask.push_back(EltNo);
4949
4950 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4951 return DAG.getNode(ISD::BITCAST, dl, NVT,
4952 DAG.getVectorShuffle(CanonVT, dl, V1,
4953 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004954 }
4955
4956 return SDValue();
4957}
4958
Michael J. Spencerec38de22010-10-10 22:04:20 +00004959/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4960/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004961/// load which has the same value as a build_vector whose operands are 'elts'.
4962///
4963/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004964///
Nate Begeman1449f292010-03-24 22:19:06 +00004965/// FIXME: we'd also like to handle the case where the last elements are zero
4966/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4967/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004968static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004969 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004970 EVT EltVT = VT.getVectorElementType();
4971 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004972
Nate Begemanfdea31a2010-03-24 20:49:50 +00004973 LoadSDNode *LDBase = NULL;
4974 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004975
Nate Begeman1449f292010-03-24 22:19:06 +00004976 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004977 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004978 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004979 for (unsigned i = 0; i < NumElems; ++i) {
4980 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004981
Nate Begemanfdea31a2010-03-24 20:49:50 +00004982 if (!Elt.getNode() ||
4983 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4984 return SDValue();
4985 if (!LDBase) {
4986 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4987 return SDValue();
4988 LDBase = cast<LoadSDNode>(Elt.getNode());
4989 LastLoadedElt = i;
4990 continue;
4991 }
4992 if (Elt.getOpcode() == ISD::UNDEF)
4993 continue;
4994
4995 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4996 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4997 return SDValue();
4998 LastLoadedElt = i;
4999 }
Nate Begeman1449f292010-03-24 22:19:06 +00005000
5001 // If we have found an entire vector of loads and undefs, then return a large
5002 // load of the entire vector width starting at the base pointer. If we found
5003 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005004 if (LastLoadedElt == NumElems - 1) {
5005 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005006 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005007 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005008 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005009 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005010 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005011 LDBase->isVolatile(), LDBase->isNonTemporal(),
5012 LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00005013 } else if (NumElems == 4 && LastLoadedElt == 1 &&
5014 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005015 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5016 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00005017 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
5018 Ops, 2, MVT::i32,
5019 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005020 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005021 }
5022 return SDValue();
5023}
5024
Evan Chengc3630942009-12-09 21:00:30 +00005025SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005026X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005027 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005028
David Greenef125a292011-02-08 19:04:41 +00005029 EVT VT = Op.getValueType();
5030 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005031 unsigned NumElems = Op.getNumOperands();
5032
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005033 // Vectors containing all zeros can be matched by pxor and xorps later
5034 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5035 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5036 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005037 if (Op.getValueType() == MVT::v4i32 ||
5038 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005039 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005040
Dale Johannesenace16102009-02-03 19:33:06 +00005041 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005042 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005043
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005044 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
5045 // vectors or broken into v4i32 operations on 256-bit vectors.
5046 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
5047 if (Op.getValueType() == MVT::v4i32)
5048 return Op;
5049
5050 return getOnesVector(Op.getValueType(), DAG, dl);
5051 }
5052
Owen Andersone50ed302009-08-10 22:56:29 +00005053 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005054
Evan Cheng0db9fe62006-04-25 20:13:52 +00005055 unsigned NumZero = 0;
5056 unsigned NumNonZero = 0;
5057 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005058 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005059 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005060 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005061 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005062 if (Elt.getOpcode() == ISD::UNDEF)
5063 continue;
5064 Values.insert(Elt);
5065 if (Elt.getOpcode() != ISD::Constant &&
5066 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005067 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005068 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005069 NumZero++;
5070 else {
5071 NonZeros |= (1 << i);
5072 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005073 }
5074 }
5075
Chris Lattner97a2a562010-08-26 05:24:29 +00005076 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5077 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005078 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005079
Chris Lattner67f453a2008-03-09 05:42:06 +00005080 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005081 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005082 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005083 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005084
Chris Lattner62098042008-03-09 01:05:04 +00005085 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5086 // the value are obviously zero, truncate the value to i32 and do the
5087 // insertion that way. Only do this if the value is non-constant or if the
5088 // value is a constant being inserted into element 0. It is cheaper to do
5089 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005090 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005091 (!IsAllConstants || Idx == 0)) {
5092 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005093 // Handle SSE only.
5094 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5095 EVT VecVT = MVT::v4i32;
5096 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005097
Chris Lattner62098042008-03-09 01:05:04 +00005098 // Truncate the value (which may itself be a constant) to i32, and
5099 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005100 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005101 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005102 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
5103 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005104
Chris Lattner62098042008-03-09 01:05:04 +00005105 // Now we have our 32-bit value zero extended in the low element of
5106 // a vector. If Idx != 0, swizzle it into place.
5107 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005108 SmallVector<int, 4> Mask;
5109 Mask.push_back(Idx);
5110 for (unsigned i = 1; i != VecElts; ++i)
5111 Mask.push_back(i);
5112 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005113 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005114 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005115 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005116 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005117 }
5118 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005119
Chris Lattner19f79692008-03-08 22:59:52 +00005120 // If we have a constant or non-constant insertion into the low element of
5121 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5122 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005123 // depending on what the source datatype is.
5124 if (Idx == 0) {
5125 if (NumZero == 0) {
5126 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005127 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5128 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005129 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5130 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
5131 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
5132 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005133 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5134 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005135 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5136 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005137 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5138 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
5139 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005140 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005141 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005142 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005143
5144 // Is it a vector logical left shift?
5145 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005146 X86::isZeroNode(Op.getOperand(0)) &&
5147 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005148 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005149 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005150 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005151 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005152 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005153 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005154
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005155 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005156 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005157
Chris Lattner19f79692008-03-08 22:59:52 +00005158 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5159 // is a non-constant being inserted into an element other than the low one,
5160 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5161 // movd/movss) to move this into the low element, then shuffle it into
5162 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005163 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005164 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005165
Evan Cheng0db9fe62006-04-25 20:13:52 +00005166 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005167 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
5168 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005169 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005170 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005171 MaskVec.push_back(i == Idx ? 0 : 1);
5172 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005173 }
5174 }
5175
Chris Lattner67f453a2008-03-09 05:42:06 +00005176 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005177 if (Values.size() == 1) {
5178 if (EVTBits == 32) {
5179 // Instead of a shuffle like this:
5180 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5181 // Check if it's possible to issue this instead.
5182 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5183 unsigned Idx = CountTrailingZeros_32(NonZeros);
5184 SDValue Item = Op.getOperand(Idx);
5185 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5186 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5187 }
Dan Gohman475871a2008-07-27 21:46:04 +00005188 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005189 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005190
Dan Gohmana3941172007-07-24 22:55:08 +00005191 // A vector full of immediates; various special cases are already
5192 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005193 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005194 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005195
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005196 // For AVX-length vectors, build the individual 128-bit pieces and use
5197 // shuffles to put them in place.
5198 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5199 SmallVector<SDValue, 32> V;
5200 for (unsigned i = 0; i < NumElems; ++i)
5201 V.push_back(Op.getOperand(i));
5202
5203 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5204
5205 // Build both the lower and upper subvector.
5206 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5207 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5208 NumElems/2);
5209
5210 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005211 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5212 DAG.getConstant(0, MVT::i32), DAG, dl);
5213 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005214 DAG, dl);
5215 }
5216
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005217 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005218 if (EVTBits == 64) {
5219 if (NumNonZero == 1) {
5220 // One half is zero or undef.
5221 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005222 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005223 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005224 return getShuffleVectorZeroOrUndef(V2, Idx, true,
5225 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005226 }
Dan Gohman475871a2008-07-27 21:46:04 +00005227 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005228 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005229
5230 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005231 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005232 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005233 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005234 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005235 }
5236
Bill Wendling826f36f2007-03-28 00:57:11 +00005237 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005238 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005239 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005240 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005241 }
5242
5243 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005244 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005245 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005246 if (NumElems == 4 && NumZero > 0) {
5247 for (unsigned i = 0; i < 4; ++i) {
5248 bool isZero = !(NonZeros & (1 << i));
5249 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00005250 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005251 else
Dale Johannesenace16102009-02-03 19:33:06 +00005252 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005253 }
5254
5255 for (unsigned i = 0; i < 2; ++i) {
5256 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5257 default: break;
5258 case 0:
5259 V[i] = V[i*2]; // Must be a zero vector.
5260 break;
5261 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005262 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005263 break;
5264 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005265 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005266 break;
5267 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005268 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005269 break;
5270 }
5271 }
5272
Nate Begeman9008ca62009-04-27 18:41:29 +00005273 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005274 bool Reverse = (NonZeros & 0x3) == 2;
5275 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005276 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005277 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5278 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005279 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5280 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005281 }
5282
Nate Begemanfdea31a2010-03-24 20:49:50 +00005283 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5284 // Check for a build vector of consecutive loads.
5285 for (unsigned i = 0; i < NumElems; ++i)
5286 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005287
Nate Begemanfdea31a2010-03-24 20:49:50 +00005288 // Check for elements which are consecutive loads.
5289 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5290 if (LD.getNode())
5291 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005292
5293 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005294 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005295 SDValue Result;
5296 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5297 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5298 else
5299 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005300
Chris Lattner24faf612010-08-28 17:59:08 +00005301 for (unsigned i = 1; i < NumElems; ++i) {
5302 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5303 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005304 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005305 }
5306 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005307 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005308
Chris Lattner6e80e442010-08-28 17:15:43 +00005309 // Otherwise, expand into a number of unpckl*, start by extending each of
5310 // our (non-undef) elements to the full vector width with the element in the
5311 // bottom slot of the vector (which generates no code for SSE).
5312 for (unsigned i = 0; i < NumElems; ++i) {
5313 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5314 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5315 else
5316 V[i] = DAG.getUNDEF(VT);
5317 }
5318
5319 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005320 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5321 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5322 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005323 unsigned EltStride = NumElems >> 1;
5324 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005325 for (unsigned i = 0; i < EltStride; ++i) {
5326 // If V[i+EltStride] is undef and this is the first round of mixing,
5327 // then it is safe to just drop this shuffle: V[i] is already in the
5328 // right place, the one element (since it's the first round) being
5329 // inserted as undef can be dropped. This isn't safe for successive
5330 // rounds because they will permute elements within both vectors.
5331 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5332 EltStride == NumElems/2)
5333 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005334
Chris Lattner6e80e442010-08-28 17:15:43 +00005335 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005336 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005337 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005338 }
5339 return V[0];
5340 }
Dan Gohman475871a2008-07-27 21:46:04 +00005341 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005342}
5343
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005344// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5345// them in a MMX register. This is better than doing a stack convert.
5346static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005347 DebugLoc dl = Op.getDebugLoc();
5348 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005349
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005350 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5351 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5352 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005353 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005354 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5355 InVec = Op.getOperand(1);
5356 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5357 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005358 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005359 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5360 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5361 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005362 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005363 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5364 Mask[0] = 0; Mask[1] = 2;
5365 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5366 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005367 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005368}
5369
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005370// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5371// to create 256-bit vectors from two other 128-bit ones.
5372static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5373 DebugLoc dl = Op.getDebugLoc();
5374 EVT ResVT = Op.getValueType();
5375
5376 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5377
5378 SDValue V1 = Op.getOperand(0);
5379 SDValue V2 = Op.getOperand(1);
5380 unsigned NumElems = ResVT.getVectorNumElements();
5381
5382 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5383 DAG.getConstant(0, MVT::i32), DAG, dl);
5384 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5385 DAG, dl);
5386}
5387
5388SDValue
5389X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005390 EVT ResVT = Op.getValueType();
5391
5392 assert(Op.getNumOperands() == 2);
5393 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5394 "Unsupported CONCAT_VECTORS for value type");
5395
5396 // We support concatenate two MMX registers and place them in a MMX register.
5397 // This is better than doing a stack convert.
5398 if (ResVT.is128BitVector())
5399 return LowerMMXCONCAT_VECTORS(Op, DAG);
5400
5401 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5402 // from two other 128-bit ones.
5403 return LowerAVXCONCAT_VECTORS(Op, DAG);
5404}
5405
Nate Begemanb9a47b82009-02-23 08:49:38 +00005406// v8i16 shuffles - Prefer shuffles in the following order:
5407// 1. [all] pshuflw, pshufhw, optional move
5408// 2. [ssse3] 1 x pshufb
5409// 3. [ssse3] 2 x pshufb + 1 x por
5410// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005411SDValue
5412X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5413 SelectionDAG &DAG) const {
5414 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005415 SDValue V1 = SVOp->getOperand(0);
5416 SDValue V2 = SVOp->getOperand(1);
5417 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005418 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005419
Nate Begemanb9a47b82009-02-23 08:49:38 +00005420 // Determine if more than 1 of the words in each of the low and high quadwords
5421 // of the result come from the same quadword of one of the two inputs. Undef
5422 // mask values count as coming from any quadword, for better codegen.
5423 SmallVector<unsigned, 4> LoQuad(4);
5424 SmallVector<unsigned, 4> HiQuad(4);
5425 BitVector InputQuads(4);
5426 for (unsigned i = 0; i < 8; ++i) {
5427 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005428 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005429 MaskVals.push_back(EltIdx);
5430 if (EltIdx < 0) {
5431 ++Quad[0];
5432 ++Quad[1];
5433 ++Quad[2];
5434 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005435 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005436 }
5437 ++Quad[EltIdx / 4];
5438 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005439 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005440
Nate Begemanb9a47b82009-02-23 08:49:38 +00005441 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005442 unsigned MaxQuad = 1;
5443 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005444 if (LoQuad[i] > MaxQuad) {
5445 BestLoQuad = i;
5446 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005447 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005448 }
5449
Nate Begemanb9a47b82009-02-23 08:49:38 +00005450 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005451 MaxQuad = 1;
5452 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005453 if (HiQuad[i] > MaxQuad) {
5454 BestHiQuad = i;
5455 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005456 }
5457 }
5458
Nate Begemanb9a47b82009-02-23 08:49:38 +00005459 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005460 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005461 // single pshufb instruction is necessary. If There are more than 2 input
5462 // quads, disable the next transformation since it does not help SSSE3.
5463 bool V1Used = InputQuads[0] || InputQuads[1];
5464 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005465 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005466 if (InputQuads.count() == 2 && V1Used && V2Used) {
5467 BestLoQuad = InputQuads.find_first();
5468 BestHiQuad = InputQuads.find_next(BestLoQuad);
5469 }
5470 if (InputQuads.count() > 2) {
5471 BestLoQuad = -1;
5472 BestHiQuad = -1;
5473 }
5474 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005475
Nate Begemanb9a47b82009-02-23 08:49:38 +00005476 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5477 // the shuffle mask. If a quad is scored as -1, that means that it contains
5478 // words from all 4 input quadwords.
5479 SDValue NewV;
5480 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005481 SmallVector<int, 8> MaskV;
5482 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5483 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005484 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005485 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5486 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5487 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005488
Nate Begemanb9a47b82009-02-23 08:49:38 +00005489 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5490 // source words for the shuffle, to aid later transformations.
5491 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005492 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005493 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005494 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005495 if (idx != (int)i)
5496 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005497 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005498 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005499 AllWordsInNewV = false;
5500 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005501 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005502
Nate Begemanb9a47b82009-02-23 08:49:38 +00005503 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5504 if (AllWordsInNewV) {
5505 for (int i = 0; i != 8; ++i) {
5506 int idx = MaskVals[i];
5507 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005508 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005509 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005510 if ((idx != i) && idx < 4)
5511 pshufhw = false;
5512 if ((idx != i) && idx > 3)
5513 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005514 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005515 V1 = NewV;
5516 V2Used = false;
5517 BestLoQuad = 0;
5518 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005519 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005520
Nate Begemanb9a47b82009-02-23 08:49:38 +00005521 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5522 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005523 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005524 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5525 unsigned TargetMask = 0;
5526 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005527 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005528 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5529 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5530 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005531 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005532 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005533 }
Eric Christopherfd179292009-08-27 18:07:15 +00005534
Nate Begemanb9a47b82009-02-23 08:49:38 +00005535 // If we have SSSE3, and all words of the result are from 1 input vector,
5536 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5537 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005538 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005539 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005540
Nate Begemanb9a47b82009-02-23 08:49:38 +00005541 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005542 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005543 // mask, and elements that come from V1 in the V2 mask, so that the two
5544 // results can be OR'd together.
5545 bool TwoInputs = V1Used && V2Used;
5546 for (unsigned i = 0; i != 8; ++i) {
5547 int EltIdx = MaskVals[i] * 2;
5548 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005549 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5550 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005551 continue;
5552 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005553 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5554 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005555 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005556 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005557 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005558 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005559 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005560 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005561 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005562
Nate Begemanb9a47b82009-02-23 08:49:38 +00005563 // Calculate the shuffle mask for the second input, shuffle it, and
5564 // OR it with the first shuffled input.
5565 pshufbMask.clear();
5566 for (unsigned i = 0; i != 8; ++i) {
5567 int EltIdx = MaskVals[i] * 2;
5568 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005569 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5570 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005571 continue;
5572 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005573 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5574 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005575 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005576 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005577 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005578 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005579 MVT::v16i8, &pshufbMask[0], 16));
5580 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005581 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005582 }
5583
5584 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5585 // and update MaskVals with new element order.
5586 BitVector InOrder(8);
5587 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005588 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005589 for (int i = 0; i != 4; ++i) {
5590 int idx = MaskVals[i];
5591 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005592 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005593 InOrder.set(i);
5594 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005595 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005596 InOrder.set(i);
5597 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005598 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005599 }
5600 }
5601 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005602 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005603 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005604 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005605
5606 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5607 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5608 NewV.getOperand(0),
5609 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5610 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005611 }
Eric Christopherfd179292009-08-27 18:07:15 +00005612
Nate Begemanb9a47b82009-02-23 08:49:38 +00005613 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5614 // and update MaskVals with the new element order.
5615 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005616 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005617 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005618 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005619 for (unsigned i = 4; i != 8; ++i) {
5620 int idx = MaskVals[i];
5621 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005622 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005623 InOrder.set(i);
5624 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005625 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005626 InOrder.set(i);
5627 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005628 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005629 }
5630 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005631 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005632 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005633
5634 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5635 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5636 NewV.getOperand(0),
5637 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5638 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005639 }
Eric Christopherfd179292009-08-27 18:07:15 +00005640
Nate Begemanb9a47b82009-02-23 08:49:38 +00005641 // In case BestHi & BestLo were both -1, which means each quadword has a word
5642 // from each of the four input quadwords, calculate the InOrder bitvector now
5643 // before falling through to the insert/extract cleanup.
5644 if (BestLoQuad == -1 && BestHiQuad == -1) {
5645 NewV = V1;
5646 for (int i = 0; i != 8; ++i)
5647 if (MaskVals[i] < 0 || MaskVals[i] == i)
5648 InOrder.set(i);
5649 }
Eric Christopherfd179292009-08-27 18:07:15 +00005650
Nate Begemanb9a47b82009-02-23 08:49:38 +00005651 // The other elements are put in the right place using pextrw and pinsrw.
5652 for (unsigned i = 0; i != 8; ++i) {
5653 if (InOrder[i])
5654 continue;
5655 int EltIdx = MaskVals[i];
5656 if (EltIdx < 0)
5657 continue;
5658 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005659 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005660 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005661 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005662 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005663 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005664 DAG.getIntPtrConstant(i));
5665 }
5666 return NewV;
5667}
5668
5669// v16i8 shuffles - Prefer shuffles in the following order:
5670// 1. [ssse3] 1 x pshufb
5671// 2. [ssse3] 2 x pshufb + 1 x por
5672// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5673static
Nate Begeman9008ca62009-04-27 18:41:29 +00005674SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005675 SelectionDAG &DAG,
5676 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005677 SDValue V1 = SVOp->getOperand(0);
5678 SDValue V2 = SVOp->getOperand(1);
5679 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005680 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005681 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005682
Nate Begemanb9a47b82009-02-23 08:49:38 +00005683 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005684 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005685 // present, fall back to case 3.
5686 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5687 bool V1Only = true;
5688 bool V2Only = true;
5689 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005690 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005691 if (EltIdx < 0)
5692 continue;
5693 if (EltIdx < 16)
5694 V2Only = false;
5695 else
5696 V1Only = false;
5697 }
Eric Christopherfd179292009-08-27 18:07:15 +00005698
Nate Begemanb9a47b82009-02-23 08:49:38 +00005699 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
5700 if (TLI.getSubtarget()->hasSSSE3()) {
5701 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005702
Nate Begemanb9a47b82009-02-23 08:49:38 +00005703 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005704 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005705 //
5706 // Otherwise, we have elements from both input vectors, and must zero out
5707 // elements that come from V2 in the first mask, and V1 in the second mask
5708 // so that we can OR them together.
5709 bool TwoInputs = !(V1Only || V2Only);
5710 for (unsigned i = 0; i != 16; ++i) {
5711 int EltIdx = MaskVals[i];
5712 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005713 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005714 continue;
5715 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005716 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005717 }
5718 // If all the elements are from V2, assign it to V1 and return after
5719 // building the first pshufb.
5720 if (V2Only)
5721 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005722 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005723 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005724 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005725 if (!TwoInputs)
5726 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005727
Nate Begemanb9a47b82009-02-23 08:49:38 +00005728 // Calculate the shuffle mask for the second input, shuffle it, and
5729 // OR it with the first shuffled input.
5730 pshufbMask.clear();
5731 for (unsigned i = 0; i != 16; ++i) {
5732 int EltIdx = MaskVals[i];
5733 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005734 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005735 continue;
5736 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005737 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005738 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005739 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005740 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005741 MVT::v16i8, &pshufbMask[0], 16));
5742 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005743 }
Eric Christopherfd179292009-08-27 18:07:15 +00005744
Nate Begemanb9a47b82009-02-23 08:49:38 +00005745 // No SSSE3 - Calculate in place words and then fix all out of place words
5746 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5747 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005748 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5749 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005750 SDValue NewV = V2Only ? V2 : V1;
5751 for (int i = 0; i != 8; ++i) {
5752 int Elt0 = MaskVals[i*2];
5753 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005754
Nate Begemanb9a47b82009-02-23 08:49:38 +00005755 // This word of the result is all undef, skip it.
5756 if (Elt0 < 0 && Elt1 < 0)
5757 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005758
Nate Begemanb9a47b82009-02-23 08:49:38 +00005759 // This word of the result is already in the correct place, skip it.
5760 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5761 continue;
5762 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5763 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005764
Nate Begemanb9a47b82009-02-23 08:49:38 +00005765 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5766 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5767 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005768
5769 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5770 // using a single extract together, load it and store it.
5771 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005772 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005773 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005774 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005775 DAG.getIntPtrConstant(i));
5776 continue;
5777 }
5778
Nate Begemanb9a47b82009-02-23 08:49:38 +00005779 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005780 // source byte is not also odd, shift the extracted word left 8 bits
5781 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005782 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005783 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005784 DAG.getIntPtrConstant(Elt1 / 2));
5785 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005786 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005787 DAG.getConstant(8,
5788 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005789 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005790 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5791 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005792 }
5793 // If Elt0 is defined, extract it from the appropriate source. If the
5794 // source byte is not also even, shift the extracted word right 8 bits. If
5795 // Elt1 was also defined, OR the extracted values together before
5796 // inserting them in the result.
5797 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005798 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005799 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5800 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005801 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005802 DAG.getConstant(8,
5803 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005804 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005805 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5806 DAG.getConstant(0x00FF, MVT::i16));
5807 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005808 : InsElt0;
5809 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005810 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005811 DAG.getIntPtrConstant(i));
5812 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005813 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005814}
5815
Evan Cheng7a831ce2007-12-15 03:00:47 +00005816/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005817/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005818/// done when every pair / quad of shuffle mask elements point to elements in
5819/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005820/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005821static
Nate Begeman9008ca62009-04-27 18:41:29 +00005822SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005823 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005824 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005825 SDValue V1 = SVOp->getOperand(0);
5826 SDValue V2 = SVOp->getOperand(1);
5827 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005828 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005829 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005830 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005831 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005832 case MVT::v4f32: NewVT = MVT::v2f64; break;
5833 case MVT::v4i32: NewVT = MVT::v2i64; break;
5834 case MVT::v8i16: NewVT = MVT::v4i32; break;
5835 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005836 }
5837
Nate Begeman9008ca62009-04-27 18:41:29 +00005838 int Scale = NumElems / NewWidth;
5839 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005840 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005841 int StartIdx = -1;
5842 for (int j = 0; j < Scale; ++j) {
5843 int EltIdx = SVOp->getMaskElt(i+j);
5844 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005845 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005846 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005847 StartIdx = EltIdx - (EltIdx % Scale);
5848 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005849 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005850 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005851 if (StartIdx == -1)
5852 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005853 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005854 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005855 }
5856
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005857 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5858 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005859 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005860}
5861
Evan Chengd880b972008-05-09 21:53:03 +00005862/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005863///
Owen Andersone50ed302009-08-10 22:56:29 +00005864static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005865 SDValue SrcOp, SelectionDAG &DAG,
5866 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005867 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005868 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005869 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005870 LD = dyn_cast<LoadSDNode>(SrcOp);
5871 if (!LD) {
5872 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5873 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005874 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005875 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005876 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005877 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005878 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005879 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005880 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005881 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005882 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5883 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5884 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005885 SrcOp.getOperand(0)
5886 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005887 }
5888 }
5889 }
5890
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005891 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005892 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005893 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005894 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005895}
5896
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005897/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5898/// shuffle node referes to only one lane in the sources.
5899static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5900 EVT VT = SVOp->getValueType(0);
5901 int NumElems = VT.getVectorNumElements();
5902 int HalfSize = NumElems/2;
5903 SmallVector<int, 16> M;
5904 SVOp->getMask(M);
5905 bool MatchA = false, MatchB = false;
5906
5907 for (int l = 0; l < NumElems*2; l += HalfSize) {
5908 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5909 MatchA = true;
5910 break;
5911 }
5912 }
5913
5914 for (int l = 0; l < NumElems*2; l += HalfSize) {
5915 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
5916 MatchB = true;
5917 break;
5918 }
5919 }
5920
5921 return MatchA && MatchB;
5922}
5923
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005924/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5925/// which could not be matched by any known target speficic shuffle
5926static SDValue
5927LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005928 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
5929 // If each half of a vector shuffle node referes to only one lane in the
5930 // source vectors, extract each used 128-bit lane and shuffle them using
5931 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
5932 // the work to the legalizer.
5933 DebugLoc dl = SVOp->getDebugLoc();
5934 EVT VT = SVOp->getValueType(0);
5935 int NumElems = VT.getVectorNumElements();
5936 int HalfSize = NumElems/2;
5937
5938 // Extract the reference for each half
5939 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
5940 int FstVecOpNum = 0, SndVecOpNum = 0;
5941 for (int i = 0; i < HalfSize; ++i) {
5942 int Elt = SVOp->getMaskElt(i);
5943 if (SVOp->getMaskElt(i) < 0)
5944 continue;
5945 FstVecOpNum = Elt/NumElems;
5946 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5947 break;
5948 }
5949 for (int i = HalfSize; i < NumElems; ++i) {
5950 int Elt = SVOp->getMaskElt(i);
5951 if (SVOp->getMaskElt(i) < 0)
5952 continue;
5953 SndVecOpNum = Elt/NumElems;
5954 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5955 break;
5956 }
5957
5958 // Extract the subvectors
5959 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
5960 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
5961 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
5962 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
5963
5964 // Generate 128-bit shuffles
5965 SmallVector<int, 16> MaskV1, MaskV2;
5966 for (int i = 0; i < HalfSize; ++i) {
5967 int Elt = SVOp->getMaskElt(i);
5968 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5969 }
5970 for (int i = HalfSize; i < NumElems; ++i) {
5971 int Elt = SVOp->getMaskElt(i);
5972 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5973 }
5974
5975 EVT NVT = V1.getValueType();
5976 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
5977 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
5978
5979 // Concatenate the result back
5980 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
5981 DAG.getConstant(0, MVT::i32), DAG, dl);
5982 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5983 DAG, dl);
5984 }
5985
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005986 return SDValue();
5987}
5988
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005989/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
5990/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00005991static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005992LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005993 SDValue V1 = SVOp->getOperand(0);
5994 SDValue V2 = SVOp->getOperand(1);
5995 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005996 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005997
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00005998 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
5999
Evan Chengace3c172008-07-22 21:13:36 +00006000 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00006001 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006002 SmallVector<int, 8> Mask1(4U, -1);
6003 SmallVector<int, 8> PermMask;
6004 SVOp->getMask(PermMask);
6005
Evan Chengace3c172008-07-22 21:13:36 +00006006 unsigned NumHi = 0;
6007 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006008 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006009 int Idx = PermMask[i];
6010 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006011 Locs[i] = std::make_pair(-1, -1);
6012 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006013 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6014 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006015 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006016 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006017 NumLo++;
6018 } else {
6019 Locs[i] = std::make_pair(1, NumHi);
6020 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006021 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006022 NumHi++;
6023 }
6024 }
6025 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006026
Evan Chengace3c172008-07-22 21:13:36 +00006027 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006028 // If no more than two elements come from either vector. This can be
6029 // implemented with two shuffles. First shuffle gather the elements.
6030 // The second shuffle, which takes the first shuffle as both of its
6031 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006032 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006033
Nate Begeman9008ca62009-04-27 18:41:29 +00006034 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006035
Evan Chengace3c172008-07-22 21:13:36 +00006036 for (unsigned i = 0; i != 4; ++i) {
6037 if (Locs[i].first == -1)
6038 continue;
6039 else {
6040 unsigned Idx = (i < 2) ? 0 : 4;
6041 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006042 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006043 }
6044 }
6045
Nate Begeman9008ca62009-04-27 18:41:29 +00006046 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006047 } else if (NumLo == 3 || NumHi == 3) {
6048 // Otherwise, we must have three elements from one vector, call it X, and
6049 // one element from the other, call it Y. First, use a shufps to build an
6050 // intermediate vector with the one element from Y and the element from X
6051 // that will be in the same half in the final destination (the indexes don't
6052 // matter). Then, use a shufps to build the final vector, taking the half
6053 // containing the element from Y from the intermediate, and the other half
6054 // from X.
6055 if (NumHi == 3) {
6056 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00006057 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006058 std::swap(V1, V2);
6059 }
6060
6061 // Find the element from V2.
6062 unsigned HiIndex;
6063 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006064 int Val = PermMask[HiIndex];
6065 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006066 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006067 if (Val >= 4)
6068 break;
6069 }
6070
Nate Begeman9008ca62009-04-27 18:41:29 +00006071 Mask1[0] = PermMask[HiIndex];
6072 Mask1[1] = -1;
6073 Mask1[2] = PermMask[HiIndex^1];
6074 Mask1[3] = -1;
6075 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006076
6077 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006078 Mask1[0] = PermMask[0];
6079 Mask1[1] = PermMask[1];
6080 Mask1[2] = HiIndex & 1 ? 6 : 4;
6081 Mask1[3] = HiIndex & 1 ? 4 : 6;
6082 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006083 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006084 Mask1[0] = HiIndex & 1 ? 2 : 0;
6085 Mask1[1] = HiIndex & 1 ? 0 : 2;
6086 Mask1[2] = PermMask[2];
6087 Mask1[3] = PermMask[3];
6088 if (Mask1[2] >= 0)
6089 Mask1[2] += 4;
6090 if (Mask1[3] >= 0)
6091 Mask1[3] += 4;
6092 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006093 }
Evan Chengace3c172008-07-22 21:13:36 +00006094 }
6095
6096 // Break it into (shuffle shuffle_hi, shuffle_lo).
6097 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006098 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006099 SmallVector<int,8> LoMask(4U, -1);
6100 SmallVector<int,8> HiMask(4U, -1);
6101
6102 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006103 unsigned MaskIdx = 0;
6104 unsigned LoIdx = 0;
6105 unsigned HiIdx = 2;
6106 for (unsigned i = 0; i != 4; ++i) {
6107 if (i == 2) {
6108 MaskPtr = &HiMask;
6109 MaskIdx = 1;
6110 LoIdx = 0;
6111 HiIdx = 2;
6112 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006113 int Idx = PermMask[i];
6114 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006115 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006116 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006117 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006118 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006119 LoIdx++;
6120 } else {
6121 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006122 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006123 HiIdx++;
6124 }
6125 }
6126
Nate Begeman9008ca62009-04-27 18:41:29 +00006127 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6128 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6129 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006130 for (unsigned i = 0; i != 4; ++i) {
6131 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006132 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006133 } else {
6134 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006135 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006136 }
6137 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006138 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006139}
6140
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006141static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006142 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006143 V = V.getOperand(0);
6144 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6145 V = V.getOperand(0);
6146 if (MayFoldLoad(V))
6147 return true;
6148 return false;
6149}
6150
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006151// FIXME: the version above should always be used. Since there's
6152// a bug where several vector shuffles can't be folded because the
6153// DAG is not updated during lowering and a node claims to have two
6154// uses while it only has one, use this version, and let isel match
6155// another instruction if the load really happens to have more than
6156// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006157// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006158static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006159 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006160 V = V.getOperand(0);
6161 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6162 V = V.getOperand(0);
6163 if (ISD::isNormalLoad(V.getNode()))
6164 return true;
6165 return false;
6166}
6167
6168/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6169/// a vector extract, and if both can be later optimized into a single load.
6170/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6171/// here because otherwise a target specific shuffle node is going to be
6172/// emitted for this shuffle, and the optimization not done.
6173/// FIXME: This is probably not the best approach, but fix the problem
6174/// until the right path is decided.
6175static
6176bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6177 const TargetLowering &TLI) {
6178 EVT VT = V.getValueType();
6179 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6180
6181 // Be sure that the vector shuffle is present in a pattern like this:
6182 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6183 if (!V.hasOneUse())
6184 return false;
6185
6186 SDNode *N = *V.getNode()->use_begin();
6187 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6188 return false;
6189
6190 SDValue EltNo = N->getOperand(1);
6191 if (!isa<ConstantSDNode>(EltNo))
6192 return false;
6193
6194 // If the bit convert changed the number of elements, it is unsafe
6195 // to examine the mask.
6196 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006197 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006198 EVT SrcVT = V.getOperand(0).getValueType();
6199 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6200 return false;
6201 V = V.getOperand(0);
6202 HasShuffleIntoBitcast = true;
6203 }
6204
6205 // Select the input vector, guarding against out of range extract vector.
6206 unsigned NumElems = VT.getVectorNumElements();
6207 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6208 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6209 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6210
6211 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006212 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006213 V = V.getOperand(0);
6214
6215 if (ISD::isNormalLoad(V.getNode())) {
6216 // Is the original load suitable?
6217 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6218
6219 // FIXME: avoid the multi-use bug that is preventing lots of
6220 // of foldings to be detected, this is still wrong of course, but
6221 // give the temporary desired behavior, and if it happens that
6222 // the load has real more uses, during isel it will not fold, and
6223 // will generate poor code.
6224 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6225 return false;
6226
6227 if (!HasShuffleIntoBitcast)
6228 return true;
6229
6230 // If there's a bitcast before the shuffle, check if the load type and
6231 // alignment is valid.
6232 unsigned Align = LN0->getAlignment();
6233 unsigned NewAlign =
6234 TLI.getTargetData()->getABITypeAlignment(
6235 VT.getTypeForEVT(*DAG.getContext()));
6236
6237 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6238 return false;
6239 }
6240
6241 return true;
6242}
6243
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006244static
Evan Cheng835580f2010-10-07 20:50:20 +00006245SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6246 EVT VT = Op.getValueType();
6247
6248 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006249 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6250 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006251 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6252 V1, DAG));
6253}
6254
6255static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006256SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
6257 bool HasSSE2) {
6258 SDValue V1 = Op.getOperand(0);
6259 SDValue V2 = Op.getOperand(1);
6260 EVT VT = Op.getValueType();
6261
6262 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6263
6264 if (HasSSE2 && VT == MVT::v2f64)
6265 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6266
Evan Cheng0899f5c2011-08-31 02:05:24 +00006267 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6268 return DAG.getNode(ISD::BITCAST, dl, VT,
6269 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6270 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6271 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006272}
6273
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006274static
6275SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6276 SDValue V1 = Op.getOperand(0);
6277 SDValue V2 = Op.getOperand(1);
6278 EVT VT = Op.getValueType();
6279
6280 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6281 "unsupported shuffle type");
6282
6283 if (V2.getOpcode() == ISD::UNDEF)
6284 V2 = V1;
6285
6286 // v4i32 or v4f32
6287 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6288}
6289
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006290static inline unsigned getSHUFPOpcode(EVT VT) {
6291 switch(VT.getSimpleVT().SimpleTy) {
6292 case MVT::v8i32: // Use fp unit for int unpack.
6293 case MVT::v8f32:
6294 case MVT::v4i32: // Use fp unit for int unpack.
6295 case MVT::v4f32: return X86ISD::SHUFPS;
6296 case MVT::v4i64: // Use fp unit for int unpack.
6297 case MVT::v4f64:
6298 case MVT::v2i64: // Use fp unit for int unpack.
6299 case MVT::v2f64: return X86ISD::SHUFPD;
6300 default:
6301 llvm_unreachable("Unknown type for shufp*");
6302 }
6303 return 0;
6304}
6305
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006306static
6307SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
6308 SDValue V1 = Op.getOperand(0);
6309 SDValue V2 = Op.getOperand(1);
6310 EVT VT = Op.getValueType();
6311 unsigned NumElems = VT.getVectorNumElements();
6312
6313 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6314 // operand of these instructions is only memory, so check if there's a
6315 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6316 // same masks.
6317 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006318
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006319 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006320 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006321 CanFoldLoad = true;
6322
6323 // When V1 is a load, it can be folded later into a store in isel, example:
6324 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6325 // turns into:
6326 // (MOVLPSmr addr:$src1, VR128:$src2)
6327 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006328 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006329 CanFoldLoad = true;
6330
Eric Christopher893a8822011-02-20 05:04:42 +00006331 // Both of them can't be memory operations though.
6332 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
6333 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00006334
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006335 if (CanFoldLoad) {
6336 if (HasSSE2 && NumElems == 2)
6337 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6338
6339 if (NumElems == 4)
6340 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
6341 }
6342
6343 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6344 // movl and movlp will both match v2i64, but v2i64 is never matched by
6345 // movl earlier because we make it strict to avoid messing with the movlp load
6346 // folding logic (see the code above getMOVLP call). Match it here then,
6347 // this is horrible, but will stay like this until we move all shuffle
6348 // matching to x86 specific nodes. Note that for the 1st condition all
6349 // types are matched with movsd.
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006350 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006351 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6352 // as to remove this logic from here, as much as possible
6353 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006354 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006355 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006356 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006357
6358 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6359
6360 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006361 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006362 X86::getShuffleSHUFImmediate(SVOp), DAG);
6363}
6364
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006365static inline unsigned getUNPCKLOpcode(EVT VT) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006366 switch(VT.getSimpleVT().SimpleTy) {
6367 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
6368 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006369 case MVT::v4f32: return X86ISD::UNPCKLPS;
6370 case MVT::v2f64: return X86ISD::UNPCKLPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006371 case MVT::v8i32: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006372 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006373 case MVT::v4i64: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006374 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006375 case MVT::v16i8: return X86ISD::PUNPCKLBW;
6376 case MVT::v8i16: return X86ISD::PUNPCKLWD;
6377 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006378 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006379 }
6380 return 0;
6381}
6382
6383static inline unsigned getUNPCKHOpcode(EVT VT) {
6384 switch(VT.getSimpleVT().SimpleTy) {
6385 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
6386 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
6387 case MVT::v4f32: return X86ISD::UNPCKHPS;
6388 case MVT::v2f64: return X86ISD::UNPCKHPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006389 case MVT::v8i32: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006390 case MVT::v8f32: return X86ISD::VUNPCKHPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006391 case MVT::v4i64: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006392 case MVT::v4f64: return X86ISD::VUNPCKHPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006393 case MVT::v16i8: return X86ISD::PUNPCKHBW;
6394 case MVT::v8i16: return X86ISD::PUNPCKHWD;
6395 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006396 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006397 }
6398 return 0;
6399}
6400
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006401static inline unsigned getVPERMILOpcode(EVT VT) {
6402 switch(VT.getSimpleVT().SimpleTy) {
6403 case MVT::v4i32:
6404 case MVT::v4f32: return X86ISD::VPERMILPS;
6405 case MVT::v2i64:
6406 case MVT::v2f64: return X86ISD::VPERMILPD;
6407 case MVT::v8i32:
6408 case MVT::v8f32: return X86ISD::VPERMILPSY;
6409 case MVT::v4i64:
6410 case MVT::v4f64: return X86ISD::VPERMILPDY;
6411 default:
6412 llvm_unreachable("Unknown type for vpermil");
6413 }
6414 return 0;
6415}
6416
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006417/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
6418/// a vbroadcast node. The nodes are suitable whenever we can fold a load coming
6419/// from a 32 or 64 bit scalar. Update Op to the desired load to be folded.
6420static bool isVectorBroadcast(SDValue &Op) {
6421 EVT VT = Op.getValueType();
6422 bool Is256 = VT.getSizeInBits() == 256;
6423
6424 assert((VT.getSizeInBits() == 128 || Is256) &&
6425 "Unsupported type for vbroadcast node");
6426
6427 SDValue V = Op;
6428 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
6429 V = V.getOperand(0);
6430
6431 if (Is256 && !(V.hasOneUse() &&
6432 V.getOpcode() == ISD::INSERT_SUBVECTOR &&
6433 V.getOperand(0).getOpcode() == ISD::UNDEF))
6434 return false;
6435
6436 if (Is256)
6437 V = V.getOperand(1);
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006438
6439 if (!V.hasOneUse())
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006440 return false;
6441
6442 // Check the source scalar_to_vector type. 256-bit broadcasts are
6443 // supported for 32/64-bit sizes, while 128-bit ones are only supported
6444 // for 32-bit scalars.
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006445 if (V.getOpcode() != ISD::SCALAR_TO_VECTOR)
6446 return false;
6447
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006448 unsigned ScalarSize = V.getOperand(0).getValueType().getSizeInBits();
6449 if (ScalarSize != 32 && ScalarSize != 64)
6450 return false;
6451 if (!Is256 && ScalarSize == 64)
6452 return false;
6453
6454 V = V.getOperand(0);
6455 if (!MayFoldLoad(V))
6456 return false;
6457
6458 // Return the load node
6459 Op = V;
6460 return true;
6461}
6462
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006463static
6464SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006465 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006466 const X86Subtarget *Subtarget) {
6467 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6468 EVT VT = Op.getValueType();
6469 DebugLoc dl = Op.getDebugLoc();
6470 SDValue V1 = Op.getOperand(0);
6471 SDValue V2 = Op.getOperand(1);
6472
6473 if (isZeroShuffle(SVOp))
6474 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
6475
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006476 // Handle splat operations
6477 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006478 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006479 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006480 // Special case, this is the only place now where it's allowed to return
6481 // a vector_shuffle operation without using a target specific node, because
6482 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6483 // this be moved to DAGCombine instead?
6484 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006485 return Op;
6486
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006487 // Use vbroadcast whenever the splat comes from a foldable load
6488 if (Subtarget->hasAVX() && isVectorBroadcast(V1))
6489 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, V1);
6490
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006491 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006492 if ((Size == 128 && NumElem <= 4) ||
6493 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006494 return SDValue();
6495
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006496 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006497 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006498 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006499
6500 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6501 // do it!
6502 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6503 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6504 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006505 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006506 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
6507 // FIXME: Figure out a cleaner way to do this.
6508 // Try to make use of movq to zero out the top part.
6509 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6510 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6511 if (NewOp.getNode()) {
6512 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6513 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6514 DAG, Subtarget, dl);
6515 }
6516 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6517 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6518 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6519 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6520 DAG, Subtarget, dl);
6521 }
6522 }
6523 return SDValue();
6524}
6525
Dan Gohman475871a2008-07-27 21:46:04 +00006526SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006527X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006528 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006529 SDValue V1 = Op.getOperand(0);
6530 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006531 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006532 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006533 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006534 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006535 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
6536 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006537 bool V1IsSplat = false;
6538 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006539 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006540 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006541 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006542 MachineFunction &MF = DAG.getMachineFunction();
6543 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006544
Dale Johannesen0488fb62010-09-30 23:57:10 +00006545 // Shuffle operations on MMX not supported.
6546 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006547 return Op;
6548
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006549 // Vector shuffle lowering takes 3 steps:
6550 //
6551 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6552 // narrowing and commutation of operands should be handled.
6553 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6554 // shuffle nodes.
6555 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6556 // so the shuffle can be broken into other shuffles and the legalizer can
6557 // try the lowering again.
6558 //
6559 // The general ideia is that no vector_shuffle operation should be left to
6560 // be matched during isel, all of them must be converted to a target specific
6561 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006562
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006563 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6564 // narrowing and commutation of operands should be handled. The actual code
6565 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006566 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006567 if (NewOp.getNode())
6568 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006569
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006570 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6571 // unpckh_undef). Only use pshufd if speed is more important than size.
6572 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006573 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006574 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006575 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006576
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006577 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00006578 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006579 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006580
Dale Johannesen0488fb62010-09-30 23:57:10 +00006581 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006582 return getMOVHighToLow(Op, dl, DAG);
6583
6584 // Use to match splats
6585 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
6586 (VT == MVT::v2f64 || VT == MVT::v2i64))
6587 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
6588
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006589 if (X86::isPSHUFDMask(SVOp)) {
6590 // The actual implementation will match the mask in the if above and then
6591 // during isel it can match several different instructions, not only pshufd
6592 // as its name says, sad but true, emulate the behavior for now...
6593 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6594 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6595
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006596 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6597
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006598 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006599 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6600
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006601 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6602 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006603 }
Eric Christopherfd179292009-08-27 18:07:15 +00006604
Evan Chengf26ffe92008-05-29 08:22:04 +00006605 // Check if this can be converted into a logical shift.
6606 bool isLeft = false;
6607 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006608 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00006609 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00006610 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006611 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006612 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006613 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006614 EVT EltVT = VT.getVectorElementType();
6615 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006616 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006617 }
Eric Christopherfd179292009-08-27 18:07:15 +00006618
Nate Begeman9008ca62009-04-27 18:41:29 +00006619 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006620 if (V1IsUndef)
6621 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00006622 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006623 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006624 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006625 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006626 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6627
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006628 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006629 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6630 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006631 }
Eric Christopherfd179292009-08-27 18:07:15 +00006632
Nate Begeman9008ca62009-04-27 18:41:29 +00006633 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00006634 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
6635 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006636
Dale Johannesen0488fb62010-09-30 23:57:10 +00006637 if (X86::isMOVHLPSMask(SVOp))
6638 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006639
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006640 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006641 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006642
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006643 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006644 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006645
Dale Johannesen0488fb62010-09-30 23:57:10 +00006646 if (X86::isMOVLPMask(SVOp))
6647 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006648
Nate Begeman9008ca62009-04-27 18:41:29 +00006649 if (ShouldXformToMOVHLPS(SVOp) ||
6650 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6651 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006652
Evan Chengf26ffe92008-05-29 08:22:04 +00006653 if (isShift) {
6654 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006655 EVT EltVT = VT.getVectorElementType();
6656 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006657 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006658 }
Eric Christopherfd179292009-08-27 18:07:15 +00006659
Evan Cheng9eca5e82006-10-25 21:49:50 +00006660 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006661 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6662 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006663 V1IsSplat = isSplatVector(V1.getNode());
6664 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006665
Chris Lattner8a594482007-11-25 00:24:49 +00006666 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00006667 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006668 Op = CommuteVectorShuffle(SVOp, DAG);
6669 SVOp = cast<ShuffleVectorSDNode>(Op);
6670 V1 = SVOp->getOperand(0);
6671 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006672 std::swap(V1IsSplat, V2IsSplat);
6673 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006674 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006675 }
6676
Nate Begeman9008ca62009-04-27 18:41:29 +00006677 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6678 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006679 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006680 return V1;
6681 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6682 // the instruction selector will not match, so get a canonical MOVL with
6683 // swapped operands to undo the commute.
6684 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006685 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006686
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006687 if (X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006688 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006689
6690 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006691 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006692
Evan Cheng9bbbb982006-10-25 20:48:19 +00006693 if (V2IsSplat) {
6694 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006695 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006696 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006697 SDValue NewMask = NormalizeMask(SVOp, DAG);
6698 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6699 if (NSVOp != SVOp) {
6700 if (X86::isUNPCKLMask(NSVOp, true)) {
6701 return NewMask;
6702 } else if (X86::isUNPCKHMask(NSVOp, true)) {
6703 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006704 }
6705 }
6706 }
6707
Evan Cheng9eca5e82006-10-25 21:49:50 +00006708 if (Commuted) {
6709 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006710 // FIXME: this seems wrong.
6711 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6712 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006713
6714 if (X86::isUNPCKLMask(NewSVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006715 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006716
6717 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006718 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006719 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006720
Nate Begeman9008ca62009-04-27 18:41:29 +00006721 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006722 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006723 return CommuteVectorShuffle(SVOp, DAG);
6724
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006725 // The checks below are all present in isShuffleMaskLegal, but they are
6726 // inlined here right now to enable us to directly emit target specific
6727 // nodes, and remove one by one until they don't return Op anymore.
6728 SmallVector<int, 16> M;
6729 SVOp->getMask(M);
6730
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006731 if (isPALIGNRMask(M, VT, HasSSSE3))
6732 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6733 X86::getShufflePALIGNRImmediate(SVOp),
6734 DAG);
6735
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006736 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6737 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006738 if (VT == MVT::v2f64)
6739 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006740 if (VT == MVT::v2i64)
6741 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6742 }
6743
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006744 if (isPSHUFHWMask(M, VT))
6745 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6746 X86::getShufflePSHUFHWImmediate(SVOp),
6747 DAG);
6748
6749 if (isPSHUFLWMask(M, VT))
6750 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6751 X86::getShufflePSHUFLWImmediate(SVOp),
6752 DAG);
6753
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006754 if (isSHUFPMask(M, VT))
6755 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6756 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006757
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006758 if (X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006759 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006760 if (X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006761 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006762
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006763 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006764 // Generate target specific nodes for 128 or 256-bit shuffles only
6765 // supported in the AVX instruction set.
6766 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006767
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006768 // Handle VMOVDDUPY permutations
6769 if (isMOVDDUPYMask(SVOp, Subtarget))
6770 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6771
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006772 // Handle VPERMILPS* permutations
6773 if (isVPERMILPSMask(M, VT, Subtarget))
6774 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6775 getShuffleVPERMILPSImmediate(SVOp), DAG);
6776
6777 // Handle VPERMILPD* permutations
6778 if (isVPERMILPDMask(M, VT, Subtarget))
6779 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6780 getShuffleVPERMILPDImmediate(SVOp), DAG);
6781
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00006782 // Handle VPERM2F128 permutations
6783 if (isVPERM2F128Mask(M, VT, Subtarget))
6784 return getTargetShuffleNode(X86ISD::VPERM2F128, dl, VT, V1, V2,
6785 getShuffleVPERM2F128Immediate(SVOp), DAG);
6786
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006787 // Handle VSHUFPSY permutations
6788 if (isVSHUFPSYMask(M, VT, Subtarget))
6789 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6790 getShuffleVSHUFPSYImmediate(SVOp), DAG);
6791
6792 // Handle VSHUFPDY permutations
6793 if (isVSHUFPDYMask(M, VT, Subtarget))
6794 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6795 getShuffleVSHUFPDYImmediate(SVOp), DAG);
6796
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006797 //===--------------------------------------------------------------------===//
6798 // Since no target specific shuffle was selected for this generic one,
6799 // lower it into other known shuffles. FIXME: this isn't true yet, but
6800 // this is the plan.
6801 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006802
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006803 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6804 if (VT == MVT::v8i16) {
6805 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6806 if (NewOp.getNode())
6807 return NewOp;
6808 }
6809
6810 if (VT == MVT::v16i8) {
6811 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6812 if (NewOp.getNode())
6813 return NewOp;
6814 }
6815
6816 // Handle all 128-bit wide vectors with 4 elements, and match them with
6817 // several different shuffle types.
6818 if (NumElems == 4 && VT.getSizeInBits() == 128)
6819 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6820
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006821 // Handle general 256-bit shuffles
6822 if (VT.is256BitVector())
6823 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6824
Dan Gohman475871a2008-07-27 21:46:04 +00006825 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006826}
6827
Dan Gohman475871a2008-07-27 21:46:04 +00006828SDValue
6829X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006830 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006831 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006832 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006833
6834 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6835 return SDValue();
6836
Duncan Sands83ec4b62008-06-06 12:08:01 +00006837 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006838 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006839 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006840 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006841 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006842 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006843 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006844 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6845 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6846 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006847 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6848 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006849 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006850 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006851 Op.getOperand(0)),
6852 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006853 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006854 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006855 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006856 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006857 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006858 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006859 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6860 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006861 // result has a single use which is a store or a bitcast to i32. And in
6862 // the case of a store, it's not worth it if the index is a constant 0,
6863 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006864 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006865 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006866 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006867 if ((User->getOpcode() != ISD::STORE ||
6868 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6869 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006870 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006871 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006872 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006873 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006874 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006875 Op.getOperand(0)),
6876 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006877 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006878 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006879 // ExtractPS works with constant index.
6880 if (isa<ConstantSDNode>(Op.getOperand(1)))
6881 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006882 }
Dan Gohman475871a2008-07-27 21:46:04 +00006883 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006884}
6885
6886
Dan Gohman475871a2008-07-27 21:46:04 +00006887SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006888X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6889 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006890 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006891 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006892
David Greene74a579d2011-02-10 16:57:36 +00006893 SDValue Vec = Op.getOperand(0);
6894 EVT VecVT = Vec.getValueType();
6895
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006896 // If this is a 256-bit vector result, first extract the 128-bit vector and
6897 // then extract the element from the 128-bit vector.
6898 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006899 DebugLoc dl = Op.getNode()->getDebugLoc();
6900 unsigned NumElems = VecVT.getVectorNumElements();
6901 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006902 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6903
6904 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006905 bool Upper = IdxVal >= NumElems/2;
6906 Vec = Extract128BitVector(Vec,
6907 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006908
David Greene74a579d2011-02-10 16:57:36 +00006909 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006910 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006911 }
6912
6913 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6914
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006915 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006916 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006917 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006918 return Res;
6919 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006920
Owen Andersone50ed302009-08-10 22:56:29 +00006921 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006922 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006923 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006924 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006925 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006926 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006927 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006928 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6929 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006930 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006931 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006932 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006933 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006934 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006935 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006936 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006937 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006938 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006939 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006940 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006941 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006942 if (Idx == 0)
6943 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006944
Evan Cheng0db9fe62006-04-25 20:13:52 +00006945 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006946 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006947 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006948 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006949 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006950 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006951 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006952 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006953 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6954 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6955 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006956 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006957 if (Idx == 0)
6958 return Op;
6959
6960 // UNPCKHPD the element to the lowest double word, then movsd.
6961 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6962 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006963 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006964 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006965 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006966 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006967 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006968 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006969 }
6970
Dan Gohman475871a2008-07-27 21:46:04 +00006971 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006972}
6973
Dan Gohman475871a2008-07-27 21:46:04 +00006974SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006975X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6976 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006977 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006978 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006979 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006980
Dan Gohman475871a2008-07-27 21:46:04 +00006981 SDValue N0 = Op.getOperand(0);
6982 SDValue N1 = Op.getOperand(1);
6983 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006984
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006985 if (VT.getSizeInBits() == 256)
6986 return SDValue();
6987
Dan Gohman8a55ce42009-09-23 21:02:20 +00006988 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006989 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006990 unsigned Opc;
6991 if (VT == MVT::v8i16)
6992 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006993 else if (VT == MVT::v16i8)
6994 Opc = X86ISD::PINSRB;
6995 else
6996 Opc = X86ISD::PINSRB;
6997
Nate Begeman14d12ca2008-02-11 04:19:36 +00006998 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6999 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007000 if (N1.getValueType() != MVT::i32)
7001 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7002 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007003 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007004 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007005 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007006 // Bits [7:6] of the constant are the source select. This will always be
7007 // zero here. The DAG Combiner may combine an extract_elt index into these
7008 // bits. For example (insert (extract, 3), 2) could be matched by putting
7009 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007010 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007011 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007012 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007013 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007014 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007015 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007016 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007017 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007018 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007019 // PINSR* works with constant index.
7020 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007021 }
Dan Gohman475871a2008-07-27 21:46:04 +00007022 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007023}
7024
Dan Gohman475871a2008-07-27 21:46:04 +00007025SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007026X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007027 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007028 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007029
David Greene6b381262011-02-09 15:32:06 +00007030 DebugLoc dl = Op.getDebugLoc();
7031 SDValue N0 = Op.getOperand(0);
7032 SDValue N1 = Op.getOperand(1);
7033 SDValue N2 = Op.getOperand(2);
7034
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007035 // If this is a 256-bit vector result, first extract the 128-bit vector,
7036 // insert the element into the extracted half and then place it back.
7037 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007038 if (!isa<ConstantSDNode>(N2))
7039 return SDValue();
7040
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007041 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007042 unsigned NumElems = VT.getVectorNumElements();
7043 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007044 bool Upper = IdxVal >= NumElems/2;
7045 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
7046 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007047
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007048 // Insert the element into the desired half.
7049 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
7050 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00007051
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007052 // Insert the changed part back to the 256-bit vector
7053 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007054 }
7055
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007056 if (Subtarget->hasSSE41() || Subtarget->hasAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007057 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7058
Dan Gohman8a55ce42009-09-23 21:02:20 +00007059 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007060 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007061
Dan Gohman8a55ce42009-09-23 21:02:20 +00007062 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007063 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7064 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007065 if (N1.getValueType() != MVT::i32)
7066 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7067 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007068 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007069 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007070 }
Dan Gohman475871a2008-07-27 21:46:04 +00007071 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007072}
7073
Dan Gohman475871a2008-07-27 21:46:04 +00007074SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007075X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007076 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007077 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007078 EVT OpVT = Op.getValueType();
7079
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007080 // If this is a 256-bit vector result, first insert into a 128-bit
7081 // vector and then insert into the 256-bit vector.
7082 if (OpVT.getSizeInBits() > 128) {
7083 // Insert into a 128-bit vector.
7084 EVT VT128 = EVT::getVectorVT(*Context,
7085 OpVT.getVectorElementType(),
7086 OpVT.getVectorNumElements() / 2);
7087
7088 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7089
7090 // Insert the 128-bit vector.
7091 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7092 DAG.getConstant(0, MVT::i32),
7093 DAG, dl);
7094 }
7095
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007096 if (Op.getValueType() == MVT::v1i64 &&
7097 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007098 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007099
Owen Anderson825b72b2009-08-11 20:47:22 +00007100 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007101 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7102 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007103 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007104 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007105}
7106
David Greene91585092011-01-26 15:38:49 +00007107// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7108// a simple subregister reference or explicit instructions to grab
7109// upper bits of a vector.
7110SDValue
7111X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7112 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007113 DebugLoc dl = Op.getNode()->getDebugLoc();
7114 SDValue Vec = Op.getNode()->getOperand(0);
7115 SDValue Idx = Op.getNode()->getOperand(1);
7116
7117 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7118 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7119 return Extract128BitVector(Vec, Idx, DAG, dl);
7120 }
David Greene91585092011-01-26 15:38:49 +00007121 }
7122 return SDValue();
7123}
7124
David Greenecfe33c42011-01-26 19:13:22 +00007125// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7126// simple superregister reference or explicit instructions to insert
7127// the upper bits of a vector.
7128SDValue
7129X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7130 if (Subtarget->hasAVX()) {
7131 DebugLoc dl = Op.getNode()->getDebugLoc();
7132 SDValue Vec = Op.getNode()->getOperand(0);
7133 SDValue SubVec = Op.getNode()->getOperand(1);
7134 SDValue Idx = Op.getNode()->getOperand(2);
7135
7136 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7137 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007138 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007139 }
7140 }
7141 return SDValue();
7142}
7143
Bill Wendling056292f2008-09-16 21:48:12 +00007144// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7145// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7146// one of the above mentioned nodes. It has to be wrapped because otherwise
7147// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7148// be used to form addressing mode. These wrapped nodes will be selected
7149// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007150SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007151X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007152 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007153
Chris Lattner41621a22009-06-26 19:22:52 +00007154 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7155 // global base reg.
7156 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007157 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007158 CodeModel::Model M = getTargetMachine().getCodeModel();
7159
Chris Lattner4f066492009-07-11 20:29:19 +00007160 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007161 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007162 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007163 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007164 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007165 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007166 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007167
Evan Cheng1606e8e2009-03-13 07:51:59 +00007168 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007169 CP->getAlignment(),
7170 CP->getOffset(), OpFlag);
7171 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007172 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007173 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007174 if (OpFlag) {
7175 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007176 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007177 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007178 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007179 }
7180
7181 return Result;
7182}
7183
Dan Gohmand858e902010-04-17 15:26:15 +00007184SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007185 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007186
Chris Lattner18c59872009-06-27 04:16:01 +00007187 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7188 // global base reg.
7189 unsigned char OpFlag = 0;
7190 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007191 CodeModel::Model M = getTargetMachine().getCodeModel();
7192
Chris Lattner4f066492009-07-11 20:29:19 +00007193 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007194 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007195 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007196 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007197 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007198 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007199 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007200
Chris Lattner18c59872009-06-27 04:16:01 +00007201 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7202 OpFlag);
7203 DebugLoc DL = JT->getDebugLoc();
7204 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007205
Chris Lattner18c59872009-06-27 04:16:01 +00007206 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007207 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007208 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7209 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007210 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007211 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007212
Chris Lattner18c59872009-06-27 04:16:01 +00007213 return Result;
7214}
7215
7216SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007217X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007218 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007219
Chris Lattner18c59872009-06-27 04:16:01 +00007220 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7221 // global base reg.
7222 unsigned char OpFlag = 0;
7223 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007224 CodeModel::Model M = getTargetMachine().getCodeModel();
7225
Chris Lattner4f066492009-07-11 20:29:19 +00007226 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007227 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7228 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7229 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007230 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007231 } else if (Subtarget->isPICStyleGOT()) {
7232 OpFlag = X86II::MO_GOT;
7233 } else if (Subtarget->isPICStyleStubPIC()) {
7234 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7235 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7236 OpFlag = X86II::MO_DARWIN_NONLAZY;
7237 }
Eric Christopherfd179292009-08-27 18:07:15 +00007238
Chris Lattner18c59872009-06-27 04:16:01 +00007239 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007240
Chris Lattner18c59872009-06-27 04:16:01 +00007241 DebugLoc DL = Op.getDebugLoc();
7242 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007243
7244
Chris Lattner18c59872009-06-27 04:16:01 +00007245 // With PIC, the address is actually $g + Offset.
7246 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007247 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007248 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7249 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007250 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007251 Result);
7252 }
Eric Christopherfd179292009-08-27 18:07:15 +00007253
Eli Friedman586272d2011-08-11 01:48:05 +00007254 // For symbols that require a load from a stub to get the address, emit the
7255 // load.
7256 if (isGlobalStubReference(OpFlag))
7257 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
7258 MachinePointerInfo::getGOT(), false, false, 0);
7259
Chris Lattner18c59872009-06-27 04:16:01 +00007260 return Result;
7261}
7262
Dan Gohman475871a2008-07-27 21:46:04 +00007263SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007264X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007265 // Create the TargetBlockAddressAddress node.
7266 unsigned char OpFlags =
7267 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007268 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007269 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007270 DebugLoc dl = Op.getDebugLoc();
7271 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7272 /*isTarget=*/true, OpFlags);
7273
Dan Gohmanf705adb2009-10-30 01:28:02 +00007274 if (Subtarget->isPICStyleRIPRel() &&
7275 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007276 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7277 else
7278 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007279
Dan Gohman29cbade2009-11-20 23:18:13 +00007280 // With PIC, the address is actually $g + Offset.
7281 if (isGlobalRelativeToPICBase(OpFlags)) {
7282 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7283 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7284 Result);
7285 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007286
7287 return Result;
7288}
7289
7290SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007291X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007292 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007293 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007294 // Create the TargetGlobalAddress node, folding in the constant
7295 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007296 unsigned char OpFlags =
7297 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007298 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007299 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007300 if (OpFlags == X86II::MO_NO_FLAG &&
7301 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007302 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007303 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007304 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007305 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007306 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007307 }
Eric Christopherfd179292009-08-27 18:07:15 +00007308
Chris Lattner4f066492009-07-11 20:29:19 +00007309 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007310 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007311 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7312 else
7313 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007314
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007315 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007316 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007317 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7318 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007319 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007320 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007321
Chris Lattner36c25012009-07-10 07:34:39 +00007322 // For globals that require a load from a stub to get the address, emit the
7323 // load.
7324 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007325 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007326 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007327
Dan Gohman6520e202008-10-18 02:06:02 +00007328 // If there was a non-zero offset that we didn't fold, create an explicit
7329 // addition for it.
7330 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007331 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007332 DAG.getConstant(Offset, getPointerTy()));
7333
Evan Cheng0db9fe62006-04-25 20:13:52 +00007334 return Result;
7335}
7336
Evan Chengda43bcf2008-09-24 00:05:32 +00007337SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007338X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007339 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007340 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007341 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007342}
7343
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007344static SDValue
7345GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007346 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007347 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007348 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007349 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007350 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007351 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007352 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007353 GA->getOffset(),
7354 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007355 if (InFlag) {
7356 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007357 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007358 } else {
7359 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007360 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007361 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007362
7363 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007364 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007365
Rafael Espindola15f1b662009-04-24 12:59:40 +00007366 SDValue Flag = Chain.getValue(1);
7367 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007368}
7369
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007370// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007371static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007372LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007373 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007374 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007375 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7376 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007377 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007378 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007379 InFlag = Chain.getValue(1);
7380
Chris Lattnerb903bed2009-06-26 21:20:29 +00007381 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007382}
7383
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007384// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007385static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007386LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007387 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007388 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7389 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007390}
7391
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007392// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7393// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007394static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007395 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007396 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007397 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007398
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007399 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7400 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7401 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007402
Michael J. Spencerec38de22010-10-10 22:04:20 +00007403 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007404 DAG.getIntPtrConstant(0),
7405 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007406
Chris Lattnerb903bed2009-06-26 21:20:29 +00007407 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007408 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7409 // initialexec.
7410 unsigned WrapperKind = X86ISD::Wrapper;
7411 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007412 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007413 } else if (is64Bit) {
7414 assert(model == TLSModel::InitialExec);
7415 OperandFlags = X86II::MO_GOTTPOFF;
7416 WrapperKind = X86ISD::WrapperRIP;
7417 } else {
7418 assert(model == TLSModel::InitialExec);
7419 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007420 }
Eric Christopherfd179292009-08-27 18:07:15 +00007421
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007422 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7423 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007424 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007425 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007426 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007427 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007428
Rafael Espindola9a580232009-02-27 13:37:18 +00007429 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007430 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007431 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007432
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007433 // The address of the thread local variable is the add of the thread
7434 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007435 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007436}
7437
Dan Gohman475871a2008-07-27 21:46:04 +00007438SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007439X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007440
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007441 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007442 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007443
Eric Christopher30ef0e52010-06-03 04:07:48 +00007444 if (Subtarget->isTargetELF()) {
7445 // TODO: implement the "local dynamic" model
7446 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007447
Eric Christopher30ef0e52010-06-03 04:07:48 +00007448 // If GV is an alias then use the aliasee for determining
7449 // thread-localness.
7450 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7451 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007452
7453 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007454 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007455
Eric Christopher30ef0e52010-06-03 04:07:48 +00007456 switch (model) {
7457 case TLSModel::GeneralDynamic:
7458 case TLSModel::LocalDynamic: // not implemented
7459 if (Subtarget->is64Bit())
7460 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7461 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007462
Eric Christopher30ef0e52010-06-03 04:07:48 +00007463 case TLSModel::InitialExec:
7464 case TLSModel::LocalExec:
7465 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7466 Subtarget->is64Bit());
7467 }
7468 } else if (Subtarget->isTargetDarwin()) {
7469 // Darwin only has one model of TLS. Lower to that.
7470 unsigned char OpFlag = 0;
7471 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7472 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007473
Eric Christopher30ef0e52010-06-03 04:07:48 +00007474 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7475 // global base reg.
7476 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7477 !Subtarget->is64Bit();
7478 if (PIC32)
7479 OpFlag = X86II::MO_TLVP_PIC_BASE;
7480 else
7481 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007482 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007483 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007484 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007485 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007486 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007487
Eric Christopher30ef0e52010-06-03 04:07:48 +00007488 // With PIC32, the address is actually $g + Offset.
7489 if (PIC32)
7490 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7491 DAG.getNode(X86ISD::GlobalBaseReg,
7492 DebugLoc(), getPointerTy()),
7493 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007494
Eric Christopher30ef0e52010-06-03 04:07:48 +00007495 // Lowering the machine isd will make sure everything is in the right
7496 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007497 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007498 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007499 SDValue Args[] = { Chain, Offset };
7500 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007501
Eric Christopher30ef0e52010-06-03 04:07:48 +00007502 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7503 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7504 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007505
Eric Christopher30ef0e52010-06-03 04:07:48 +00007506 // And our return value (tls address) is in the standard call return value
7507 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007508 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
7509 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007510 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007511
Eric Christopher30ef0e52010-06-03 04:07:48 +00007512 assert(false &&
7513 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007514
Torok Edwinc23197a2009-07-14 16:55:14 +00007515 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007516 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007517}
7518
Evan Cheng0db9fe62006-04-25 20:13:52 +00007519
Nadav Rotem43012222011-05-11 08:12:09 +00007520/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007521/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007522SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007523 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007524 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007525 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007526 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007527 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007528 SDValue ShOpLo = Op.getOperand(0);
7529 SDValue ShOpHi = Op.getOperand(1);
7530 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007531 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007532 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007533 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007534
Dan Gohman475871a2008-07-27 21:46:04 +00007535 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007536 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007537 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7538 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007539 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007540 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7541 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007542 }
Evan Chenge3413162006-01-09 18:33:28 +00007543
Owen Anderson825b72b2009-08-11 20:47:22 +00007544 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7545 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007546 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007547 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007548
Dan Gohman475871a2008-07-27 21:46:04 +00007549 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007550 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007551 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7552 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007553
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007554 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007555 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7556 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007557 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007558 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7559 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007560 }
7561
Dan Gohman475871a2008-07-27 21:46:04 +00007562 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007563 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007564}
Evan Chenga3195e82006-01-12 22:54:21 +00007565
Dan Gohmand858e902010-04-17 15:26:15 +00007566SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7567 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007568 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007569
Dale Johannesen0488fb62010-09-30 23:57:10 +00007570 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007571 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007572
Owen Anderson825b72b2009-08-11 20:47:22 +00007573 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007574 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007575
Eli Friedman36df4992009-05-27 00:47:34 +00007576 // These are really Legal; return the operand so the caller accepts it as
7577 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007578 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007579 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007580 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007581 Subtarget->is64Bit()) {
7582 return Op;
7583 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007584
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007585 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007586 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007587 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007588 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007589 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007590 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007591 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007592 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007593 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007594 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7595}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007596
Owen Andersone50ed302009-08-10 22:56:29 +00007597SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007598 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007599 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007600 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007601 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007602 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007603 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007604 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007605 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007606 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007607 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007608
Chris Lattner492a43e2010-09-22 01:28:21 +00007609 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007610
Stuart Hastings84be9582011-06-02 15:57:11 +00007611 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7612 MachineMemOperand *MMO;
7613 if (FI) {
7614 int SSFI = FI->getIndex();
7615 MMO =
7616 DAG.getMachineFunction()
7617 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7618 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7619 } else {
7620 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7621 StackSlot = StackSlot.getOperand(1);
7622 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007623 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007624 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7625 X86ISD::FILD, DL,
7626 Tys, Ops, array_lengthof(Ops),
7627 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007628
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007629 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007630 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007631 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007632
7633 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7634 // shouldn't be necessary except that RFP cannot be live across
7635 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007636 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007637 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7638 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007639 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007640 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007641 SDValue Ops[] = {
7642 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7643 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007644 MachineMemOperand *MMO =
7645 DAG.getMachineFunction()
7646 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007647 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007648
Chris Lattner492a43e2010-09-22 01:28:21 +00007649 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7650 Ops, array_lengthof(Ops),
7651 Op.getValueType(), MMO);
7652 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007653 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007654 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007655 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007656
Evan Cheng0db9fe62006-04-25 20:13:52 +00007657 return Result;
7658}
7659
Bill Wendling8b8a6362009-01-17 03:56:04 +00007660// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007661SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7662 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007663 // This algorithm is not obvious. Here it is in C code, more or less:
7664 /*
7665 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7666 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7667 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007668
Bill Wendling8b8a6362009-01-17 03:56:04 +00007669 // Copy ints to xmm registers.
7670 __m128i xh = _mm_cvtsi32_si128( hi );
7671 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007672
Bill Wendling8b8a6362009-01-17 03:56:04 +00007673 // Combine into low half of a single xmm register.
7674 __m128i x = _mm_unpacklo_epi32( xh, xl );
7675 __m128d d;
7676 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007677
Bill Wendling8b8a6362009-01-17 03:56:04 +00007678 // Merge in appropriate exponents to give the integer bits the right
7679 // magnitude.
7680 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007681
Bill Wendling8b8a6362009-01-17 03:56:04 +00007682 // Subtract away the biases to deal with the IEEE-754 double precision
7683 // implicit 1.
7684 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007685
Bill Wendling8b8a6362009-01-17 03:56:04 +00007686 // All conversions up to here are exact. The correctly rounded result is
7687 // calculated using the current rounding mode using the following
7688 // horizontal add.
7689 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7690 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7691 // store doesn't really need to be here (except
7692 // maybe to zero the other double)
7693 return sd;
7694 }
7695 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007696
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007697 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007698 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007699
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007700 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007701 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007702 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7703 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7704 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7705 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007706 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007707 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007708
Bill Wendling8b8a6362009-01-17 03:56:04 +00007709 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007710 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007711 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007712 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007713 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007714 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007715 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007716
Owen Anderson825b72b2009-08-11 20:47:22 +00007717 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7718 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007719 Op.getOperand(0),
7720 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007721 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7722 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007723 Op.getOperand(0),
7724 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007725 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7726 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007727 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007728 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007729 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007730 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007731 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007732 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007733 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007734 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007735
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007736 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007737 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007738 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7739 DAG.getUNDEF(MVT::v2f64), ShufMask);
7740 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7741 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007742 DAG.getIntPtrConstant(0));
7743}
7744
Bill Wendling8b8a6362009-01-17 03:56:04 +00007745// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007746SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7747 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007748 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007749 // FP constant to bias correct the final result.
7750 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007751 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007752
7753 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007754 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007755 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007756
Eli Friedmanf3704762011-08-29 21:15:46 +00007757 // Zero out the upper parts of the register.
7758 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasSSE2(), DAG);
7759
Owen Anderson825b72b2009-08-11 20:47:22 +00007760 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007761 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007762 DAG.getIntPtrConstant(0));
7763
7764 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007765 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007766 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007767 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007768 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007769 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007770 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007771 MVT::v2f64, Bias)));
7772 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007773 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007774 DAG.getIntPtrConstant(0));
7775
7776 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007777 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007778
7779 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007780 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007781
Owen Anderson825b72b2009-08-11 20:47:22 +00007782 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007783 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007784 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007785 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007786 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007787 }
7788
7789 // Handle final rounding.
7790 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007791}
7792
Dan Gohmand858e902010-04-17 15:26:15 +00007793SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7794 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007795 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007796 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007797
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007798 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007799 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7800 // the optimization here.
7801 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007802 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007803
Owen Andersone50ed302009-08-10 22:56:29 +00007804 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007805 EVT DstVT = Op.getValueType();
7806 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007807 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007808 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007809 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007810
7811 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007812 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007813 if (SrcVT == MVT::i32) {
7814 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7815 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7816 getPointerTy(), StackSlot, WordOff);
7817 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007818 StackSlot, MachinePointerInfo(),
7819 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007820 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007821 OffsetSlot, MachinePointerInfo(),
7822 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007823 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7824 return Fild;
7825 }
7826
7827 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7828 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007829 StackSlot, MachinePointerInfo(),
7830 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007831 // For i64 source, we need to add the appropriate power of 2 if the input
7832 // was negative. This is the same as the optimization in
7833 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7834 // we must be careful to do the computation in x87 extended precision, not
7835 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007836 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7837 MachineMemOperand *MMO =
7838 DAG.getMachineFunction()
7839 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7840 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007841
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007842 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7843 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007844 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7845 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007846
7847 APInt FF(32, 0x5F800000ULL);
7848
7849 // Check whether the sign bit is set.
7850 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7851 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7852 ISD::SETLT);
7853
7854 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7855 SDValue FudgePtr = DAG.getConstantPool(
7856 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7857 getPointerTy());
7858
7859 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7860 SDValue Zero = DAG.getIntPtrConstant(0);
7861 SDValue Four = DAG.getIntPtrConstant(4);
7862 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7863 Zero, Four);
7864 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7865
7866 // Load the value out, extending it from f32 to f80.
7867 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007868 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007869 FudgePtr, MachinePointerInfo::getConstantPool(),
7870 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007871 // Extend everything to 80 bits to force it to be done on x87.
7872 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7873 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007874}
7875
Dan Gohman475871a2008-07-27 21:46:04 +00007876std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007877FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007878 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007879
Owen Andersone50ed302009-08-10 22:56:29 +00007880 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007881
7882 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007883 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7884 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007885 }
7886
Owen Anderson825b72b2009-08-11 20:47:22 +00007887 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7888 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007889 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007890
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007891 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007892 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007893 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007894 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007895 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007896 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007897 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007898 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007899
Evan Cheng87c89352007-10-15 20:11:21 +00007900 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7901 // stack slot.
7902 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007903 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007904 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007905 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007906
Michael J. Spencerec38de22010-10-10 22:04:20 +00007907
7908
Evan Cheng0db9fe62006-04-25 20:13:52 +00007909 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007910 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007911 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007912 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7913 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7914 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007915 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007916
Dan Gohman475871a2008-07-27 21:46:04 +00007917 SDValue Chain = DAG.getEntryNode();
7918 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007919 EVT TheVT = Op.getOperand(0).getValueType();
7920 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007921 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007922 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007923 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007924 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007925 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007926 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007927 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007928 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007929
Chris Lattner492a43e2010-09-22 01:28:21 +00007930 MachineMemOperand *MMO =
7931 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7932 MachineMemOperand::MOLoad, MemSize, MemSize);
7933 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7934 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007935 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007936 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007937 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7938 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007939
Chris Lattner07290932010-09-22 01:05:16 +00007940 MachineMemOperand *MMO =
7941 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7942 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007943
Evan Cheng0db9fe62006-04-25 20:13:52 +00007944 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007945 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007946 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7947 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007948
Chris Lattner27a6c732007-11-24 07:07:01 +00007949 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007950}
7951
Dan Gohmand858e902010-04-17 15:26:15 +00007952SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7953 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007954 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007955 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007956
Eli Friedman948e95a2009-05-23 09:59:16 +00007957 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007958 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007959 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7960 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007961
Chris Lattner27a6c732007-11-24 07:07:01 +00007962 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007963 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007964 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007965}
7966
Dan Gohmand858e902010-04-17 15:26:15 +00007967SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7968 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007969 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7970 SDValue FIST = Vals.first, StackSlot = Vals.second;
7971 assert(FIST.getNode() && "Unexpected failure");
7972
7973 // Load the result.
7974 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007975 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007976}
7977
Dan Gohmand858e902010-04-17 15:26:15 +00007978SDValue X86TargetLowering::LowerFABS(SDValue Op,
7979 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007980 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007981 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007982 EVT VT = Op.getValueType();
7983 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007984 if (VT.isVector())
7985 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007986 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007987 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007988 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007989 CV.push_back(C);
7990 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007991 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007992 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007993 CV.push_back(C);
7994 CV.push_back(C);
7995 CV.push_back(C);
7996 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007997 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007998 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007999 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008000 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008001 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008002 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008003 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008004}
8005
Dan Gohmand858e902010-04-17 15:26:15 +00008006SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008007 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008008 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008009 EVT VT = Op.getValueType();
8010 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00008011 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008012 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008013 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008014 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008015 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00008016 CV.push_back(C);
8017 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008018 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008019 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00008020 CV.push_back(C);
8021 CV.push_back(C);
8022 CV.push_back(C);
8023 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008024 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008025 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008026 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008027 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008028 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008029 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008030 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008031 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008032 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008033 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008034 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008035 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008036 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008037 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00008038 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008039}
8040
Dan Gohmand858e902010-04-17 15:26:15 +00008041SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008042 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008043 SDValue Op0 = Op.getOperand(0);
8044 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008045 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008046 EVT VT = Op.getValueType();
8047 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008048
8049 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008050 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008051 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008052 SrcVT = VT;
8053 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008054 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008055 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008056 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008057 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008058 }
8059
8060 // At this point the operands and the result should have the same
8061 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008062
Evan Cheng68c47cb2007-01-05 07:55:56 +00008063 // First get the sign bit of second operand.
8064 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008065 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008066 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8067 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008068 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008069 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8070 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8071 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8072 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008073 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008074 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008075 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008076 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008077 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008078 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008079 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008080
8081 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008082 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008083 // Op0 is MVT::f32, Op1 is MVT::f64.
8084 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8085 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8086 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008087 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008088 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008089 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008090 }
8091
Evan Cheng73d6cf12007-01-05 21:37:56 +00008092 // Clear first operand sign bit.
8093 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008094 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008095 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8096 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008097 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008098 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8099 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8100 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8101 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008102 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008103 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008104 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008105 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008106 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008107 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008108 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008109
8110 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008111 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008112}
8113
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008114SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8115 SDValue N0 = Op.getOperand(0);
8116 DebugLoc dl = Op.getDebugLoc();
8117 EVT VT = Op.getValueType();
8118
8119 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8120 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8121 DAG.getConstant(1, VT));
8122 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8123}
8124
Dan Gohman076aee32009-03-04 19:44:21 +00008125/// Emit nodes that will be selected as "test Op0,Op0", or something
8126/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008127SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008128 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008129 DebugLoc dl = Op.getDebugLoc();
8130
Dan Gohman31125812009-03-07 01:58:32 +00008131 // CF and OF aren't always set the way we want. Determine which
8132 // of these we need.
8133 bool NeedCF = false;
8134 bool NeedOF = false;
8135 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008136 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008137 case X86::COND_A: case X86::COND_AE:
8138 case X86::COND_B: case X86::COND_BE:
8139 NeedCF = true;
8140 break;
8141 case X86::COND_G: case X86::COND_GE:
8142 case X86::COND_L: case X86::COND_LE:
8143 case X86::COND_O: case X86::COND_NO:
8144 NeedOF = true;
8145 break;
Dan Gohman31125812009-03-07 01:58:32 +00008146 }
8147
Dan Gohman076aee32009-03-04 19:44:21 +00008148 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008149 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8150 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008151 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8152 // Emit a CMP with 0, which is the TEST pattern.
8153 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8154 DAG.getConstant(0, Op.getValueType()));
8155
8156 unsigned Opcode = 0;
8157 unsigned NumOperands = 0;
8158 switch (Op.getNode()->getOpcode()) {
8159 case ISD::ADD:
8160 // Due to an isel shortcoming, be conservative if this add is likely to be
8161 // selected as part of a load-modify-store instruction. When the root node
8162 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8163 // uses of other nodes in the match, such as the ADD in this case. This
8164 // leads to the ADD being left around and reselected, with the result being
8165 // two adds in the output. Alas, even if none our users are stores, that
8166 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8167 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8168 // climbing the DAG back to the root, and it doesn't seem to be worth the
8169 // effort.
8170 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00008171 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008172 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
8173 goto default_case;
8174
8175 if (ConstantSDNode *C =
8176 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8177 // An add of one will be selected as an INC.
8178 if (C->getAPIntValue() == 1) {
8179 Opcode = X86ISD::INC;
8180 NumOperands = 1;
8181 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008182 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008183
8184 // An add of negative one (subtract of one) will be selected as a DEC.
8185 if (C->getAPIntValue().isAllOnesValue()) {
8186 Opcode = X86ISD::DEC;
8187 NumOperands = 1;
8188 break;
8189 }
Dan Gohman076aee32009-03-04 19:44:21 +00008190 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008191
8192 // Otherwise use a regular EFLAGS-setting add.
8193 Opcode = X86ISD::ADD;
8194 NumOperands = 2;
8195 break;
8196 case ISD::AND: {
8197 // If the primary and result isn't used, don't bother using X86ISD::AND,
8198 // because a TEST instruction will be better.
8199 bool NonFlagUse = false;
8200 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8201 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8202 SDNode *User = *UI;
8203 unsigned UOpNo = UI.getOperandNo();
8204 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8205 // Look pass truncate.
8206 UOpNo = User->use_begin().getOperandNo();
8207 User = *User->use_begin();
8208 }
8209
8210 if (User->getOpcode() != ISD::BRCOND &&
8211 User->getOpcode() != ISD::SETCC &&
8212 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8213 NonFlagUse = true;
8214 break;
8215 }
Dan Gohman076aee32009-03-04 19:44:21 +00008216 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008217
8218 if (!NonFlagUse)
8219 break;
8220 }
8221 // FALL THROUGH
8222 case ISD::SUB:
8223 case ISD::OR:
8224 case ISD::XOR:
8225 // Due to the ISEL shortcoming noted above, be conservative if this op is
8226 // likely to be selected as part of a load-modify-store instruction.
8227 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8228 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8229 if (UI->getOpcode() == ISD::STORE)
8230 goto default_case;
8231
8232 // Otherwise use a regular EFLAGS-setting instruction.
8233 switch (Op.getNode()->getOpcode()) {
8234 default: llvm_unreachable("unexpected operator!");
8235 case ISD::SUB: Opcode = X86ISD::SUB; break;
8236 case ISD::OR: Opcode = X86ISD::OR; break;
8237 case ISD::XOR: Opcode = X86ISD::XOR; break;
8238 case ISD::AND: Opcode = X86ISD::AND; break;
8239 }
8240
8241 NumOperands = 2;
8242 break;
8243 case X86ISD::ADD:
8244 case X86ISD::SUB:
8245 case X86ISD::INC:
8246 case X86ISD::DEC:
8247 case X86ISD::OR:
8248 case X86ISD::XOR:
8249 case X86ISD::AND:
8250 return SDValue(Op.getNode(), 1);
8251 default:
8252 default_case:
8253 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008254 }
8255
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008256 if (Opcode == 0)
8257 // Emit a CMP with 0, which is the TEST pattern.
8258 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8259 DAG.getConstant(0, Op.getValueType()));
8260
8261 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8262 SmallVector<SDValue, 4> Ops;
8263 for (unsigned i = 0; i != NumOperands; ++i)
8264 Ops.push_back(Op.getOperand(i));
8265
8266 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8267 DAG.ReplaceAllUsesWith(Op, New);
8268 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008269}
8270
8271/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8272/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008273SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008274 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008275 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8276 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008277 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008278
8279 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008280 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008281}
8282
Evan Chengd40d03e2010-01-06 19:38:29 +00008283/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8284/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008285SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8286 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008287 SDValue Op0 = And.getOperand(0);
8288 SDValue Op1 = And.getOperand(1);
8289 if (Op0.getOpcode() == ISD::TRUNCATE)
8290 Op0 = Op0.getOperand(0);
8291 if (Op1.getOpcode() == ISD::TRUNCATE)
8292 Op1 = Op1.getOperand(0);
8293
Evan Chengd40d03e2010-01-06 19:38:29 +00008294 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008295 if (Op1.getOpcode() == ISD::SHL)
8296 std::swap(Op0, Op1);
8297 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008298 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8299 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008300 // If we looked past a truncate, check that it's only truncating away
8301 // known zeros.
8302 unsigned BitWidth = Op0.getValueSizeInBits();
8303 unsigned AndBitWidth = And.getValueSizeInBits();
8304 if (BitWidth > AndBitWidth) {
8305 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8306 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8307 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8308 return SDValue();
8309 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008310 LHS = Op1;
8311 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008312 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008313 } else if (Op1.getOpcode() == ISD::Constant) {
8314 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
8315 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00008316 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
8317 LHS = AndLHS.getOperand(0);
8318 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008319 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008320 }
Evan Cheng0488db92007-09-25 01:57:46 +00008321
Evan Chengd40d03e2010-01-06 19:38:29 +00008322 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008323 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008324 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008325 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008326 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008327 // Also promote i16 to i32 for performance / code size reason.
8328 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008329 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008330 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008331
Evan Chengd40d03e2010-01-06 19:38:29 +00008332 // If the operand types disagree, extend the shift amount to match. Since
8333 // BT ignores high bits (like shifts) we can use anyextend.
8334 if (LHS.getValueType() != RHS.getValueType())
8335 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008336
Evan Chengd40d03e2010-01-06 19:38:29 +00008337 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8338 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8339 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8340 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008341 }
8342
Evan Cheng54de3ea2010-01-05 06:52:31 +00008343 return SDValue();
8344}
8345
Dan Gohmand858e902010-04-17 15:26:15 +00008346SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008347
8348 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8349
Evan Cheng54de3ea2010-01-05 06:52:31 +00008350 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8351 SDValue Op0 = Op.getOperand(0);
8352 SDValue Op1 = Op.getOperand(1);
8353 DebugLoc dl = Op.getDebugLoc();
8354 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8355
8356 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008357 // Lower (X & (1 << N)) == 0 to BT(X, N).
8358 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8359 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008360 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008361 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008362 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008363 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8364 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8365 if (NewSetCC.getNode())
8366 return NewSetCC;
8367 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008368
Chris Lattner481eebc2010-12-19 21:23:48 +00008369 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8370 // these.
8371 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008372 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008373 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8374 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008375
Chris Lattner481eebc2010-12-19 21:23:48 +00008376 // If the input is a setcc, then reuse the input setcc or use a new one with
8377 // the inverted condition.
8378 if (Op0.getOpcode() == X86ISD::SETCC) {
8379 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8380 bool Invert = (CC == ISD::SETNE) ^
8381 cast<ConstantSDNode>(Op1)->isNullValue();
8382 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008383
Evan Cheng2c755ba2010-02-27 07:36:59 +00008384 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008385 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8386 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8387 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008388 }
8389
Evan Chenge5b51ac2010-04-17 06:13:15 +00008390 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008391 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008392 if (X86CC == X86::COND_INVALID)
8393 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008394
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008395 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008396 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008397 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008398}
8399
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008400// Lower256IntVETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
8401// ones, and then concatenate the result back.
8402static SDValue Lower256IntVETCC(SDValue Op, SelectionDAG &DAG) {
8403 EVT VT = Op.getValueType();
8404
Duncan Sands28b77e92011-09-06 19:07:46 +00008405 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008406 "Unsupported value type for operation");
8407
8408 int NumElems = VT.getVectorNumElements();
8409 DebugLoc dl = Op.getDebugLoc();
8410 SDValue CC = Op.getOperand(2);
8411 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8412 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8413
8414 // Extract the LHS vectors
8415 SDValue LHS = Op.getOperand(0);
8416 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8417 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8418
8419 // Extract the RHS vectors
8420 SDValue RHS = Op.getOperand(1);
8421 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8422 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8423
8424 // Issue the operation on the smaller types and concatenate the result back
8425 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8426 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8427 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8428 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8429 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8430}
8431
8432
Dan Gohmand858e902010-04-17 15:26:15 +00008433SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008434 SDValue Cond;
8435 SDValue Op0 = Op.getOperand(0);
8436 SDValue Op1 = Op.getOperand(1);
8437 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008438 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008439 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8440 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008441 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008442
8443 if (isFP) {
8444 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008445 EVT EltVT = Op0.getValueType().getVectorElementType();
8446 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8447
8448 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008449 bool Swap = false;
8450
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008451 // SSE Condition code mapping:
8452 // 0 - EQ
8453 // 1 - LT
8454 // 2 - LE
8455 // 3 - UNORD
8456 // 4 - NEQ
8457 // 5 - NLT
8458 // 6 - NLE
8459 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008460 switch (SetCCOpcode) {
8461 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008462 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008463 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008464 case ISD::SETOGT:
8465 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008466 case ISD::SETLT:
8467 case ISD::SETOLT: SSECC = 1; break;
8468 case ISD::SETOGE:
8469 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008470 case ISD::SETLE:
8471 case ISD::SETOLE: SSECC = 2; break;
8472 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008473 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008474 case ISD::SETNE: SSECC = 4; break;
8475 case ISD::SETULE: Swap = true;
8476 case ISD::SETUGE: SSECC = 5; break;
8477 case ISD::SETULT: Swap = true;
8478 case ISD::SETUGT: SSECC = 6; break;
8479 case ISD::SETO: SSECC = 7; break;
8480 }
8481 if (Swap)
8482 std::swap(Op0, Op1);
8483
Nate Begemanfb8ead02008-07-25 19:05:58 +00008484 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008485 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008486 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008487 SDValue UNORD, EQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008488 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8489 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008490 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008491 }
8492 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008493 SDValue ORD, NEQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008494 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8495 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008496 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008497 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008498 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008499 }
8500 // Handle all other FP comparisons here.
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008501 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008502 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008503
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008504 // Break 256-bit integer vector compare into smaller ones.
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008505 if (!isFP && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008506 return Lower256IntVETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008507
Nate Begeman30a0de92008-07-17 16:51:19 +00008508 // We are handling one of the integer comparisons here. Since SSE only has
8509 // GT and EQ comparisons for integer, swapping operands and multiple
8510 // operations may be required for some comparisons.
8511 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8512 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008513
Owen Anderson825b72b2009-08-11 20:47:22 +00008514 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008515 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008516 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008517 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008518 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8519 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008520 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008521
Nate Begeman30a0de92008-07-17 16:51:19 +00008522 switch (SetCCOpcode) {
8523 default: break;
8524 case ISD::SETNE: Invert = true;
8525 case ISD::SETEQ: Opc = EQOpc; break;
8526 case ISD::SETLT: Swap = true;
8527 case ISD::SETGT: Opc = GTOpc; break;
8528 case ISD::SETGE: Swap = true;
8529 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8530 case ISD::SETULT: Swap = true;
8531 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8532 case ISD::SETUGE: Swap = true;
8533 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8534 }
8535 if (Swap)
8536 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008537
Nate Begeman30a0de92008-07-17 16:51:19 +00008538 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8539 // bits of the inputs before performing those operations.
8540 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008541 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008542 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8543 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008544 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008545 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8546 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008547 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8548 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008549 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008550
Dale Johannesenace16102009-02-03 19:33:06 +00008551 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008552
8553 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008554 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008555 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008556
Nate Begeman30a0de92008-07-17 16:51:19 +00008557 return Result;
8558}
Evan Cheng0488db92007-09-25 01:57:46 +00008559
Evan Cheng370e5342008-12-03 08:38:43 +00008560// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008561static bool isX86LogicalCmp(SDValue Op) {
8562 unsigned Opc = Op.getNode()->getOpcode();
8563 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8564 return true;
8565 if (Op.getResNo() == 1 &&
8566 (Opc == X86ISD::ADD ||
8567 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008568 Opc == X86ISD::ADC ||
8569 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008570 Opc == X86ISD::SMUL ||
8571 Opc == X86ISD::UMUL ||
8572 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008573 Opc == X86ISD::DEC ||
8574 Opc == X86ISD::OR ||
8575 Opc == X86ISD::XOR ||
8576 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008577 return true;
8578
Chris Lattner9637d5b2010-12-05 07:49:54 +00008579 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8580 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008581
Dan Gohman076aee32009-03-04 19:44:21 +00008582 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008583}
8584
Chris Lattnera2b56002010-12-05 01:23:24 +00008585static bool isZero(SDValue V) {
8586 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8587 return C && C->isNullValue();
8588}
8589
Chris Lattner96908b12010-12-05 02:00:51 +00008590static bool isAllOnes(SDValue V) {
8591 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8592 return C && C->isAllOnesValue();
8593}
8594
Dan Gohmand858e902010-04-17 15:26:15 +00008595SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008596 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008597 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008598 SDValue Op1 = Op.getOperand(1);
8599 SDValue Op2 = Op.getOperand(2);
8600 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008601 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008602
Dan Gohman1a492952009-10-20 16:22:37 +00008603 if (Cond.getOpcode() == ISD::SETCC) {
8604 SDValue NewCond = LowerSETCC(Cond, DAG);
8605 if (NewCond.getNode())
8606 Cond = NewCond;
8607 }
Evan Cheng734503b2006-09-11 02:19:56 +00008608
Chris Lattnera2b56002010-12-05 01:23:24 +00008609 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008610 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008611 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008612 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008613 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008614 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8615 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008616 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008617
Chris Lattnera2b56002010-12-05 01:23:24 +00008618 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008619
8620 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008621 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8622 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008623
8624 SDValue CmpOp0 = Cmp.getOperand(0);
8625 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8626 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008627
Chris Lattner96908b12010-12-05 02:00:51 +00008628 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008629 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8630 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008631
Chris Lattner96908b12010-12-05 02:00:51 +00008632 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8633 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008634
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008635 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008636 if (N2C == 0 || !N2C->isNullValue())
8637 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8638 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008639 }
8640 }
8641
Chris Lattnera2b56002010-12-05 01:23:24 +00008642 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008643 if (Cond.getOpcode() == ISD::AND &&
8644 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8645 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008646 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008647 Cond = Cond.getOperand(0);
8648 }
8649
Evan Cheng3f41d662007-10-08 22:16:29 +00008650 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8651 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008652 if (Cond.getOpcode() == X86ISD::SETCC ||
8653 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008654 CC = Cond.getOperand(0);
8655
Dan Gohman475871a2008-07-27 21:46:04 +00008656 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008657 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008658 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008659
Evan Cheng3f41d662007-10-08 22:16:29 +00008660 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008661 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008662 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008663 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008664
Chris Lattnerd1980a52009-03-12 06:52:53 +00008665 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8666 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008667 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008668 addTest = false;
8669 }
8670 }
8671
8672 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008673 // Look pass the truncate.
8674 if (Cond.getOpcode() == ISD::TRUNCATE)
8675 Cond = Cond.getOperand(0);
8676
8677 // We know the result of AND is compared against zero. Try to match
8678 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008679 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008680 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008681 if (NewSetCC.getNode()) {
8682 CC = NewSetCC.getOperand(0);
8683 Cond = NewSetCC.getOperand(1);
8684 addTest = false;
8685 }
8686 }
8687 }
8688
8689 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008690 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008691 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008692 }
8693
Benjamin Kramere915ff32010-12-22 23:09:28 +00008694 // a < b ? -1 : 0 -> RES = ~setcc_carry
8695 // a < b ? 0 : -1 -> RES = setcc_carry
8696 // a >= b ? -1 : 0 -> RES = setcc_carry
8697 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8698 if (Cond.getOpcode() == X86ISD::CMP) {
8699 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8700
8701 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8702 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8703 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8704 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8705 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8706 return DAG.getNOT(DL, Res, Res.getValueType());
8707 return Res;
8708 }
8709 }
8710
Evan Cheng0488db92007-09-25 01:57:46 +00008711 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8712 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008713 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008714 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008715 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008716}
8717
Evan Cheng370e5342008-12-03 08:38:43 +00008718// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8719// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8720// from the AND / OR.
8721static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8722 Opc = Op.getOpcode();
8723 if (Opc != ISD::OR && Opc != ISD::AND)
8724 return false;
8725 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8726 Op.getOperand(0).hasOneUse() &&
8727 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8728 Op.getOperand(1).hasOneUse());
8729}
8730
Evan Cheng961d6d42009-02-02 08:19:07 +00008731// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8732// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008733static bool isXor1OfSetCC(SDValue Op) {
8734 if (Op.getOpcode() != ISD::XOR)
8735 return false;
8736 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8737 if (N1C && N1C->getAPIntValue() == 1) {
8738 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8739 Op.getOperand(0).hasOneUse();
8740 }
8741 return false;
8742}
8743
Dan Gohmand858e902010-04-17 15:26:15 +00008744SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008745 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008746 SDValue Chain = Op.getOperand(0);
8747 SDValue Cond = Op.getOperand(1);
8748 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008749 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008750 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00008751
Dan Gohman1a492952009-10-20 16:22:37 +00008752 if (Cond.getOpcode() == ISD::SETCC) {
8753 SDValue NewCond = LowerSETCC(Cond, DAG);
8754 if (NewCond.getNode())
8755 Cond = NewCond;
8756 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008757#if 0
8758 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008759 else if (Cond.getOpcode() == X86ISD::ADD ||
8760 Cond.getOpcode() == X86ISD::SUB ||
8761 Cond.getOpcode() == X86ISD::SMUL ||
8762 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008763 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008764#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008765
Evan Chengad9c0a32009-12-15 00:53:42 +00008766 // Look pass (and (setcc_carry (cmp ...)), 1).
8767 if (Cond.getOpcode() == ISD::AND &&
8768 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8769 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008770 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008771 Cond = Cond.getOperand(0);
8772 }
8773
Evan Cheng3f41d662007-10-08 22:16:29 +00008774 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8775 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008776 if (Cond.getOpcode() == X86ISD::SETCC ||
8777 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008778 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008779
Dan Gohman475871a2008-07-27 21:46:04 +00008780 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008781 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008782 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008783 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008784 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008785 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008786 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008787 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008788 default: break;
8789 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008790 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008791 // These can only come from an arithmetic instruction with overflow,
8792 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008793 Cond = Cond.getNode()->getOperand(1);
8794 addTest = false;
8795 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008796 }
Evan Cheng0488db92007-09-25 01:57:46 +00008797 }
Evan Cheng370e5342008-12-03 08:38:43 +00008798 } else {
8799 unsigned CondOpc;
8800 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8801 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008802 if (CondOpc == ISD::OR) {
8803 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8804 // two branches instead of an explicit OR instruction with a
8805 // separate test.
8806 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008807 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008808 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008809 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008810 Chain, Dest, CC, Cmp);
8811 CC = Cond.getOperand(1).getOperand(0);
8812 Cond = Cmp;
8813 addTest = false;
8814 }
8815 } else { // ISD::AND
8816 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8817 // two branches instead of an explicit AND instruction with a
8818 // separate test. However, we only do this if this block doesn't
8819 // have a fall-through edge, because this requires an explicit
8820 // jmp when the condition is false.
8821 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008822 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008823 Op.getNode()->hasOneUse()) {
8824 X86::CondCode CCode =
8825 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8826 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008827 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008828 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008829 // Look for an unconditional branch following this conditional branch.
8830 // We need this because we need to reverse the successors in order
8831 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008832 if (User->getOpcode() == ISD::BR) {
8833 SDValue FalseBB = User->getOperand(1);
8834 SDNode *NewBR =
8835 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008836 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008837 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008838 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008839
Dale Johannesene4d209d2009-02-03 20:21:25 +00008840 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008841 Chain, Dest, CC, Cmp);
8842 X86::CondCode CCode =
8843 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8844 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008845 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008846 Cond = Cmp;
8847 addTest = false;
8848 }
8849 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008850 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008851 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8852 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8853 // It should be transformed during dag combiner except when the condition
8854 // is set by a arithmetics with overflow node.
8855 X86::CondCode CCode =
8856 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8857 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008858 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008859 Cond = Cond.getOperand(0).getOperand(1);
8860 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00008861 }
Evan Cheng0488db92007-09-25 01:57:46 +00008862 }
8863
8864 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008865 // Look pass the truncate.
8866 if (Cond.getOpcode() == ISD::TRUNCATE)
8867 Cond = Cond.getOperand(0);
8868
8869 // We know the result of AND is compared against zero. Try to match
8870 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008871 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008872 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8873 if (NewSetCC.getNode()) {
8874 CC = NewSetCC.getOperand(0);
8875 Cond = NewSetCC.getOperand(1);
8876 addTest = false;
8877 }
8878 }
8879 }
8880
8881 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008882 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008883 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008884 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008885 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008886 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008887}
8888
Anton Korobeynikove060b532007-04-17 19:34:00 +00008889
8890// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8891// Calls to _alloca is needed to probe the stack when allocating more than 4k
8892// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8893// that the guard pages used by the OS virtual memory manager are allocated in
8894// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008895SDValue
8896X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008897 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008898 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
8899 EnableSegmentedStacks) &&
8900 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00008901 "are being used");
8902 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008903 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008904
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008905 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008906 SDValue Chain = Op.getOperand(0);
8907 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008908 // FIXME: Ensure alignment here
8909
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008910 bool Is64Bit = Subtarget->is64Bit();
8911 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008912
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008913 if (EnableSegmentedStacks) {
8914 MachineFunction &MF = DAG.getMachineFunction();
8915 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008916
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008917 if (Is64Bit) {
8918 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00008919 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008920 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008921
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008922 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
8923 I != E; I++)
8924 if (I->hasNestAttr())
8925 report_fatal_error("Cannot use segmented stacks with functions that "
8926 "have nested arguments.");
8927 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008928
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008929 const TargetRegisterClass *AddrRegClass =
8930 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
8931 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
8932 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
8933 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
8934 DAG.getRegister(Vreg, SPTy));
8935 SDValue Ops1[2] = { Value, Chain };
8936 return DAG.getMergeValues(Ops1, 2, dl);
8937 } else {
8938 SDValue Flag;
8939 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008940
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008941 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
8942 Flag = Chain.getValue(1);
8943 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008944
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008945 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
8946 Flag = Chain.getValue(1);
8947
8948 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
8949
8950 SDValue Ops1[2] = { Chain.getValue(0), Chain };
8951 return DAG.getMergeValues(Ops1, 2, dl);
8952 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008953}
8954
Dan Gohmand858e902010-04-17 15:26:15 +00008955SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008956 MachineFunction &MF = DAG.getMachineFunction();
8957 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8958
Dan Gohman69de1932008-02-06 22:27:42 +00008959 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008960 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008961
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008962 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008963 // vastart just stores the address of the VarArgsFrameIndex slot into the
8964 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008965 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8966 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008967 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8968 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008969 }
8970
8971 // __va_list_tag:
8972 // gp_offset (0 - 6 * 8)
8973 // fp_offset (48 - 48 + 8 * 16)
8974 // overflow_arg_area (point to parameters coming in memory).
8975 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008976 SmallVector<SDValue, 8> MemOps;
8977 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008978 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008979 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008980 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8981 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008982 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008983 MemOps.push_back(Store);
8984
8985 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008986 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008987 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008988 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008989 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
8990 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008991 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008992 MemOps.push_back(Store);
8993
8994 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00008995 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008996 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00008997 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8998 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008999 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9000 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009001 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009002 MemOps.push_back(Store);
9003
9004 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009005 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009006 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009007 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9008 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009009 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9010 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009011 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009012 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009013 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009014}
9015
Dan Gohmand858e902010-04-17 15:26:15 +00009016SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009017 assert(Subtarget->is64Bit() &&
9018 "LowerVAARG only handles 64-bit va_arg!");
9019 assert((Subtarget->isTargetLinux() ||
9020 Subtarget->isTargetDarwin()) &&
9021 "Unhandled target in LowerVAARG");
9022 assert(Op.getNode()->getNumOperands() == 4);
9023 SDValue Chain = Op.getOperand(0);
9024 SDValue SrcPtr = Op.getOperand(1);
9025 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9026 unsigned Align = Op.getConstantOperandVal(3);
9027 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009028
Dan Gohman320afb82010-10-12 18:00:49 +00009029 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009030 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009031 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9032 uint8_t ArgMode;
9033
9034 // Decide which area this value should be read from.
9035 // TODO: Implement the AMD64 ABI in its entirety. This simple
9036 // selection mechanism works only for the basic types.
9037 if (ArgVT == MVT::f80) {
9038 llvm_unreachable("va_arg for f80 not yet implemented");
9039 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9040 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9041 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9042 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9043 } else {
9044 llvm_unreachable("Unhandled argument type in LowerVAARG");
9045 }
9046
9047 if (ArgMode == 2) {
9048 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00009049 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009050 !(DAG.getMachineFunction()
9051 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009052 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009053 }
9054
9055 // Insert VAARG_64 node into the DAG
9056 // VAARG_64 returns two values: Variable Argument Address, Chain
9057 SmallVector<SDValue, 11> InstOps;
9058 InstOps.push_back(Chain);
9059 InstOps.push_back(SrcPtr);
9060 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9061 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9062 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9063 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9064 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9065 VTs, &InstOps[0], InstOps.size(),
9066 MVT::i64,
9067 MachinePointerInfo(SV),
9068 /*Align=*/0,
9069 /*Volatile=*/false,
9070 /*ReadMem=*/true,
9071 /*WriteMem=*/true);
9072 Chain = VAARG.getValue(1);
9073
9074 // Load the next argument and return it
9075 return DAG.getLoad(ArgVT, dl,
9076 Chain,
9077 VAARG,
9078 MachinePointerInfo(),
9079 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009080}
9081
Dan Gohmand858e902010-04-17 15:26:15 +00009082SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009083 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009084 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009085 SDValue Chain = Op.getOperand(0);
9086 SDValue DstPtr = Op.getOperand(1);
9087 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009088 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9089 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009090 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009091
Chris Lattnere72f2022010-09-21 05:40:29 +00009092 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009093 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009094 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009095 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009096}
9097
Dan Gohman475871a2008-07-27 21:46:04 +00009098SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009099X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009100 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009101 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009102 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009103 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009104 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009105 case Intrinsic::x86_sse_comieq_ss:
9106 case Intrinsic::x86_sse_comilt_ss:
9107 case Intrinsic::x86_sse_comile_ss:
9108 case Intrinsic::x86_sse_comigt_ss:
9109 case Intrinsic::x86_sse_comige_ss:
9110 case Intrinsic::x86_sse_comineq_ss:
9111 case Intrinsic::x86_sse_ucomieq_ss:
9112 case Intrinsic::x86_sse_ucomilt_ss:
9113 case Intrinsic::x86_sse_ucomile_ss:
9114 case Intrinsic::x86_sse_ucomigt_ss:
9115 case Intrinsic::x86_sse_ucomige_ss:
9116 case Intrinsic::x86_sse_ucomineq_ss:
9117 case Intrinsic::x86_sse2_comieq_sd:
9118 case Intrinsic::x86_sse2_comilt_sd:
9119 case Intrinsic::x86_sse2_comile_sd:
9120 case Intrinsic::x86_sse2_comigt_sd:
9121 case Intrinsic::x86_sse2_comige_sd:
9122 case Intrinsic::x86_sse2_comineq_sd:
9123 case Intrinsic::x86_sse2_ucomieq_sd:
9124 case Intrinsic::x86_sse2_ucomilt_sd:
9125 case Intrinsic::x86_sse2_ucomile_sd:
9126 case Intrinsic::x86_sse2_ucomigt_sd:
9127 case Intrinsic::x86_sse2_ucomige_sd:
9128 case Intrinsic::x86_sse2_ucomineq_sd: {
9129 unsigned Opc = 0;
9130 ISD::CondCode CC = ISD::SETCC_INVALID;
9131 switch (IntNo) {
9132 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009133 case Intrinsic::x86_sse_comieq_ss:
9134 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009135 Opc = X86ISD::COMI;
9136 CC = ISD::SETEQ;
9137 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009138 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009139 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009140 Opc = X86ISD::COMI;
9141 CC = ISD::SETLT;
9142 break;
9143 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009144 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009145 Opc = X86ISD::COMI;
9146 CC = ISD::SETLE;
9147 break;
9148 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009149 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009150 Opc = X86ISD::COMI;
9151 CC = ISD::SETGT;
9152 break;
9153 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009154 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009155 Opc = X86ISD::COMI;
9156 CC = ISD::SETGE;
9157 break;
9158 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009159 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009160 Opc = X86ISD::COMI;
9161 CC = ISD::SETNE;
9162 break;
9163 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009164 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009165 Opc = X86ISD::UCOMI;
9166 CC = ISD::SETEQ;
9167 break;
9168 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009169 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009170 Opc = X86ISD::UCOMI;
9171 CC = ISD::SETLT;
9172 break;
9173 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009174 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009175 Opc = X86ISD::UCOMI;
9176 CC = ISD::SETLE;
9177 break;
9178 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009179 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009180 Opc = X86ISD::UCOMI;
9181 CC = ISD::SETGT;
9182 break;
9183 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009184 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009185 Opc = X86ISD::UCOMI;
9186 CC = ISD::SETGE;
9187 break;
9188 case Intrinsic::x86_sse_ucomineq_ss:
9189 case Intrinsic::x86_sse2_ucomineq_sd:
9190 Opc = X86ISD::UCOMI;
9191 CC = ISD::SETNE;
9192 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009193 }
Evan Cheng734503b2006-09-11 02:19:56 +00009194
Dan Gohman475871a2008-07-27 21:46:04 +00009195 SDValue LHS = Op.getOperand(1);
9196 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009197 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009198 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009199 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9200 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9201 DAG.getConstant(X86CC, MVT::i8), Cond);
9202 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009203 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009204 // ptest and testp intrinsics. The intrinsic these come from are designed to
9205 // return an integer value, not just an instruction so lower it to the ptest
9206 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009207 case Intrinsic::x86_sse41_ptestz:
9208 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009209 case Intrinsic::x86_sse41_ptestnzc:
9210 case Intrinsic::x86_avx_ptestz_256:
9211 case Intrinsic::x86_avx_ptestc_256:
9212 case Intrinsic::x86_avx_ptestnzc_256:
9213 case Intrinsic::x86_avx_vtestz_ps:
9214 case Intrinsic::x86_avx_vtestc_ps:
9215 case Intrinsic::x86_avx_vtestnzc_ps:
9216 case Intrinsic::x86_avx_vtestz_pd:
9217 case Intrinsic::x86_avx_vtestc_pd:
9218 case Intrinsic::x86_avx_vtestnzc_pd:
9219 case Intrinsic::x86_avx_vtestz_ps_256:
9220 case Intrinsic::x86_avx_vtestc_ps_256:
9221 case Intrinsic::x86_avx_vtestnzc_ps_256:
9222 case Intrinsic::x86_avx_vtestz_pd_256:
9223 case Intrinsic::x86_avx_vtestc_pd_256:
9224 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9225 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009226 unsigned X86CC = 0;
9227 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009228 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009229 case Intrinsic::x86_avx_vtestz_ps:
9230 case Intrinsic::x86_avx_vtestz_pd:
9231 case Intrinsic::x86_avx_vtestz_ps_256:
9232 case Intrinsic::x86_avx_vtestz_pd_256:
9233 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009234 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009235 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009236 // ZF = 1
9237 X86CC = X86::COND_E;
9238 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009239 case Intrinsic::x86_avx_vtestc_ps:
9240 case Intrinsic::x86_avx_vtestc_pd:
9241 case Intrinsic::x86_avx_vtestc_ps_256:
9242 case Intrinsic::x86_avx_vtestc_pd_256:
9243 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009244 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009245 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009246 // CF = 1
9247 X86CC = X86::COND_B;
9248 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009249 case Intrinsic::x86_avx_vtestnzc_ps:
9250 case Intrinsic::x86_avx_vtestnzc_pd:
9251 case Intrinsic::x86_avx_vtestnzc_ps_256:
9252 case Intrinsic::x86_avx_vtestnzc_pd_256:
9253 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009254 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009255 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009256 // ZF and CF = 0
9257 X86CC = X86::COND_A;
9258 break;
9259 }
Eric Christopherfd179292009-08-27 18:07:15 +00009260
Eric Christopher71c67532009-07-29 00:28:05 +00009261 SDValue LHS = Op.getOperand(1);
9262 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009263 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9264 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009265 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9266 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9267 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009268 }
Evan Cheng5759f972008-05-04 09:15:50 +00009269
9270 // Fix vector shift instructions where the last operand is a non-immediate
9271 // i32 value.
9272 case Intrinsic::x86_sse2_pslli_w:
9273 case Intrinsic::x86_sse2_pslli_d:
9274 case Intrinsic::x86_sse2_pslli_q:
9275 case Intrinsic::x86_sse2_psrli_w:
9276 case Intrinsic::x86_sse2_psrli_d:
9277 case Intrinsic::x86_sse2_psrli_q:
9278 case Intrinsic::x86_sse2_psrai_w:
9279 case Intrinsic::x86_sse2_psrai_d:
9280 case Intrinsic::x86_mmx_pslli_w:
9281 case Intrinsic::x86_mmx_pslli_d:
9282 case Intrinsic::x86_mmx_pslli_q:
9283 case Intrinsic::x86_mmx_psrli_w:
9284 case Intrinsic::x86_mmx_psrli_d:
9285 case Intrinsic::x86_mmx_psrli_q:
9286 case Intrinsic::x86_mmx_psrai_w:
9287 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009288 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009289 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009290 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009291
9292 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009293 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009294 switch (IntNo) {
9295 case Intrinsic::x86_sse2_pslli_w:
9296 NewIntNo = Intrinsic::x86_sse2_psll_w;
9297 break;
9298 case Intrinsic::x86_sse2_pslli_d:
9299 NewIntNo = Intrinsic::x86_sse2_psll_d;
9300 break;
9301 case Intrinsic::x86_sse2_pslli_q:
9302 NewIntNo = Intrinsic::x86_sse2_psll_q;
9303 break;
9304 case Intrinsic::x86_sse2_psrli_w:
9305 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9306 break;
9307 case Intrinsic::x86_sse2_psrli_d:
9308 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9309 break;
9310 case Intrinsic::x86_sse2_psrli_q:
9311 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9312 break;
9313 case Intrinsic::x86_sse2_psrai_w:
9314 NewIntNo = Intrinsic::x86_sse2_psra_w;
9315 break;
9316 case Intrinsic::x86_sse2_psrai_d:
9317 NewIntNo = Intrinsic::x86_sse2_psra_d;
9318 break;
9319 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009320 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009321 switch (IntNo) {
9322 case Intrinsic::x86_mmx_pslli_w:
9323 NewIntNo = Intrinsic::x86_mmx_psll_w;
9324 break;
9325 case Intrinsic::x86_mmx_pslli_d:
9326 NewIntNo = Intrinsic::x86_mmx_psll_d;
9327 break;
9328 case Intrinsic::x86_mmx_pslli_q:
9329 NewIntNo = Intrinsic::x86_mmx_psll_q;
9330 break;
9331 case Intrinsic::x86_mmx_psrli_w:
9332 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9333 break;
9334 case Intrinsic::x86_mmx_psrli_d:
9335 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9336 break;
9337 case Intrinsic::x86_mmx_psrli_q:
9338 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9339 break;
9340 case Intrinsic::x86_mmx_psrai_w:
9341 NewIntNo = Intrinsic::x86_mmx_psra_w;
9342 break;
9343 case Intrinsic::x86_mmx_psrai_d:
9344 NewIntNo = Intrinsic::x86_mmx_psra_d;
9345 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009346 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009347 }
9348 break;
9349 }
9350 }
Mon P Wangefa42202009-09-03 19:56:25 +00009351
9352 // The vector shift intrinsics with scalars uses 32b shift amounts but
9353 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9354 // to be zero.
9355 SDValue ShOps[4];
9356 ShOps[0] = ShAmt;
9357 ShOps[1] = DAG.getConstant(0, MVT::i32);
9358 if (ShAmtVT == MVT::v4i32) {
9359 ShOps[2] = DAG.getUNDEF(MVT::i32);
9360 ShOps[3] = DAG.getUNDEF(MVT::i32);
9361 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9362 } else {
9363 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009364// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009365 }
9366
Owen Andersone50ed302009-08-10 22:56:29 +00009367 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009368 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009369 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009370 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009371 Op.getOperand(1), ShAmt);
9372 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009373 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009374}
Evan Cheng72261582005-12-20 06:22:03 +00009375
Dan Gohmand858e902010-04-17 15:26:15 +00009376SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9377 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009378 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9379 MFI->setReturnAddressIsTaken(true);
9380
Bill Wendling64e87322009-01-16 19:25:27 +00009381 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009382 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009383
9384 if (Depth > 0) {
9385 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9386 SDValue Offset =
9387 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009388 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009389 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009390 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009391 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00009392 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009393 }
9394
9395 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009396 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009397 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009398 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009399}
9400
Dan Gohmand858e902010-04-17 15:26:15 +00009401SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009402 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9403 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009404
Owen Andersone50ed302009-08-10 22:56:29 +00009405 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009406 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009407 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9408 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009409 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009410 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009411 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9412 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00009413 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009414 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009415}
9416
Dan Gohman475871a2008-07-27 21:46:04 +00009417SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009418 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009419 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009420}
9421
Dan Gohmand858e902010-04-17 15:26:15 +00009422SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009423 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009424 SDValue Chain = Op.getOperand(0);
9425 SDValue Offset = Op.getOperand(1);
9426 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009427 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009428
Dan Gohmand8816272010-08-11 18:14:00 +00009429 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9430 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9431 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009432 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009433
Dan Gohmand8816272010-08-11 18:14:00 +00009434 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9435 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009436 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009437 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9438 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009439 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009440 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009441
Dale Johannesene4d209d2009-02-03 20:21:25 +00009442 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009443 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009444 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009445}
9446
Duncan Sands4a544a72011-09-06 13:37:06 +00009447SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9448 SelectionDAG &DAG) const {
9449 return Op.getOperand(0);
9450}
9451
9452SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9453 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009454 SDValue Root = Op.getOperand(0);
9455 SDValue Trmp = Op.getOperand(1); // trampoline
9456 SDValue FPtr = Op.getOperand(2); // nested function
9457 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009458 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009459
Dan Gohman69de1932008-02-06 22:27:42 +00009460 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009461
9462 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009463 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009464
9465 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009466 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9467 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009468
Evan Cheng0e6a0522011-07-18 20:57:22 +00009469 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9470 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009471
9472 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9473
9474 // Load the pointer to the nested function into R11.
9475 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009476 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009477 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009478 Addr, MachinePointerInfo(TrmpAddr),
9479 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009480
Owen Anderson825b72b2009-08-11 20:47:22 +00009481 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9482 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009483 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9484 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009485 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009486
9487 // Load the 'nest' parameter value into R10.
9488 // R10 is specified in X86CallingConv.td
9489 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009490 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9491 DAG.getConstant(10, MVT::i64));
9492 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009493 Addr, MachinePointerInfo(TrmpAddr, 10),
9494 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009495
Owen Anderson825b72b2009-08-11 20:47:22 +00009496 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9497 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009498 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9499 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009500 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009501
9502 // Jump to the nested function.
9503 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009504 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9505 DAG.getConstant(20, MVT::i64));
9506 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009507 Addr, MachinePointerInfo(TrmpAddr, 20),
9508 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009509
9510 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009511 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9512 DAG.getConstant(22, MVT::i64));
9513 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009514 MachinePointerInfo(TrmpAddr, 22),
9515 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009516
Duncan Sands4a544a72011-09-06 13:37:06 +00009517 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009518 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009519 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009520 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009521 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009522 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009523
9524 switch (CC) {
9525 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009526 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009527 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009528 case CallingConv::X86_StdCall: {
9529 // Pass 'nest' parameter in ECX.
9530 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009531 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009532
9533 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009534 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009535 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009536
Chris Lattner58d74912008-03-12 17:45:29 +00009537 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009538 unsigned InRegCount = 0;
9539 unsigned Idx = 1;
9540
9541 for (FunctionType::param_iterator I = FTy->param_begin(),
9542 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009543 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009544 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009545 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009546
9547 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009548 report_fatal_error("Nest register in use - reduce number of inreg"
9549 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009550 }
9551 }
9552 break;
9553 }
9554 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009555 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009556 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009557 // Pass 'nest' parameter in EAX.
9558 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009559 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009560 break;
9561 }
9562
Dan Gohman475871a2008-07-27 21:46:04 +00009563 SDValue OutChains[4];
9564 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009565
Owen Anderson825b72b2009-08-11 20:47:22 +00009566 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9567 DAG.getConstant(10, MVT::i32));
9568 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009569
Chris Lattnera62fe662010-02-05 19:20:30 +00009570 // This is storing the opcode for MOV32ri.
9571 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009572 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009573 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009574 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009575 Trmp, MachinePointerInfo(TrmpAddr),
9576 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009577
Owen Anderson825b72b2009-08-11 20:47:22 +00009578 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9579 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009580 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9581 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009582 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009583
Chris Lattnera62fe662010-02-05 19:20:30 +00009584 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009585 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9586 DAG.getConstant(5, MVT::i32));
9587 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009588 MachinePointerInfo(TrmpAddr, 5),
9589 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009590
Owen Anderson825b72b2009-08-11 20:47:22 +00009591 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9592 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009593 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9594 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009595 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009596
Duncan Sands4a544a72011-09-06 13:37:06 +00009597 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009598 }
9599}
9600
Dan Gohmand858e902010-04-17 15:26:15 +00009601SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9602 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009603 /*
9604 The rounding mode is in bits 11:10 of FPSR, and has the following
9605 settings:
9606 00 Round to nearest
9607 01 Round to -inf
9608 10 Round to +inf
9609 11 Round to 0
9610
9611 FLT_ROUNDS, on the other hand, expects the following:
9612 -1 Undefined
9613 0 Round to 0
9614 1 Round to nearest
9615 2 Round to +inf
9616 3 Round to -inf
9617
9618 To perform the conversion, we do:
9619 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9620 */
9621
9622 MachineFunction &MF = DAG.getMachineFunction();
9623 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009624 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009625 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009626 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009627 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009628
9629 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009630 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009631 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009632
Michael J. Spencerec38de22010-10-10 22:04:20 +00009633
Chris Lattner2156b792010-09-22 01:11:26 +00009634 MachineMemOperand *MMO =
9635 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9636 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009637
Chris Lattner2156b792010-09-22 01:11:26 +00009638 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9639 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9640 DAG.getVTList(MVT::Other),
9641 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009642
9643 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009644 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00009645 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009646
9647 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009648 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009649 DAG.getNode(ISD::SRL, DL, MVT::i16,
9650 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009651 CWD, DAG.getConstant(0x800, MVT::i16)),
9652 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009653 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009654 DAG.getNode(ISD::SRL, DL, MVT::i16,
9655 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009656 CWD, DAG.getConstant(0x400, MVT::i16)),
9657 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009658
Dan Gohman475871a2008-07-27 21:46:04 +00009659 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009660 DAG.getNode(ISD::AND, DL, MVT::i16,
9661 DAG.getNode(ISD::ADD, DL, MVT::i16,
9662 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009663 DAG.getConstant(1, MVT::i16)),
9664 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009665
9666
Duncan Sands83ec4b62008-06-06 12:08:01 +00009667 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009668 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009669}
9670
Dan Gohmand858e902010-04-17 15:26:15 +00009671SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009672 EVT VT = Op.getValueType();
9673 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009674 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009675 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009676
9677 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009678 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009679 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009680 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009681 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009682 }
Evan Cheng18efe262007-12-14 02:13:44 +00009683
Evan Cheng152804e2007-12-14 08:30:15 +00009684 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009685 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009686 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009687
9688 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009689 SDValue Ops[] = {
9690 Op,
9691 DAG.getConstant(NumBits+NumBits-1, OpVT),
9692 DAG.getConstant(X86::COND_E, MVT::i8),
9693 Op.getValue(1)
9694 };
9695 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009696
9697 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009698 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009699
Owen Anderson825b72b2009-08-11 20:47:22 +00009700 if (VT == MVT::i8)
9701 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009702 return Op;
9703}
9704
Dan Gohmand858e902010-04-17 15:26:15 +00009705SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009706 EVT VT = Op.getValueType();
9707 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009708 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009709 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009710
9711 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009712 if (VT == MVT::i8) {
9713 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009714 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009715 }
Evan Cheng152804e2007-12-14 08:30:15 +00009716
9717 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009718 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009719 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009720
9721 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009722 SDValue Ops[] = {
9723 Op,
9724 DAG.getConstant(NumBits, OpVT),
9725 DAG.getConstant(X86::COND_E, MVT::i8),
9726 Op.getValue(1)
9727 };
9728 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009729
Owen Anderson825b72b2009-08-11 20:47:22 +00009730 if (VT == MVT::i8)
9731 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009732 return Op;
9733}
9734
Craig Topper13894fa2011-08-24 06:14:18 +00009735// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9736// ones, and then concatenate the result back.
9737static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009738 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009739
9740 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9741 "Unsupported value type for operation");
9742
9743 int NumElems = VT.getVectorNumElements();
9744 DebugLoc dl = Op.getDebugLoc();
9745 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9746 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9747
9748 // Extract the LHS vectors
9749 SDValue LHS = Op.getOperand(0);
9750 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9751 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9752
9753 // Extract the RHS vectors
9754 SDValue RHS = Op.getOperand(1);
9755 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9756 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9757
9758 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9759 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9760
9761 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9762 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
9763 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
9764}
9765
9766SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
9767 assert(Op.getValueType().getSizeInBits() == 256 &&
9768 Op.getValueType().isInteger() &&
9769 "Only handle AVX 256-bit vector integer operation");
9770 return Lower256IntArith(Op, DAG);
9771}
9772
9773SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
9774 assert(Op.getValueType().getSizeInBits() == 256 &&
9775 Op.getValueType().isInteger() &&
9776 "Only handle AVX 256-bit vector integer operation");
9777 return Lower256IntArith(Op, DAG);
9778}
9779
9780SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
9781 EVT VT = Op.getValueType();
9782
9783 // Decompose 256-bit ops into smaller 128-bit ops.
9784 if (VT.getSizeInBits() == 256)
9785 return Lower256IntArith(Op, DAG);
9786
Owen Anderson825b72b2009-08-11 20:47:22 +00009787 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009788 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00009789
Mon P Wangaf9b9522008-12-18 21:42:19 +00009790 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
9791 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
9792 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
9793 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
9794 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
9795 //
9796 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
9797 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
9798 // return AloBlo + AloBhi + AhiBlo;
9799
9800 SDValue A = Op.getOperand(0);
9801 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009802
Dale Johannesene4d209d2009-02-03 20:21:25 +00009803 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009804 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9805 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009806 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009807 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9808 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009809 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009810 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009811 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009812 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009813 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009814 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009815 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009816 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009817 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009818 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009819 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9820 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009821 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009822 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9823 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009824 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9825 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009826 return Res;
9827}
9828
Nadav Rotem43012222011-05-11 08:12:09 +00009829SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
9830
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009831 EVT VT = Op.getValueType();
9832 DebugLoc dl = Op.getDebugLoc();
9833 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00009834 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009835 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009836
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009837 if (!(Subtarget->hasSSE2() || Subtarget->hasAVX()))
9838 return SDValue();
9839
9840 // Decompose 256-bit shifts into smaller 128-bit shifts.
9841 if (VT.getSizeInBits() == 256) {
9842 int NumElems = VT.getVectorNumElements();
9843 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9844 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9845
9846 // Extract the two vectors
9847 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
9848 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
9849 DAG, dl);
9850
9851 // Recreate the shift amount vectors
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009852 SDValue Amt1, Amt2;
9853 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
9854 // Constant shift amount
9855 SmallVector<SDValue, 4> Amt1Csts;
9856 SmallVector<SDValue, 4> Amt2Csts;
9857 for (int i = 0; i < NumElems/2; ++i)
9858 Amt1Csts.push_back(Amt->getOperand(i));
9859 for (int i = NumElems/2; i < NumElems; ++i)
9860 Amt2Csts.push_back(Amt->getOperand(i));
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009861
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009862 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9863 &Amt1Csts[0], NumElems/2);
9864 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9865 &Amt2Csts[0], NumElems/2);
9866 } else {
9867 // Variable shift amount
9868 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
9869 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
9870 DAG, dl);
9871 }
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009872
9873 // Issue new vector shifts for the smaller types
9874 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
9875 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
9876
9877 // Concatenate the result back
9878 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
9879 }
Nate Begeman51409212010-07-28 00:21:48 +00009880
Nadav Rotem43012222011-05-11 08:12:09 +00009881 // Optimize shl/srl/sra with constant shift amount.
9882 if (isSplatVector(Amt.getNode())) {
9883 SDValue SclrAmt = Amt->getOperand(0);
9884 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
9885 uint64_t ShiftAmt = C->getZExtValue();
9886
9887 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
9888 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9889 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9890 R, DAG.getConstant(ShiftAmt, MVT::i32));
9891
9892 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
9893 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9894 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9895 R, DAG.getConstant(ShiftAmt, MVT::i32));
9896
9897 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
9898 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9899 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9900 R, DAG.getConstant(ShiftAmt, MVT::i32));
9901
9902 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
9903 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9904 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9905 R, DAG.getConstant(ShiftAmt, MVT::i32));
9906
9907 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
9908 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9909 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
9910 R, DAG.getConstant(ShiftAmt, MVT::i32));
9911
9912 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
9913 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9914 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
9915 R, DAG.getConstant(ShiftAmt, MVT::i32));
9916
9917 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
9918 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9919 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
9920 R, DAG.getConstant(ShiftAmt, MVT::i32));
9921
9922 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
9923 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9924 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
9925 R, DAG.getConstant(ShiftAmt, MVT::i32));
9926 }
9927 }
9928
9929 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00009930 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009931 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9932 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9933 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
9934
9935 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009936
Nate Begeman51409212010-07-28 00:21:48 +00009937 std::vector<Constant*> CV(4, CI);
9938 Constant *C = ConstantVector::get(CV);
9939 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9940 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009941 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009942 false, false, 16);
9943
9944 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009945 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009946 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
9947 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
9948 }
Nadav Rotem43012222011-05-11 08:12:09 +00009949 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009950 // a = a << 5;
9951 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9952 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9953 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
9954
9955 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
9956 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
9957
9958 std::vector<Constant*> CVM1(16, CM1);
9959 std::vector<Constant*> CVM2(16, CM2);
9960 Constant *C = ConstantVector::get(CVM1);
9961 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9962 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009963 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009964 false, false, 16);
9965
9966 // r = pblendv(r, psllw(r & (char16)15, 4), a);
9967 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9968 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9969 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
9970 DAG.getConstant(4, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +00009971 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +00009972 // a += a
9973 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009974
Nate Begeman51409212010-07-28 00:21:48 +00009975 C = ConstantVector::get(CVM2);
9976 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9977 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009978 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009979 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009980
Nate Begeman51409212010-07-28 00:21:48 +00009981 // r = pblendv(r, psllw(r & (char16)63, 2), a);
9982 M = DAG.getNode(ISD::AND, dl, VT, R, M);
9983 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9984 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
9985 DAG.getConstant(2, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +00009986 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +00009987 // a += a
9988 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009989
Nate Begeman51409212010-07-28 00:21:48 +00009990 // return pblendv(r, r+r, a);
Nadav Rotemfbad25e2011-09-11 15:02:23 +00009991 R = DAG.getNode(ISD::VSELECT, dl, VT, Op,
9992 R, DAG.getNode(ISD::ADD, dl, VT, R, R));
Nate Begeman51409212010-07-28 00:21:48 +00009993 return R;
9994 }
9995 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009996}
Mon P Wangaf9b9522008-12-18 21:42:19 +00009997
Dan Gohmand858e902010-04-17 15:26:15 +00009998SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00009999 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10000 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010001 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10002 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010003 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010004 SDValue LHS = N->getOperand(0);
10005 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010006 unsigned BaseOp = 0;
10007 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010008 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010009 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010010 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010011 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010012 // A subtract of one will be selected as a INC. Note that INC doesn't
10013 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010014 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10015 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010016 BaseOp = X86ISD::INC;
10017 Cond = X86::COND_O;
10018 break;
10019 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010020 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010021 Cond = X86::COND_O;
10022 break;
10023 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010024 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010025 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010026 break;
10027 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010028 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10029 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010030 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10031 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010032 BaseOp = X86ISD::DEC;
10033 Cond = X86::COND_O;
10034 break;
10035 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010036 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010037 Cond = X86::COND_O;
10038 break;
10039 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010040 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010041 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010042 break;
10043 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010044 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010045 Cond = X86::COND_O;
10046 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010047 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10048 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10049 MVT::i32);
10050 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010051
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010052 SDValue SetCC =
10053 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10054 DAG.getConstant(X86::COND_O, MVT::i32),
10055 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010056
Dan Gohman6e5fda22011-07-22 18:45:15 +000010057 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010058 }
Bill Wendling74c37652008-12-09 22:08:41 +000010059 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010060
Bill Wendling61edeb52008-12-02 01:06:39 +000010061 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010062 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010063 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010064
Bill Wendling61edeb52008-12-02 01:06:39 +000010065 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010066 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10067 DAG.getConstant(Cond, MVT::i32),
10068 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010069
Dan Gohman6e5fda22011-07-22 18:45:15 +000010070 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010071}
10072
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010073SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10074 DebugLoc dl = Op.getDebugLoc();
10075 SDNode* Node = Op.getNode();
10076 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
10077 EVT VT = Node->getValueType(0);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010078 if (Subtarget->hasSSE2() && VT.isVector()) {
10079 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10080 ExtraVT.getScalarType().getSizeInBits();
10081 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10082
10083 unsigned SHLIntrinsicsID = 0;
10084 unsigned SRAIntrinsicsID = 0;
10085 switch (VT.getSimpleVT().SimpleTy) {
10086 default:
10087 return SDValue();
10088 case MVT::v2i64: {
10089 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_q;
10090 SRAIntrinsicsID = 0;
10091 break;
10092 }
10093 case MVT::v4i32: {
10094 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10095 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10096 break;
10097 }
10098 case MVT::v8i16: {
10099 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10100 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10101 break;
10102 }
10103 }
10104
10105 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10106 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
10107 Node->getOperand(0), ShAmt);
10108
10109 // In case of 1 bit sext, no need to shr
10110 if (ExtraVT.getScalarType().getSizeInBits() == 1) return Tmp1;
10111
10112 if (SRAIntrinsicsID) {
10113 Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10114 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10115 Tmp1, ShAmt);
10116 }
10117 return Tmp1;
10118 }
10119
10120 return SDValue();
10121}
10122
10123
Eric Christopher9a9d2752010-07-22 02:48:34 +000010124SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10125 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010126
Eric Christopher77ed1352011-07-08 00:04:56 +000010127 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10128 // There isn't any reason to disable it if the target processor supports it.
10129 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010130 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010131 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010132 SDValue Ops[] = {
10133 DAG.getRegister(X86::ESP, MVT::i32), // Base
10134 DAG.getTargetConstant(1, MVT::i8), // Scale
10135 DAG.getRegister(0, MVT::i32), // Index
10136 DAG.getTargetConstant(0, MVT::i32), // Disp
10137 DAG.getRegister(0, MVT::i32), // Segment.
10138 Zero,
10139 Chain
10140 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010141 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010142 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10143 array_lengthof(Ops));
10144 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010145 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010146
Eric Christopher9a9d2752010-07-22 02:48:34 +000010147 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010148 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010149 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010150
Chris Lattner132929a2010-08-14 17:26:09 +000010151 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10152 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10153 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10154 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010155
Chris Lattner132929a2010-08-14 17:26:09 +000010156 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10157 if (!Op1 && !Op2 && !Op3 && Op4)
10158 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010159
Chris Lattner132929a2010-08-14 17:26:09 +000010160 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10161 if (Op1 && !Op2 && !Op3 && !Op4)
10162 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010163
10164 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010165 // (MFENCE)>;
10166 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010167}
10168
Eli Friedman14648462011-07-27 22:21:52 +000010169SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10170 SelectionDAG &DAG) const {
10171 DebugLoc dl = Op.getDebugLoc();
10172 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10173 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10174 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10175 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10176
10177 // The only fence that needs an instruction is a sequentially-consistent
10178 // cross-thread fence.
10179 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10180 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10181 // no-sse2). There isn't any reason to disable it if the target processor
10182 // supports it.
10183 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
10184 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10185
10186 SDValue Chain = Op.getOperand(0);
10187 SDValue Zero = DAG.getConstant(0, MVT::i32);
10188 SDValue Ops[] = {
10189 DAG.getRegister(X86::ESP, MVT::i32), // Base
10190 DAG.getTargetConstant(1, MVT::i8), // Scale
10191 DAG.getRegister(0, MVT::i32), // Index
10192 DAG.getTargetConstant(0, MVT::i32), // Disp
10193 DAG.getRegister(0, MVT::i32), // Segment.
10194 Zero,
10195 Chain
10196 };
10197 SDNode *Res =
10198 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10199 array_lengthof(Ops));
10200 return SDValue(Res, 0);
10201 }
10202
10203 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10204 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10205}
10206
10207
Dan Gohmand858e902010-04-17 15:26:15 +000010208SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010209 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010210 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010211 unsigned Reg = 0;
10212 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010213 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010214 default:
10215 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010216 case MVT::i8: Reg = X86::AL; size = 1; break;
10217 case MVT::i16: Reg = X86::AX; size = 2; break;
10218 case MVT::i32: Reg = X86::EAX; size = 4; break;
10219 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010220 assert(Subtarget->is64Bit() && "Node not type legal!");
10221 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010222 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010223 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010224 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010225 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010226 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010227 Op.getOperand(1),
10228 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010229 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010230 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010231 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010232 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10233 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10234 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010235 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010236 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010237 return cpOut;
10238}
10239
Duncan Sands1607f052008-12-01 11:39:25 +000010240SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010241 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010242 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010243 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010244 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010245 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010246 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010247 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10248 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010249 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010250 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10251 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010252 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010253 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010254 rdx.getValue(1)
10255 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010256 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010257}
10258
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010259SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010260 SelectionDAG &DAG) const {
10261 EVT SrcVT = Op.getOperand(0).getValueType();
10262 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +000010263 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
10264 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010265 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010266 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010267 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010268 // i64 <=> MMX conversions are Legal.
10269 if (SrcVT==MVT::i64 && DstVT.isVector())
10270 return Op;
10271 if (DstVT==MVT::i64 && SrcVT.isVector())
10272 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010273 // MMX <=> MMX conversions are Legal.
10274 if (SrcVT.isVector() && DstVT.isVector())
10275 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010276 // All other conversions need to be expanded.
10277 return SDValue();
10278}
Chris Lattner5b856542010-12-20 00:59:46 +000010279
Dan Gohmand858e902010-04-17 15:26:15 +000010280SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010281 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010282 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010283 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010284 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010285 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010286 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010287 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010288 Node->getOperand(0),
10289 Node->getOperand(1), negOp,
10290 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010291 cast<AtomicSDNode>(Node)->getAlignment(),
10292 cast<AtomicSDNode>(Node)->getOrdering(),
10293 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010294}
10295
Eli Friedman327236c2011-08-24 20:50:09 +000010296static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10297 SDNode *Node = Op.getNode();
10298 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010299 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010300
10301 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010302 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10303 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10304 // (The only way to get a 16-byte store is cmpxchg16b)
10305 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10306 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10307 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010308 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10309 cast<AtomicSDNode>(Node)->getMemoryVT(),
10310 Node->getOperand(0),
10311 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010312 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010313 cast<AtomicSDNode>(Node)->getOrdering(),
10314 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010315 return Swap.getValue(1);
10316 }
10317 // Other atomic stores have a simple pattern.
10318 return Op;
10319}
10320
Chris Lattner5b856542010-12-20 00:59:46 +000010321static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10322 EVT VT = Op.getNode()->getValueType(0);
10323
10324 // Let legalize expand this if it isn't a legal type yet.
10325 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10326 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010327
Chris Lattner5b856542010-12-20 00:59:46 +000010328 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010329
Chris Lattner5b856542010-12-20 00:59:46 +000010330 unsigned Opc;
10331 bool ExtraOp = false;
10332 switch (Op.getOpcode()) {
10333 default: assert(0 && "Invalid code");
10334 case ISD::ADDC: Opc = X86ISD::ADD; break;
10335 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10336 case ISD::SUBC: Opc = X86ISD::SUB; break;
10337 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10338 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010339
Chris Lattner5b856542010-12-20 00:59:46 +000010340 if (!ExtraOp)
10341 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10342 Op.getOperand(1));
10343 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10344 Op.getOperand(1), Op.getOperand(2));
10345}
10346
Evan Cheng0db9fe62006-04-25 20:13:52 +000010347/// LowerOperation - Provide custom lowering hooks for some operations.
10348///
Dan Gohmand858e902010-04-17 15:26:15 +000010349SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010350 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010351 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010352 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010353 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010354 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010355 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10356 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010357 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010358 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010359 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010360 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10361 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10362 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010363 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010364 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010365 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10366 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10367 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010368 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010369 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010370 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010371 case ISD::SHL_PARTS:
10372 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010373 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010374 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010375 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010376 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010377 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010378 case ISD::FABS: return LowerFABS(Op, DAG);
10379 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010380 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010381 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010382 case ISD::SETCC: return LowerSETCC(Op, DAG);
10383 case ISD::SELECT: return LowerSELECT(Op, DAG);
10384 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010385 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010386 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010387 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010388 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010389 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010390 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10391 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010392 case ISD::FRAME_TO_ARGS_OFFSET:
10393 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010394 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010395 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010396 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10397 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010398 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010399 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10400 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010401 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010402 case ISD::SRA:
10403 case ISD::SRL:
10404 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010405 case ISD::SADDO:
10406 case ISD::UADDO:
10407 case ISD::SSUBO:
10408 case ISD::USUBO:
10409 case ISD::SMULO:
10410 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010411 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010412 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010413 case ISD::ADDC:
10414 case ISD::ADDE:
10415 case ISD::SUBC:
10416 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010417 case ISD::ADD: return LowerADD(Op, DAG);
10418 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010419 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010420}
10421
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010422static void ReplaceATOMIC_LOAD(SDNode *Node,
10423 SmallVectorImpl<SDValue> &Results,
10424 SelectionDAG &DAG) {
10425 DebugLoc dl = Node->getDebugLoc();
10426 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10427
10428 // Convert wide load -> cmpxchg8b/cmpxchg16b
10429 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10430 // (The only way to get a 16-byte load is cmpxchg16b)
10431 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010432 SDValue Zero = DAG.getConstant(0, VT);
10433 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010434 Node->getOperand(0),
10435 Node->getOperand(1), Zero, Zero,
10436 cast<AtomicSDNode>(Node)->getMemOperand(),
10437 cast<AtomicSDNode>(Node)->getOrdering(),
10438 cast<AtomicSDNode>(Node)->getSynchScope());
10439 Results.push_back(Swap.getValue(0));
10440 Results.push_back(Swap.getValue(1));
10441}
10442
Duncan Sands1607f052008-12-01 11:39:25 +000010443void X86TargetLowering::
10444ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010445 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010446 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010447 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +000010448 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010449
10450 SDValue Chain = Node->getOperand(0);
10451 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010452 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010453 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010454 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010455 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010456 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010457 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010458 SDValue Result =
10459 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10460 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010461 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010462 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010463 Results.push_back(Result.getValue(2));
10464}
10465
Duncan Sands126d9072008-07-04 11:47:58 +000010466/// ReplaceNodeResults - Replace a node with an illegal result type
10467/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010468void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10469 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010470 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010471 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010472 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010473 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010474 assert(false && "Do not know how to custom type legalize this operation!");
10475 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010476 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010477 case ISD::ADDC:
10478 case ISD::ADDE:
10479 case ISD::SUBC:
10480 case ISD::SUBE:
10481 // We don't want to expand or promote these.
10482 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010483 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010484 std::pair<SDValue,SDValue> Vals =
10485 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010486 SDValue FIST = Vals.first, StackSlot = Vals.second;
10487 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010488 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010489 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010490 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
10491 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010492 }
10493 return;
10494 }
10495 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010496 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010497 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010498 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010499 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010500 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010501 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010502 eax.getValue(2));
10503 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10504 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010505 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010506 Results.push_back(edx.getValue(1));
10507 return;
10508 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010509 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010510 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010511 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010512 bool Regs64bit = T == MVT::i128;
10513 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010514 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010515 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10516 DAG.getConstant(0, HalfT));
10517 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10518 DAG.getConstant(1, HalfT));
10519 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10520 Regs64bit ? X86::RAX : X86::EAX,
10521 cpInL, SDValue());
10522 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10523 Regs64bit ? X86::RDX : X86::EDX,
10524 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010525 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010526 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10527 DAG.getConstant(0, HalfT));
10528 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10529 DAG.getConstant(1, HalfT));
10530 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10531 Regs64bit ? X86::RBX : X86::EBX,
10532 swapInL, cpInH.getValue(1));
10533 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10534 Regs64bit ? X86::RCX : X86::ECX,
10535 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010536 SDValue Ops[] = { swapInH.getValue(0),
10537 N->getOperand(1),
10538 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010539 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010540 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010541 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10542 X86ISD::LCMPXCHG8_DAG;
10543 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010544 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010545 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10546 Regs64bit ? X86::RAX : X86::EAX,
10547 HalfT, Result.getValue(1));
10548 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10549 Regs64bit ? X86::RDX : X86::EDX,
10550 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010551 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010552 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010553 Results.push_back(cpOutH.getValue(1));
10554 return;
10555 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010556 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010557 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10558 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010559 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010560 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10561 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010562 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010563 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10564 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010565 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010566 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10567 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010568 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010569 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10570 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010571 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010572 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10573 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010574 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010575 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10576 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010577 case ISD::ATOMIC_LOAD:
10578 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010579 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010580}
10581
Evan Cheng72261582005-12-20 06:22:03 +000010582const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10583 switch (Opcode) {
10584 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010585 case X86ISD::BSF: return "X86ISD::BSF";
10586 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010587 case X86ISD::SHLD: return "X86ISD::SHLD";
10588 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010589 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010590 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010591 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010592 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010593 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010594 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010595 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10596 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10597 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010598 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010599 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010600 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010601 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010602 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010603 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010604 case X86ISD::COMI: return "X86ISD::COMI";
10605 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010606 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010607 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010608 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10609 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010610 case X86ISD::CMOV: return "X86ISD::CMOV";
10611 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010612 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010613 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10614 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010615 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010616 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010617 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010618 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010619 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010620 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10621 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010622 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010623 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010624 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +000010625 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
10626 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
10627 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Evan Cheng8ca29322006-11-10 21:43:37 +000010628 case X86ISD::FMAX: return "X86ISD::FMAX";
10629 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000010630 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
10631 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010632 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000010633 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010634 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000010635 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010636 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000010637 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
10638 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010639 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
10640 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
10641 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
10642 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
10643 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
10644 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000010645 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
10646 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000010647 case X86ISD::VSHL: return "X86ISD::VSHL";
10648 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000010649 case X86ISD::CMPPD: return "X86ISD::CMPPD";
10650 case X86ISD::CMPPS: return "X86ISD::CMPPS";
10651 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
10652 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
10653 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
10654 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
10655 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
10656 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
10657 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
10658 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010659 case X86ISD::ADD: return "X86ISD::ADD";
10660 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000010661 case X86ISD::ADC: return "X86ISD::ADC";
10662 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000010663 case X86ISD::SMUL: return "X86ISD::SMUL";
10664 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000010665 case X86ISD::INC: return "X86ISD::INC";
10666 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000010667 case X86ISD::OR: return "X86ISD::OR";
10668 case X86ISD::XOR: return "X86ISD::XOR";
10669 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +000010670 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000010671 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010672 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010673 case X86ISD::PALIGN: return "X86ISD::PALIGN";
10674 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
10675 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
10676 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
10677 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
10678 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
10679 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
10680 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
10681 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010682 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000010683 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010684 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000010685 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
10686 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010687 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
10688 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
10689 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
10690 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
10691 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
10692 case X86ISD::MOVSD: return "X86ISD::MOVSD";
10693 case X86ISD::MOVSS: return "X86ISD::MOVSS";
10694 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
10695 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +000010696 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010697 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
10698 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
10699 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
10700 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
10701 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
10702 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
10703 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
10704 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
10705 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
10706 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000010707 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000010708 case X86ISD::VPERMILPS: return "X86ISD::VPERMILPS";
10709 case X86ISD::VPERMILPSY: return "X86ISD::VPERMILPSY";
10710 case X86ISD::VPERMILPD: return "X86ISD::VPERMILPD";
10711 case X86ISD::VPERMILPDY: return "X86ISD::VPERMILPDY";
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000010712 case X86ISD::VPERM2F128: return "X86ISD::VPERM2F128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000010713 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000010714 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010715 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000010716 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000010717 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000010718 }
10719}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010720
Chris Lattnerc9addb72007-03-30 23:15:24 +000010721// isLegalAddressingMode - Return true if the addressing mode represented
10722// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000010723bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010724 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000010725 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010726 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000010727 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000010728
Chris Lattnerc9addb72007-03-30 23:15:24 +000010729 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010730 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010731 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000010732
Chris Lattnerc9addb72007-03-30 23:15:24 +000010733 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000010734 unsigned GVFlags =
10735 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010736
Chris Lattnerdfed4132009-07-10 07:38:24 +000010737 // If a reference to this global requires an extra load, we can't fold it.
10738 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010739 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010740
Chris Lattnerdfed4132009-07-10 07:38:24 +000010741 // If BaseGV requires a register for the PIC base, we cannot also have a
10742 // BaseReg specified.
10743 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000010744 return false;
Evan Cheng52787842007-08-01 23:46:47 +000010745
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010746 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000010747 if ((M != CodeModel::Small || R != Reloc::Static) &&
10748 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010749 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000010750 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010751
Chris Lattnerc9addb72007-03-30 23:15:24 +000010752 switch (AM.Scale) {
10753 case 0:
10754 case 1:
10755 case 2:
10756 case 4:
10757 case 8:
10758 // These scales always work.
10759 break;
10760 case 3:
10761 case 5:
10762 case 9:
10763 // These scales are formed with basereg+scalereg. Only accept if there is
10764 // no basereg yet.
10765 if (AM.HasBaseReg)
10766 return false;
10767 break;
10768 default: // Other stuff never works.
10769 return false;
10770 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010771
Chris Lattnerc9addb72007-03-30 23:15:24 +000010772 return true;
10773}
10774
10775
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010776bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010777 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000010778 return false;
Evan Chenge127a732007-10-29 07:57:50 +000010779 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
10780 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010781 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000010782 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010783 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000010784}
10785
Owen Andersone50ed302009-08-10 22:56:29 +000010786bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010787 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010788 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010789 unsigned NumBits1 = VT1.getSizeInBits();
10790 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010791 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010792 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010793 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010794}
Evan Cheng2bd122c2007-10-26 01:56:11 +000010795
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010796bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010797 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010798 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010799}
10800
Owen Andersone50ed302009-08-10 22:56:29 +000010801bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010802 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000010803 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010804}
10805
Owen Andersone50ed302009-08-10 22:56:29 +000010806bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000010807 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000010808 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000010809}
10810
Evan Cheng60c07e12006-07-05 22:17:51 +000010811/// isShuffleMaskLegal - Targets can use this to indicate that they only
10812/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
10813/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
10814/// are assumed to be legal.
10815bool
Eric Christopherfd179292009-08-27 18:07:15 +000010816X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000010817 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000010818 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000010819 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +000010820 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +000010821
Nate Begemana09008b2009-10-19 02:17:23 +000010822 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000010823 return (VT.getVectorNumElements() == 2 ||
10824 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
10825 isMOVLMask(M, VT) ||
10826 isSHUFPMask(M, VT) ||
10827 isPSHUFDMask(M, VT) ||
10828 isPSHUFHWMask(M, VT) ||
10829 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +000010830 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000010831 isUNPCKLMask(M, VT) ||
10832 isUNPCKHMask(M, VT) ||
10833 isUNPCKL_v_undef_Mask(M, VT) ||
10834 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010835}
10836
Dan Gohman7d8143f2008-04-09 20:09:42 +000010837bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000010838X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000010839 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000010840 unsigned NumElts = VT.getVectorNumElements();
10841 // FIXME: This collection of masks seems suspect.
10842 if (NumElts == 2)
10843 return true;
10844 if (NumElts == 4 && VT.getSizeInBits() == 128) {
10845 return (isMOVLMask(Mask, VT) ||
10846 isCommutedMOVLMask(Mask, VT, true) ||
10847 isSHUFPMask(Mask, VT) ||
10848 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010849 }
10850 return false;
10851}
10852
10853//===----------------------------------------------------------------------===//
10854// X86 Scheduler Hooks
10855//===----------------------------------------------------------------------===//
10856
Mon P Wang63307c32008-05-05 19:05:59 +000010857// private utility function
10858MachineBasicBlock *
10859X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
10860 MachineBasicBlock *MBB,
10861 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010862 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010863 unsigned LoadOpc,
10864 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010865 unsigned notOpc,
10866 unsigned EAXreg,
10867 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010868 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000010869 // For the atomic bitwise operator, we generate
10870 // thisMBB:
10871 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000010872 // ld t1 = [bitinstr.addr]
10873 // op t2 = t1, [bitinstr.val]
10874 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000010875 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
10876 // bz newMBB
10877 // fallthrough -->nextMBB
10878 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10879 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010880 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000010881 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010882
Mon P Wang63307c32008-05-05 19:05:59 +000010883 /// First build the CFG
10884 MachineFunction *F = MBB->getParent();
10885 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010886 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10887 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10888 F->insert(MBBIter, newMBB);
10889 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010890
Dan Gohman14152b42010-07-06 20:24:04 +000010891 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10892 nextMBB->splice(nextMBB->begin(), thisMBB,
10893 llvm::next(MachineBasicBlock::iterator(bInstr)),
10894 thisMBB->end());
10895 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010896
Mon P Wang63307c32008-05-05 19:05:59 +000010897 // Update thisMBB to fall through to newMBB
10898 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010899
Mon P Wang63307c32008-05-05 19:05:59 +000010900 // newMBB jumps to itself and fall through to nextMBB
10901 newMBB->addSuccessor(nextMBB);
10902 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010903
Mon P Wang63307c32008-05-05 19:05:59 +000010904 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010905 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010906 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000010907 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010908 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010909 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010910 int numArgs = bInstr->getNumOperands() - 1;
10911 for (int i=0; i < numArgs; ++i)
10912 argOpers[i] = &bInstr->getOperand(i+1);
10913
10914 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010915 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010916 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010917
Dale Johannesen140be2d2008-08-19 18:47:28 +000010918 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010919 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010920 for (int i=0; i <= lastAddrIndx; ++i)
10921 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010922
Dale Johannesen140be2d2008-08-19 18:47:28 +000010923 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010924 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010925 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010926 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010927 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010928 tt = t1;
10929
Dale Johannesen140be2d2008-08-19 18:47:28 +000010930 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000010931 assert((argOpers[valArgIndx]->isReg() ||
10932 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010933 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000010934 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000010935 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010936 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010937 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010938 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000010939 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010940
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010941 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000010942 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010943
Dale Johannesene4d209d2009-02-03 20:21:25 +000010944 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000010945 for (int i=0; i <= lastAddrIndx; ++i)
10946 (*MIB).addOperand(*argOpers[i]);
10947 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000010948 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010949 (*MIB).setMemRefs(bInstr->memoperands_begin(),
10950 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000010951
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010952 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000010953 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010954
Mon P Wang63307c32008-05-05 19:05:59 +000010955 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010956 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010957
Dan Gohman14152b42010-07-06 20:24:04 +000010958 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010959 return nextMBB;
10960}
10961
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000010962// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000010963MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010964X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
10965 MachineBasicBlock *MBB,
10966 unsigned regOpcL,
10967 unsigned regOpcH,
10968 unsigned immOpcL,
10969 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010970 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010971 // For the atomic bitwise operator, we generate
10972 // thisMBB (instructions are in pairs, except cmpxchg8b)
10973 // ld t1,t2 = [bitinstr.addr]
10974 // newMBB:
10975 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
10976 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000010977 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010978 // mov ECX, EBX <- t5, t6
10979 // mov EAX, EDX <- t1, t2
10980 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
10981 // mov t3, t4 <- EAX, EDX
10982 // bz newMBB
10983 // result in out1, out2
10984 // fallthrough -->nextMBB
10985
10986 const TargetRegisterClass *RC = X86::GR32RegisterClass;
10987 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010988 const unsigned NotOpc = X86::NOT32r;
10989 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10990 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10991 MachineFunction::iterator MBBIter = MBB;
10992 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010993
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010994 /// First build the CFG
10995 MachineFunction *F = MBB->getParent();
10996 MachineBasicBlock *thisMBB = MBB;
10997 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10998 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10999 F->insert(MBBIter, newMBB);
11000 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011001
Dan Gohman14152b42010-07-06 20:24:04 +000011002 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11003 nextMBB->splice(nextMBB->begin(), thisMBB,
11004 llvm::next(MachineBasicBlock::iterator(bInstr)),
11005 thisMBB->end());
11006 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011007
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011008 // Update thisMBB to fall through to newMBB
11009 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011010
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011011 // newMBB jumps to itself and fall through to nextMBB
11012 newMBB->addSuccessor(nextMBB);
11013 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011014
Dale Johannesene4d209d2009-02-03 20:21:25 +000011015 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011016 // Insert instructions into newMBB based on incoming instruction
11017 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011018 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011019 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011020 MachineOperand& dest1Oper = bInstr->getOperand(0);
11021 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011022 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11023 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011024 argOpers[i] = &bInstr->getOperand(i+2);
11025
Dan Gohman71ea4e52010-05-14 21:01:44 +000011026 // We use some of the operands multiple times, so conservatively just
11027 // clear any kill flags that might be present.
11028 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11029 argOpers[i]->setIsKill(false);
11030 }
11031
Evan Chengad5b52f2010-01-08 19:14:57 +000011032 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011033 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011034
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011035 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011036 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011037 for (int i=0; i <= lastAddrIndx; ++i)
11038 (*MIB).addOperand(*argOpers[i]);
11039 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011040 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011041 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011042 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011043 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011044 MachineOperand newOp3 = *(argOpers[3]);
11045 if (newOp3.isImm())
11046 newOp3.setImm(newOp3.getImm()+4);
11047 else
11048 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011049 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011050 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011051
11052 // t3/4 are defined later, at the bottom of the loop
11053 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11054 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011055 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011056 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011057 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011058 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11059
Evan Cheng306b4ca2010-01-08 23:41:50 +000011060 // The subsequent operations should be using the destination registers of
11061 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011062 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011063 t1 = F->getRegInfo().createVirtualRegister(RC);
11064 t2 = F->getRegInfo().createVirtualRegister(RC);
11065 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11066 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011067 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011068 t1 = dest1Oper.getReg();
11069 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011070 }
11071
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011072 int valArgIndx = lastAddrIndx + 1;
11073 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011074 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011075 "invalid operand");
11076 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11077 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011078 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011079 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011080 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011081 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011082 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011083 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011084 (*MIB).addOperand(*argOpers[valArgIndx]);
11085 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011086 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011087 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011088 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011089 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011090 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011091 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011092 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011093 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011094 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011095 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011096
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011097 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011098 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011099 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011100 MIB.addReg(t2);
11101
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011102 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011103 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011104 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011105 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011106
Dale Johannesene4d209d2009-02-03 20:21:25 +000011107 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011108 for (int i=0; i <= lastAddrIndx; ++i)
11109 (*MIB).addOperand(*argOpers[i]);
11110
11111 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011112 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11113 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011114
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011115 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011116 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011117 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011118 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011119
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011120 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011121 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011122
Dan Gohman14152b42010-07-06 20:24:04 +000011123 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011124 return nextMBB;
11125}
11126
11127// private utility function
11128MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011129X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11130 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011131 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011132 // For the atomic min/max operator, we generate
11133 // thisMBB:
11134 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011135 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011136 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011137 // cmp t1, t2
11138 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011139 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011140 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11141 // bz newMBB
11142 // fallthrough -->nextMBB
11143 //
11144 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11145 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011146 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011147 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011148
Mon P Wang63307c32008-05-05 19:05:59 +000011149 /// First build the CFG
11150 MachineFunction *F = MBB->getParent();
11151 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011152 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11153 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11154 F->insert(MBBIter, newMBB);
11155 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011156
Dan Gohman14152b42010-07-06 20:24:04 +000011157 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11158 nextMBB->splice(nextMBB->begin(), thisMBB,
11159 llvm::next(MachineBasicBlock::iterator(mInstr)),
11160 thisMBB->end());
11161 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011162
Mon P Wang63307c32008-05-05 19:05:59 +000011163 // Update thisMBB to fall through to newMBB
11164 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011165
Mon P Wang63307c32008-05-05 19:05:59 +000011166 // newMBB jumps to newMBB and fall through to nextMBB
11167 newMBB->addSuccessor(nextMBB);
11168 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011169
Dale Johannesene4d209d2009-02-03 20:21:25 +000011170 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011171 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011172 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011173 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011174 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011175 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011176 int numArgs = mInstr->getNumOperands() - 1;
11177 for (int i=0; i < numArgs; ++i)
11178 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011179
Mon P Wang63307c32008-05-05 19:05:59 +000011180 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011181 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011182 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011183
Mon P Wangab3e7472008-05-05 22:56:23 +000011184 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011185 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011186 for (int i=0; i <= lastAddrIndx; ++i)
11187 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011188
Mon P Wang63307c32008-05-05 19:05:59 +000011189 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011190 assert((argOpers[valArgIndx]->isReg() ||
11191 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011192 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011193
11194 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011195 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011196 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011197 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011198 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011199 (*MIB).addOperand(*argOpers[valArgIndx]);
11200
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011201 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011202 MIB.addReg(t1);
11203
Dale Johannesene4d209d2009-02-03 20:21:25 +000011204 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011205 MIB.addReg(t1);
11206 MIB.addReg(t2);
11207
11208 // Generate movc
11209 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011210 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011211 MIB.addReg(t2);
11212 MIB.addReg(t1);
11213
11214 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011215 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011216 for (int i=0; i <= lastAddrIndx; ++i)
11217 (*MIB).addOperand(*argOpers[i]);
11218 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011219 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011220 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11221 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011222
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011223 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011224 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011225
Mon P Wang63307c32008-05-05 19:05:59 +000011226 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011227 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011228
Dan Gohman14152b42010-07-06 20:24:04 +000011229 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011230 return nextMBB;
11231}
11232
Eric Christopherf83a5de2009-08-27 18:08:16 +000011233// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011234// or XMM0_V32I8 in AVX all of this code can be replaced with that
11235// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011236MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011237X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011238 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011239 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
11240 "Target must have SSE4.2 or AVX features enabled");
11241
Eric Christopherb120ab42009-08-18 22:50:32 +000011242 DebugLoc dl = MI->getDebugLoc();
11243 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011244 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011245 if (!Subtarget->hasAVX()) {
11246 if (memArg)
11247 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11248 else
11249 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11250 } else {
11251 if (memArg)
11252 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11253 else
11254 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11255 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011256
Eric Christopher41c902f2010-11-30 08:20:21 +000011257 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011258 for (unsigned i = 0; i < numArgs; ++i) {
11259 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011260 if (!(Op.isReg() && Op.isImplicit()))
11261 MIB.addOperand(Op);
11262 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011263 BuildMI(*BB, MI, dl,
11264 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11265 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011266 .addReg(X86::XMM0);
11267
Dan Gohman14152b42010-07-06 20:24:04 +000011268 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011269 return BB;
11270}
11271
11272MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011273X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011274 DebugLoc dl = MI->getDebugLoc();
11275 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011276
Eric Christopher228232b2010-11-30 07:20:12 +000011277 // Address into RAX/EAX, other two args into ECX, EDX.
11278 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11279 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11280 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11281 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011282 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011283
Eric Christopher228232b2010-11-30 07:20:12 +000011284 unsigned ValOps = X86::AddrNumOperands;
11285 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11286 .addReg(MI->getOperand(ValOps).getReg());
11287 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11288 .addReg(MI->getOperand(ValOps+1).getReg());
11289
11290 // The instruction doesn't actually take any operands though.
11291 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011292
Eric Christopher228232b2010-11-30 07:20:12 +000011293 MI->eraseFromParent(); // The pseudo is gone now.
11294 return BB;
11295}
11296
11297MachineBasicBlock *
11298X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011299 DebugLoc dl = MI->getDebugLoc();
11300 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011301
Eric Christopher228232b2010-11-30 07:20:12 +000011302 // First arg in ECX, the second in EAX.
11303 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11304 .addReg(MI->getOperand(0).getReg());
11305 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11306 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011307
Eric Christopher228232b2010-11-30 07:20:12 +000011308 // The instruction doesn't actually take any operands though.
11309 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011310
Eric Christopher228232b2010-11-30 07:20:12 +000011311 MI->eraseFromParent(); // The pseudo is gone now.
11312 return BB;
11313}
11314
11315MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011316X86TargetLowering::EmitVAARG64WithCustomInserter(
11317 MachineInstr *MI,
11318 MachineBasicBlock *MBB) const {
11319 // Emit va_arg instruction on X86-64.
11320
11321 // Operands to this pseudo-instruction:
11322 // 0 ) Output : destination address (reg)
11323 // 1-5) Input : va_list address (addr, i64mem)
11324 // 6 ) ArgSize : Size (in bytes) of vararg type
11325 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11326 // 8 ) Align : Alignment of type
11327 // 9 ) EFLAGS (implicit-def)
11328
11329 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11330 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11331
11332 unsigned DestReg = MI->getOperand(0).getReg();
11333 MachineOperand &Base = MI->getOperand(1);
11334 MachineOperand &Scale = MI->getOperand(2);
11335 MachineOperand &Index = MI->getOperand(3);
11336 MachineOperand &Disp = MI->getOperand(4);
11337 MachineOperand &Segment = MI->getOperand(5);
11338 unsigned ArgSize = MI->getOperand(6).getImm();
11339 unsigned ArgMode = MI->getOperand(7).getImm();
11340 unsigned Align = MI->getOperand(8).getImm();
11341
11342 // Memory Reference
11343 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11344 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11345 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11346
11347 // Machine Information
11348 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11349 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11350 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11351 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11352 DebugLoc DL = MI->getDebugLoc();
11353
11354 // struct va_list {
11355 // i32 gp_offset
11356 // i32 fp_offset
11357 // i64 overflow_area (address)
11358 // i64 reg_save_area (address)
11359 // }
11360 // sizeof(va_list) = 24
11361 // alignment(va_list) = 8
11362
11363 unsigned TotalNumIntRegs = 6;
11364 unsigned TotalNumXMMRegs = 8;
11365 bool UseGPOffset = (ArgMode == 1);
11366 bool UseFPOffset = (ArgMode == 2);
11367 unsigned MaxOffset = TotalNumIntRegs * 8 +
11368 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11369
11370 /* Align ArgSize to a multiple of 8 */
11371 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11372 bool NeedsAlign = (Align > 8);
11373
11374 MachineBasicBlock *thisMBB = MBB;
11375 MachineBasicBlock *overflowMBB;
11376 MachineBasicBlock *offsetMBB;
11377 MachineBasicBlock *endMBB;
11378
11379 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11380 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11381 unsigned OffsetReg = 0;
11382
11383 if (!UseGPOffset && !UseFPOffset) {
11384 // If we only pull from the overflow region, we don't create a branch.
11385 // We don't need to alter control flow.
11386 OffsetDestReg = 0; // unused
11387 OverflowDestReg = DestReg;
11388
11389 offsetMBB = NULL;
11390 overflowMBB = thisMBB;
11391 endMBB = thisMBB;
11392 } else {
11393 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11394 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11395 // If not, pull from overflow_area. (branch to overflowMBB)
11396 //
11397 // thisMBB
11398 // | .
11399 // | .
11400 // offsetMBB overflowMBB
11401 // | .
11402 // | .
11403 // endMBB
11404
11405 // Registers for the PHI in endMBB
11406 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11407 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11408
11409 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11410 MachineFunction *MF = MBB->getParent();
11411 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11412 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11413 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11414
11415 MachineFunction::iterator MBBIter = MBB;
11416 ++MBBIter;
11417
11418 // Insert the new basic blocks
11419 MF->insert(MBBIter, offsetMBB);
11420 MF->insert(MBBIter, overflowMBB);
11421 MF->insert(MBBIter, endMBB);
11422
11423 // Transfer the remainder of MBB and its successor edges to endMBB.
11424 endMBB->splice(endMBB->begin(), thisMBB,
11425 llvm::next(MachineBasicBlock::iterator(MI)),
11426 thisMBB->end());
11427 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11428
11429 // Make offsetMBB and overflowMBB successors of thisMBB
11430 thisMBB->addSuccessor(offsetMBB);
11431 thisMBB->addSuccessor(overflowMBB);
11432
11433 // endMBB is a successor of both offsetMBB and overflowMBB
11434 offsetMBB->addSuccessor(endMBB);
11435 overflowMBB->addSuccessor(endMBB);
11436
11437 // Load the offset value into a register
11438 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11439 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11440 .addOperand(Base)
11441 .addOperand(Scale)
11442 .addOperand(Index)
11443 .addDisp(Disp, UseFPOffset ? 4 : 0)
11444 .addOperand(Segment)
11445 .setMemRefs(MMOBegin, MMOEnd);
11446
11447 // Check if there is enough room left to pull this argument.
11448 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11449 .addReg(OffsetReg)
11450 .addImm(MaxOffset + 8 - ArgSizeA8);
11451
11452 // Branch to "overflowMBB" if offset >= max
11453 // Fall through to "offsetMBB" otherwise
11454 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11455 .addMBB(overflowMBB);
11456 }
11457
11458 // In offsetMBB, emit code to use the reg_save_area.
11459 if (offsetMBB) {
11460 assert(OffsetReg != 0);
11461
11462 // Read the reg_save_area address.
11463 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11464 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11465 .addOperand(Base)
11466 .addOperand(Scale)
11467 .addOperand(Index)
11468 .addDisp(Disp, 16)
11469 .addOperand(Segment)
11470 .setMemRefs(MMOBegin, MMOEnd);
11471
11472 // Zero-extend the offset
11473 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11474 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11475 .addImm(0)
11476 .addReg(OffsetReg)
11477 .addImm(X86::sub_32bit);
11478
11479 // Add the offset to the reg_save_area to get the final address.
11480 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11481 .addReg(OffsetReg64)
11482 .addReg(RegSaveReg);
11483
11484 // Compute the offset for the next argument
11485 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11486 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11487 .addReg(OffsetReg)
11488 .addImm(UseFPOffset ? 16 : 8);
11489
11490 // Store it back into the va_list.
11491 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11492 .addOperand(Base)
11493 .addOperand(Scale)
11494 .addOperand(Index)
11495 .addDisp(Disp, UseFPOffset ? 4 : 0)
11496 .addOperand(Segment)
11497 .addReg(NextOffsetReg)
11498 .setMemRefs(MMOBegin, MMOEnd);
11499
11500 // Jump to endMBB
11501 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11502 .addMBB(endMBB);
11503 }
11504
11505 //
11506 // Emit code to use overflow area
11507 //
11508
11509 // Load the overflow_area address into a register.
11510 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11511 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11512 .addOperand(Base)
11513 .addOperand(Scale)
11514 .addOperand(Index)
11515 .addDisp(Disp, 8)
11516 .addOperand(Segment)
11517 .setMemRefs(MMOBegin, MMOEnd);
11518
11519 // If we need to align it, do so. Otherwise, just copy the address
11520 // to OverflowDestReg.
11521 if (NeedsAlign) {
11522 // Align the overflow address
11523 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11524 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11525
11526 // aligned_addr = (addr + (align-1)) & ~(align-1)
11527 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11528 .addReg(OverflowAddrReg)
11529 .addImm(Align-1);
11530
11531 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11532 .addReg(TmpReg)
11533 .addImm(~(uint64_t)(Align-1));
11534 } else {
11535 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11536 .addReg(OverflowAddrReg);
11537 }
11538
11539 // Compute the next overflow address after this argument.
11540 // (the overflow address should be kept 8-byte aligned)
11541 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11542 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11543 .addReg(OverflowDestReg)
11544 .addImm(ArgSizeA8);
11545
11546 // Store the new overflow address.
11547 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11548 .addOperand(Base)
11549 .addOperand(Scale)
11550 .addOperand(Index)
11551 .addDisp(Disp, 8)
11552 .addOperand(Segment)
11553 .addReg(NextAddrReg)
11554 .setMemRefs(MMOBegin, MMOEnd);
11555
11556 // If we branched, emit the PHI to the front of endMBB.
11557 if (offsetMBB) {
11558 BuildMI(*endMBB, endMBB->begin(), DL,
11559 TII->get(X86::PHI), DestReg)
11560 .addReg(OffsetDestReg).addMBB(offsetMBB)
11561 .addReg(OverflowDestReg).addMBB(overflowMBB);
11562 }
11563
11564 // Erase the pseudo instruction
11565 MI->eraseFromParent();
11566
11567 return endMBB;
11568}
11569
11570MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011571X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11572 MachineInstr *MI,
11573 MachineBasicBlock *MBB) const {
11574 // Emit code to save XMM registers to the stack. The ABI says that the
11575 // number of registers to save is given in %al, so it's theoretically
11576 // possible to do an indirect jump trick to avoid saving all of them,
11577 // however this code takes a simpler approach and just executes all
11578 // of the stores if %al is non-zero. It's less code, and it's probably
11579 // easier on the hardware branch predictor, and stores aren't all that
11580 // expensive anyway.
11581
11582 // Create the new basic blocks. One block contains all the XMM stores,
11583 // and one block is the final destination regardless of whether any
11584 // stores were performed.
11585 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11586 MachineFunction *F = MBB->getParent();
11587 MachineFunction::iterator MBBIter = MBB;
11588 ++MBBIter;
11589 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11590 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11591 F->insert(MBBIter, XMMSaveMBB);
11592 F->insert(MBBIter, EndMBB);
11593
Dan Gohman14152b42010-07-06 20:24:04 +000011594 // Transfer the remainder of MBB and its successor edges to EndMBB.
11595 EndMBB->splice(EndMBB->begin(), MBB,
11596 llvm::next(MachineBasicBlock::iterator(MI)),
11597 MBB->end());
11598 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11599
Dan Gohmand6708ea2009-08-15 01:38:56 +000011600 // The original block will now fall through to the XMM save block.
11601 MBB->addSuccessor(XMMSaveMBB);
11602 // The XMMSaveMBB will fall through to the end block.
11603 XMMSaveMBB->addSuccessor(EndMBB);
11604
11605 // Now add the instructions.
11606 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11607 DebugLoc DL = MI->getDebugLoc();
11608
11609 unsigned CountReg = MI->getOperand(0).getReg();
11610 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11611 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11612
11613 if (!Subtarget->isTargetWin64()) {
11614 // If %al is 0, branch around the XMM save block.
11615 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011616 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011617 MBB->addSuccessor(EndMBB);
11618 }
11619
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011620 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000011621 // In the XMM save block, save all the XMM argument registers.
11622 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11623 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011624 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011625 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011626 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011627 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011628 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011629 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000011630 .addFrameIndex(RegSaveFrameIndex)
11631 .addImm(/*Scale=*/1)
11632 .addReg(/*IndexReg=*/0)
11633 .addImm(/*Disp=*/Offset)
11634 .addReg(/*Segment=*/0)
11635 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011636 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011637 }
11638
Dan Gohman14152b42010-07-06 20:24:04 +000011639 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011640
11641 return EndMBB;
11642}
Mon P Wang63307c32008-05-05 19:05:59 +000011643
Evan Cheng60c07e12006-07-05 22:17:51 +000011644MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000011645X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011646 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000011647 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11648 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000011649
Chris Lattner52600972009-09-02 05:57:00 +000011650 // To "insert" a SELECT_CC instruction, we actually have to insert the
11651 // diamond control-flow pattern. The incoming instruction knows the
11652 // destination vreg to set, the condition code register to branch on, the
11653 // true/false values to select between, and a branch opcode to use.
11654 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11655 MachineFunction::iterator It = BB;
11656 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000011657
Chris Lattner52600972009-09-02 05:57:00 +000011658 // thisMBB:
11659 // ...
11660 // TrueVal = ...
11661 // cmpTY ccX, r1, r2
11662 // bCC copy1MBB
11663 // fallthrough --> copy0MBB
11664 MachineBasicBlock *thisMBB = BB;
11665 MachineFunction *F = BB->getParent();
11666 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11667 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000011668 F->insert(It, copy0MBB);
11669 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000011670
Bill Wendling730c07e2010-06-25 20:48:10 +000011671 // If the EFLAGS register isn't dead in the terminator, then claim that it's
11672 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000011673 if (!MI->killsRegister(X86::EFLAGS)) {
11674 copy0MBB->addLiveIn(X86::EFLAGS);
11675 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000011676 }
11677
Dan Gohman14152b42010-07-06 20:24:04 +000011678 // Transfer the remainder of BB and its successor edges to sinkMBB.
11679 sinkMBB->splice(sinkMBB->begin(), BB,
11680 llvm::next(MachineBasicBlock::iterator(MI)),
11681 BB->end());
11682 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11683
11684 // Add the true and fallthrough blocks as its successors.
11685 BB->addSuccessor(copy0MBB);
11686 BB->addSuccessor(sinkMBB);
11687
11688 // Create the conditional branch instruction.
11689 unsigned Opc =
11690 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
11691 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
11692
Chris Lattner52600972009-09-02 05:57:00 +000011693 // copy0MBB:
11694 // %FalseValue = ...
11695 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000011696 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000011697
Chris Lattner52600972009-09-02 05:57:00 +000011698 // sinkMBB:
11699 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
11700 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000011701 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
11702 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000011703 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
11704 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
11705
Dan Gohman14152b42010-07-06 20:24:04 +000011706 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000011707 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000011708}
11709
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011710MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011711X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
11712 bool Is64Bit) const {
11713 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11714 DebugLoc DL = MI->getDebugLoc();
11715 MachineFunction *MF = BB->getParent();
11716 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11717
11718 assert(EnableSegmentedStacks);
11719
11720 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
11721 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
11722
11723 // BB:
11724 // ... [Till the alloca]
11725 // If stacklet is not large enough, jump to mallocMBB
11726 //
11727 // bumpMBB:
11728 // Allocate by subtracting from RSP
11729 // Jump to continueMBB
11730 //
11731 // mallocMBB:
11732 // Allocate by call to runtime
11733 //
11734 // continueMBB:
11735 // ...
11736 // [rest of original BB]
11737 //
11738
11739 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11740 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11741 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11742
11743 MachineRegisterInfo &MRI = MF->getRegInfo();
11744 const TargetRegisterClass *AddrRegClass =
11745 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
11746
11747 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11748 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11749 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
11750 sizeVReg = MI->getOperand(1).getReg(),
11751 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
11752
11753 MachineFunction::iterator MBBIter = BB;
11754 ++MBBIter;
11755
11756 MF->insert(MBBIter, bumpMBB);
11757 MF->insert(MBBIter, mallocMBB);
11758 MF->insert(MBBIter, continueMBB);
11759
11760 continueMBB->splice(continueMBB->begin(), BB, llvm::next
11761 (MachineBasicBlock::iterator(MI)), BB->end());
11762 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
11763
11764 // Add code to the main basic block to check if the stack limit has been hit,
11765 // and if so, jump to mallocMBB otherwise to bumpMBB.
11766 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
11767 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), tmpSPVReg)
11768 .addReg(tmpSPVReg).addReg(sizeVReg);
11769 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
11770 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
11771 .addReg(tmpSPVReg);
11772 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
11773
11774 // bumpMBB simply decreases the stack pointer, since we know the current
11775 // stacklet has enough space.
11776 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
11777 .addReg(tmpSPVReg);
11778 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
11779 .addReg(tmpSPVReg);
11780 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11781
11782 // Calls into a routine in libgcc to allocate more space from the heap.
11783 if (Is64Bit) {
11784 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
11785 .addReg(sizeVReg);
11786 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
11787 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
11788 } else {
11789 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
11790 .addImm(12);
11791 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
11792 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
11793 .addExternalSymbol("__morestack_allocate_stack_space");
11794 }
11795
11796 if (!Is64Bit)
11797 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
11798 .addImm(16);
11799
11800 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
11801 .addReg(Is64Bit ? X86::RAX : X86::EAX);
11802 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11803
11804 // Set up the CFG correctly.
11805 BB->addSuccessor(bumpMBB);
11806 BB->addSuccessor(mallocMBB);
11807 mallocMBB->addSuccessor(continueMBB);
11808 bumpMBB->addSuccessor(continueMBB);
11809
11810 // Take care of the PHI nodes.
11811 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
11812 MI->getOperand(0).getReg())
11813 .addReg(mallocPtrVReg).addMBB(mallocMBB)
11814 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
11815
11816 // Delete the original pseudo instruction.
11817 MI->eraseFromParent();
11818
11819 // And we're done.
11820 return continueMBB;
11821}
11822
11823MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011824X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011825 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011826 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11827 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011828
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011829 assert(!Subtarget->isTargetEnvMacho());
11830
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011831 // The lowering is pretty easy: we're just emitting the call to _alloca. The
11832 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011833
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011834 if (Subtarget->isTargetWin64()) {
11835 if (Subtarget->isTargetCygMing()) {
11836 // ___chkstk(Mingw64):
11837 // Clobbers R10, R11, RAX and EFLAGS.
11838 // Updates RSP.
11839 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11840 .addExternalSymbol("___chkstk")
11841 .addReg(X86::RAX, RegState::Implicit)
11842 .addReg(X86::RSP, RegState::Implicit)
11843 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
11844 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
11845 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11846 } else {
11847 // __chkstk(MSVCRT): does not update stack pointer.
11848 // Clobbers R10, R11 and EFLAGS.
11849 // FIXME: RAX(allocated size) might be reused and not killed.
11850 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11851 .addExternalSymbol("__chkstk")
11852 .addReg(X86::RAX, RegState::Implicit)
11853 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11854 // RAX has the offset to subtracted from RSP.
11855 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
11856 .addReg(X86::RSP)
11857 .addReg(X86::RAX);
11858 }
11859 } else {
11860 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011861 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
11862
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011863 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
11864 .addExternalSymbol(StackProbeSymbol)
11865 .addReg(X86::EAX, RegState::Implicit)
11866 .addReg(X86::ESP, RegState::Implicit)
11867 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
11868 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
11869 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11870 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011871
Dan Gohman14152b42010-07-06 20:24:04 +000011872 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011873 return BB;
11874}
Chris Lattner52600972009-09-02 05:57:00 +000011875
11876MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000011877X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
11878 MachineBasicBlock *BB) const {
11879 // This is pretty easy. We're taking the value that we received from
11880 // our load from the relocation, sticking it in either RDI (x86-64)
11881 // or EAX and doing an indirect call. The return value will then
11882 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000011883 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000011884 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000011885 DebugLoc DL = MI->getDebugLoc();
11886 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000011887
11888 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000011889 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011890
Eric Christopher30ef0e52010-06-03 04:07:48 +000011891 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000011892 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11893 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000011894 .addReg(X86::RIP)
11895 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011896 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011897 MI->getOperand(3).getTargetFlags())
11898 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000011899 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000011900 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000011901 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000011902 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11903 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000011904 .addReg(0)
11905 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011906 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000011907 MI->getOperand(3).getTargetFlags())
11908 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011909 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011910 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011911 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000011912 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11913 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000011914 .addReg(TII->getGlobalBaseReg(F))
11915 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011916 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011917 MI->getOperand(3).getTargetFlags())
11918 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011919 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011920 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011921 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011922
Dan Gohman14152b42010-07-06 20:24:04 +000011923 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000011924 return BB;
11925}
11926
11927MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000011928X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011929 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000011930 switch (MI->getOpcode()) {
11931 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000011932 case X86::TAILJMPd64:
11933 case X86::TAILJMPr64:
11934 case X86::TAILJMPm64:
11935 assert(!"TAILJMP64 would not be touched here.");
11936 case X86::TCRETURNdi64:
11937 case X86::TCRETURNri64:
11938 case X86::TCRETURNmi64:
11939 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
11940 // On AMD64, additional defs should be added before register allocation.
11941 if (!Subtarget->isTargetWin64()) {
11942 MI->addRegisterDefined(X86::RSI);
11943 MI->addRegisterDefined(X86::RDI);
11944 MI->addRegisterDefined(X86::XMM6);
11945 MI->addRegisterDefined(X86::XMM7);
11946 MI->addRegisterDefined(X86::XMM8);
11947 MI->addRegisterDefined(X86::XMM9);
11948 MI->addRegisterDefined(X86::XMM10);
11949 MI->addRegisterDefined(X86::XMM11);
11950 MI->addRegisterDefined(X86::XMM12);
11951 MI->addRegisterDefined(X86::XMM13);
11952 MI->addRegisterDefined(X86::XMM14);
11953 MI->addRegisterDefined(X86::XMM15);
11954 }
11955 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011956 case X86::WIN_ALLOCA:
11957 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011958 case X86::SEG_ALLOCA_32:
11959 return EmitLoweredSegAlloca(MI, BB, false);
11960 case X86::SEG_ALLOCA_64:
11961 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011962 case X86::TLSCall_32:
11963 case X86::TLSCall_64:
11964 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000011965 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000011966 case X86::CMOV_FR32:
11967 case X86::CMOV_FR64:
11968 case X86::CMOV_V4F32:
11969 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000011970 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000011971 case X86::CMOV_V8F32:
11972 case X86::CMOV_V4F64:
11973 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000011974 case X86::CMOV_GR16:
11975 case X86::CMOV_GR32:
11976 case X86::CMOV_RFP32:
11977 case X86::CMOV_RFP64:
11978 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011979 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000011980
Dale Johannesen849f2142007-07-03 00:53:03 +000011981 case X86::FP32_TO_INT16_IN_MEM:
11982 case X86::FP32_TO_INT32_IN_MEM:
11983 case X86::FP32_TO_INT64_IN_MEM:
11984 case X86::FP64_TO_INT16_IN_MEM:
11985 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000011986 case X86::FP64_TO_INT64_IN_MEM:
11987 case X86::FP80_TO_INT16_IN_MEM:
11988 case X86::FP80_TO_INT32_IN_MEM:
11989 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000011990 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11991 DebugLoc DL = MI->getDebugLoc();
11992
Evan Cheng60c07e12006-07-05 22:17:51 +000011993 // Change the floating point control register to use "round towards zero"
11994 // mode when truncating to an integer value.
11995 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000011996 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000011997 addFrameReference(BuildMI(*BB, MI, DL,
11998 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000011999
12000 // Load the old value of the high byte of the control word...
12001 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012002 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012003 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012004 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012005
12006 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012007 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012008 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012009
12010 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012011 addFrameReference(BuildMI(*BB, MI, DL,
12012 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012013
12014 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012015 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012016 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012017
12018 // Get the X86 opcode to use.
12019 unsigned Opc;
12020 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012021 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012022 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12023 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12024 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12025 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12026 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12027 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012028 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12029 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12030 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012031 }
12032
12033 X86AddressMode AM;
12034 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012035 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012036 AM.BaseType = X86AddressMode::RegBase;
12037 AM.Base.Reg = Op.getReg();
12038 } else {
12039 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012040 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012041 }
12042 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012043 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012044 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012045 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012046 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012047 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012048 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012049 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012050 AM.GV = Op.getGlobal();
12051 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012052 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012053 }
Dan Gohman14152b42010-07-06 20:24:04 +000012054 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012055 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012056
12057 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012058 addFrameReference(BuildMI(*BB, MI, DL,
12059 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012060
Dan Gohman14152b42010-07-06 20:24:04 +000012061 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012062 return BB;
12063 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012064 // String/text processing lowering.
12065 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012066 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012067 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12068 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012069 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012070 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12071 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012072 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012073 return EmitPCMP(MI, BB, 5, false /* in mem */);
12074 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012075 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012076 return EmitPCMP(MI, BB, 5, true /* in mem */);
12077
Eric Christopher228232b2010-11-30 07:20:12 +000012078 // Thread synchronization.
12079 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012080 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012081 case X86::MWAIT:
12082 return EmitMwait(MI, BB);
12083
Eric Christopherb120ab42009-08-18 22:50:32 +000012084 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012085 case X86::ATOMAND32:
12086 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012087 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012088 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012089 X86::NOT32r, X86::EAX,
12090 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012091 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012092 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12093 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012094 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012095 X86::NOT32r, X86::EAX,
12096 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012097 case X86::ATOMXOR32:
12098 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012099 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012100 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012101 X86::NOT32r, X86::EAX,
12102 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012103 case X86::ATOMNAND32:
12104 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012105 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012106 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012107 X86::NOT32r, X86::EAX,
12108 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012109 case X86::ATOMMIN32:
12110 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12111 case X86::ATOMMAX32:
12112 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12113 case X86::ATOMUMIN32:
12114 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12115 case X86::ATOMUMAX32:
12116 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012117
12118 case X86::ATOMAND16:
12119 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12120 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012121 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012122 X86::NOT16r, X86::AX,
12123 X86::GR16RegisterClass);
12124 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012125 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012126 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012127 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012128 X86::NOT16r, X86::AX,
12129 X86::GR16RegisterClass);
12130 case X86::ATOMXOR16:
12131 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12132 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012133 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012134 X86::NOT16r, X86::AX,
12135 X86::GR16RegisterClass);
12136 case X86::ATOMNAND16:
12137 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12138 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012139 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012140 X86::NOT16r, X86::AX,
12141 X86::GR16RegisterClass, true);
12142 case X86::ATOMMIN16:
12143 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12144 case X86::ATOMMAX16:
12145 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12146 case X86::ATOMUMIN16:
12147 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12148 case X86::ATOMUMAX16:
12149 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12150
12151 case X86::ATOMAND8:
12152 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12153 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012154 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012155 X86::NOT8r, X86::AL,
12156 X86::GR8RegisterClass);
12157 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012158 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012159 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012160 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012161 X86::NOT8r, X86::AL,
12162 X86::GR8RegisterClass);
12163 case X86::ATOMXOR8:
12164 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12165 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012166 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012167 X86::NOT8r, X86::AL,
12168 X86::GR8RegisterClass);
12169 case X86::ATOMNAND8:
12170 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12171 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012172 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012173 X86::NOT8r, X86::AL,
12174 X86::GR8RegisterClass, true);
12175 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012176 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012177 case X86::ATOMAND64:
12178 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012179 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012180 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012181 X86::NOT64r, X86::RAX,
12182 X86::GR64RegisterClass);
12183 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012184 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12185 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012186 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012187 X86::NOT64r, X86::RAX,
12188 X86::GR64RegisterClass);
12189 case X86::ATOMXOR64:
12190 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012191 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012192 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012193 X86::NOT64r, X86::RAX,
12194 X86::GR64RegisterClass);
12195 case X86::ATOMNAND64:
12196 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12197 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012198 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012199 X86::NOT64r, X86::RAX,
12200 X86::GR64RegisterClass, true);
12201 case X86::ATOMMIN64:
12202 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12203 case X86::ATOMMAX64:
12204 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12205 case X86::ATOMUMIN64:
12206 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12207 case X86::ATOMUMAX64:
12208 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012209
12210 // This group does 64-bit operations on a 32-bit host.
12211 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012212 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012213 X86::AND32rr, X86::AND32rr,
12214 X86::AND32ri, X86::AND32ri,
12215 false);
12216 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012217 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012218 X86::OR32rr, X86::OR32rr,
12219 X86::OR32ri, X86::OR32ri,
12220 false);
12221 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012222 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012223 X86::XOR32rr, X86::XOR32rr,
12224 X86::XOR32ri, X86::XOR32ri,
12225 false);
12226 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012227 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012228 X86::AND32rr, X86::AND32rr,
12229 X86::AND32ri, X86::AND32ri,
12230 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012231 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012232 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012233 X86::ADD32rr, X86::ADC32rr,
12234 X86::ADD32ri, X86::ADC32ri,
12235 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012236 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012237 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012238 X86::SUB32rr, X86::SBB32rr,
12239 X86::SUB32ri, X86::SBB32ri,
12240 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012241 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012242 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012243 X86::MOV32rr, X86::MOV32rr,
12244 X86::MOV32ri, X86::MOV32ri,
12245 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012246 case X86::VASTART_SAVE_XMM_REGS:
12247 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012248
12249 case X86::VAARG_64:
12250 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012251 }
12252}
12253
12254//===----------------------------------------------------------------------===//
12255// X86 Optimization Hooks
12256//===----------------------------------------------------------------------===//
12257
Dan Gohman475871a2008-07-27 21:46:04 +000012258void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012259 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012260 APInt &KnownZero,
12261 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012262 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012263 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012264 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012265 assert((Opc >= ISD::BUILTIN_OP_END ||
12266 Opc == ISD::INTRINSIC_WO_CHAIN ||
12267 Opc == ISD::INTRINSIC_W_CHAIN ||
12268 Opc == ISD::INTRINSIC_VOID) &&
12269 "Should use MaskedValueIsZero if you don't know whether Op"
12270 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012271
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012272 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012273 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012274 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012275 case X86ISD::ADD:
12276 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012277 case X86ISD::ADC:
12278 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012279 case X86ISD::SMUL:
12280 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012281 case X86ISD::INC:
12282 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012283 case X86ISD::OR:
12284 case X86ISD::XOR:
12285 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012286 // These nodes' second result is a boolean.
12287 if (Op.getResNo() == 0)
12288 break;
12289 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012290 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012291 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12292 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012293 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012294 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012295}
Chris Lattner259e97c2006-01-31 19:43:35 +000012296
Owen Andersonbc146b02010-09-21 20:42:50 +000012297unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12298 unsigned Depth) const {
12299 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12300 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12301 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012302
Owen Andersonbc146b02010-09-21 20:42:50 +000012303 // Fallback case.
12304 return 1;
12305}
12306
Evan Cheng206ee9d2006-07-07 08:33:52 +000012307/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012308/// node is a GlobalAddress + offset.
12309bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012310 const GlobalValue* &GA,
12311 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012312 if (N->getOpcode() == X86ISD::Wrapper) {
12313 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012314 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012315 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012316 return true;
12317 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012318 }
Evan Chengad4196b2008-05-12 19:56:52 +000012319 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012320}
12321
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012322/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12323/// same as extracting the high 128-bit part of 256-bit vector and then
12324/// inserting the result into the low part of a new 256-bit vector
12325static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12326 EVT VT = SVOp->getValueType(0);
12327 int NumElems = VT.getVectorNumElements();
12328
12329 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12330 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12331 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12332 SVOp->getMaskElt(j) >= 0)
12333 return false;
12334
12335 return true;
12336}
12337
12338/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12339/// same as extracting the low 128-bit part of 256-bit vector and then
12340/// inserting the result into the high part of a new 256-bit vector
12341static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12342 EVT VT = SVOp->getValueType(0);
12343 int NumElems = VT.getVectorNumElements();
12344
12345 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12346 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12347 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12348 SVOp->getMaskElt(j) >= 0)
12349 return false;
12350
12351 return true;
12352}
12353
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012354/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12355static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12356 TargetLowering::DAGCombinerInfo &DCI) {
12357 DebugLoc dl = N->getDebugLoc();
12358 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12359 SDValue V1 = SVOp->getOperand(0);
12360 SDValue V2 = SVOp->getOperand(1);
12361 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012362 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012363
12364 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12365 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12366 //
12367 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012368 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012369 // V UNDEF BUILD_VECTOR UNDEF
12370 // \ / \ /
12371 // CONCAT_VECTOR CONCAT_VECTOR
12372 // \ /
12373 // \ /
12374 // RESULT: V + zero extended
12375 //
12376 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12377 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12378 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12379 return SDValue();
12380
12381 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12382 return SDValue();
12383
12384 // To match the shuffle mask, the first half of the mask should
12385 // be exactly the first vector, and all the rest a splat with the
12386 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012387 for (int i = 0; i < NumElems/2; ++i)
12388 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12389 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12390 return SDValue();
12391
12392 // Emit a zeroed vector and insert the desired subvector on its
12393 // first half.
12394 SDValue Zeros = getZeroVector(VT, true /* HasSSE2 */, DAG, dl);
12395 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12396 DAG.getConstant(0, MVT::i32), DAG, dl);
12397 return DCI.CombineTo(N, InsV);
12398 }
12399
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012400 //===--------------------------------------------------------------------===//
12401 // Combine some shuffles into subvector extracts and inserts:
12402 //
12403
12404 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12405 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12406 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12407 DAG, dl);
12408 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12409 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12410 return DCI.CombineTo(N, InsV);
12411 }
12412
12413 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12414 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12415 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12416 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12417 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12418 return DCI.CombineTo(N, InsV);
12419 }
12420
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012421 return SDValue();
12422}
12423
12424/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012425static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012426 TargetLowering::DAGCombinerInfo &DCI,
12427 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012428 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012429 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012430
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012431 // Don't create instructions with illegal types after legalize types has run.
12432 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12433 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12434 return SDValue();
12435
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012436 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12437 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12438 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012439 return PerformShuffleCombine256(N, DAG, DCI);
12440
12441 // Only handle 128 wide vector from here on.
12442 if (VT.getSizeInBits() != 128)
12443 return SDValue();
12444
12445 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12446 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12447 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012448 SmallVector<SDValue, 16> Elts;
12449 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012450 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012451
Nate Begemanfdea31a2010-03-24 20:49:50 +000012452 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012453}
Evan Chengd880b972008-05-09 21:53:03 +000012454
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012455/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12456/// generation and convert it from being a bunch of shuffles and extracts
12457/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012458static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12459 const TargetLowering &TLI) {
12460 SDValue InputVector = N->getOperand(0);
12461
12462 // Only operate on vectors of 4 elements, where the alternative shuffling
12463 // gets to be more expensive.
12464 if (InputVector.getValueType() != MVT::v4i32)
12465 return SDValue();
12466
12467 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12468 // single use which is a sign-extend or zero-extend, and all elements are
12469 // used.
12470 SmallVector<SDNode *, 4> Uses;
12471 unsigned ExtractedElements = 0;
12472 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12473 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12474 if (UI.getUse().getResNo() != InputVector.getResNo())
12475 return SDValue();
12476
12477 SDNode *Extract = *UI;
12478 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12479 return SDValue();
12480
12481 if (Extract->getValueType(0) != MVT::i32)
12482 return SDValue();
12483 if (!Extract->hasOneUse())
12484 return SDValue();
12485 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12486 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12487 return SDValue();
12488 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12489 return SDValue();
12490
12491 // Record which element was extracted.
12492 ExtractedElements |=
12493 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12494
12495 Uses.push_back(Extract);
12496 }
12497
12498 // If not all the elements were used, this may not be worthwhile.
12499 if (ExtractedElements != 15)
12500 return SDValue();
12501
12502 // Ok, we've now decided to do the transformation.
12503 DebugLoc dl = InputVector.getDebugLoc();
12504
12505 // Store the value to a temporary stack slot.
12506 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012507 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12508 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012509
12510 // Replace each use (extract) with a load of the appropriate element.
12511 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12512 UE = Uses.end(); UI != UE; ++UI) {
12513 SDNode *Extract = *UI;
12514
Nadav Rotem86694292011-05-17 08:31:57 +000012515 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012516 SDValue Idx = Extract->getOperand(1);
12517 unsigned EltSize =
12518 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12519 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12520 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12521
Nadav Rotem86694292011-05-17 08:31:57 +000012522 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012523 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012524
12525 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012526 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012527 ScalarAddr, MachinePointerInfo(),
12528 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012529
12530 // Replace the exact with the load.
12531 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12532 }
12533
12534 // The replacement was made in place; don't return anything.
12535 return SDValue();
12536}
12537
Chris Lattner83e6c992006-10-04 06:57:07 +000012538/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012539static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012540 const X86Subtarget *Subtarget) {
12541 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012542 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012543 // Get the LHS/RHS of the select.
12544 SDValue LHS = N->getOperand(1);
12545 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000012546
Dan Gohman670e5392009-09-21 18:03:22 +000012547 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012548 // instructions match the semantics of the common C idiom x<y?x:y but not
12549 // x<=y?x:y, because of how they handle negative zero (which can be
12550 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000012551 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000012552 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000012553 Cond.getOpcode() == ISD::SETCC) {
12554 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012555
Chris Lattner47b4ce82009-03-11 05:48:52 +000012556 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000012557 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000012558 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12559 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012560 switch (CC) {
12561 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012562 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012563 // Converting this to a min would handle NaNs incorrectly, and swapping
12564 // the operands would cause it to handle comparisons between positive
12565 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012566 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012567 if (!UnsafeFPMath &&
12568 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12569 break;
12570 std::swap(LHS, RHS);
12571 }
Dan Gohman670e5392009-09-21 18:03:22 +000012572 Opcode = X86ISD::FMIN;
12573 break;
12574 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012575 // Converting this to a min would handle comparisons between positive
12576 // and negative zero incorrectly.
12577 if (!UnsafeFPMath &&
12578 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12579 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012580 Opcode = X86ISD::FMIN;
12581 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012582 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012583 // Converting this to a min would handle both negative zeros and NaNs
12584 // incorrectly, but we can swap the operands to fix both.
12585 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012586 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012587 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012588 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012589 Opcode = X86ISD::FMIN;
12590 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012591
Dan Gohman670e5392009-09-21 18:03:22 +000012592 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012593 // Converting this to a max would handle comparisons between positive
12594 // and negative zero incorrectly.
12595 if (!UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000012596 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012597 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012598 Opcode = X86ISD::FMAX;
12599 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012600 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012601 // Converting this to a max would handle NaNs incorrectly, and swapping
12602 // the operands would cause it to handle comparisons between positive
12603 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012604 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012605 if (!UnsafeFPMath &&
12606 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12607 break;
12608 std::swap(LHS, RHS);
12609 }
Dan Gohman670e5392009-09-21 18:03:22 +000012610 Opcode = X86ISD::FMAX;
12611 break;
12612 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012613 // Converting this to a max would handle both negative zeros and NaNs
12614 // incorrectly, but we can swap the operands to fix both.
12615 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012616 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012617 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012618 case ISD::SETGE:
12619 Opcode = X86ISD::FMAX;
12620 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000012621 }
Dan Gohman670e5392009-09-21 18:03:22 +000012622 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000012623 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
12624 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012625 switch (CC) {
12626 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012627 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012628 // Converting this to a min would handle comparisons between positive
12629 // and negative zero incorrectly, and swapping the operands would
12630 // cause it to handle NaNs incorrectly.
12631 if (!UnsafeFPMath &&
12632 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000012633 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012634 break;
12635 std::swap(LHS, RHS);
12636 }
Dan Gohman670e5392009-09-21 18:03:22 +000012637 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000012638 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012639 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012640 // Converting this to a min would handle NaNs incorrectly.
12641 if (!UnsafeFPMath &&
12642 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
12643 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012644 Opcode = X86ISD::FMIN;
12645 break;
12646 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012647 // Converting this to a min would handle both negative zeros and NaNs
12648 // incorrectly, but we can swap the operands to fix both.
12649 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012650 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012651 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012652 case ISD::SETGE:
12653 Opcode = X86ISD::FMIN;
12654 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012655
Dan Gohman670e5392009-09-21 18:03:22 +000012656 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012657 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012658 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012659 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012660 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000012661 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012662 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012663 // Converting this to a max would handle comparisons between positive
12664 // and negative zero incorrectly, and swapping the operands would
12665 // cause it to handle NaNs incorrectly.
12666 if (!UnsafeFPMath &&
12667 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000012668 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012669 break;
12670 std::swap(LHS, RHS);
12671 }
Dan Gohman670e5392009-09-21 18:03:22 +000012672 Opcode = X86ISD::FMAX;
12673 break;
12674 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012675 // Converting this to a max would handle both negative zeros and NaNs
12676 // incorrectly, but we can swap the operands to fix both.
12677 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012678 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012679 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012680 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012681 Opcode = X86ISD::FMAX;
12682 break;
12683 }
Chris Lattner83e6c992006-10-04 06:57:07 +000012684 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012685
Chris Lattner47b4ce82009-03-11 05:48:52 +000012686 if (Opcode)
12687 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000012688 }
Eric Christopherfd179292009-08-27 18:07:15 +000012689
Chris Lattnerd1980a52009-03-12 06:52:53 +000012690 // If this is a select between two integer constants, try to do some
12691 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000012692 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
12693 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000012694 // Don't do this for crazy integer types.
12695 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
12696 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000012697 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012698 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000012699
Chris Lattnercee56e72009-03-13 05:53:31 +000012700 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000012701 // Efficiently invertible.
12702 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
12703 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
12704 isa<ConstantSDNode>(Cond.getOperand(1))))) {
12705 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000012706 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012707 }
Eric Christopherfd179292009-08-27 18:07:15 +000012708
Chris Lattnerd1980a52009-03-12 06:52:53 +000012709 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012710 if (FalseC->getAPIntValue() == 0 &&
12711 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012712 if (NeedsCondInvert) // Invert the condition if needed.
12713 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12714 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012715
Chris Lattnerd1980a52009-03-12 06:52:53 +000012716 // Zero extend the condition if needed.
12717 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012718
Chris Lattnercee56e72009-03-13 05:53:31 +000012719 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000012720 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012721 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012722 }
Eric Christopherfd179292009-08-27 18:07:15 +000012723
Chris Lattner97a29a52009-03-13 05:22:11 +000012724 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000012725 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000012726 if (NeedsCondInvert) // Invert the condition if needed.
12727 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12728 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012729
Chris Lattner97a29a52009-03-13 05:22:11 +000012730 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012731 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12732 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012733 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000012734 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000012735 }
Eric Christopherfd179292009-08-27 18:07:15 +000012736
Chris Lattnercee56e72009-03-13 05:53:31 +000012737 // Optimize cases that will turn into an LEA instruction. This requires
12738 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012739 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012740 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012741 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012742
Chris Lattnercee56e72009-03-13 05:53:31 +000012743 bool isFastMultiplier = false;
12744 if (Diff < 10) {
12745 switch ((unsigned char)Diff) {
12746 default: break;
12747 case 1: // result = add base, cond
12748 case 2: // result = lea base( , cond*2)
12749 case 3: // result = lea base(cond, cond*2)
12750 case 4: // result = lea base( , cond*4)
12751 case 5: // result = lea base(cond, cond*4)
12752 case 8: // result = lea base( , cond*8)
12753 case 9: // result = lea base(cond, cond*8)
12754 isFastMultiplier = true;
12755 break;
12756 }
12757 }
Eric Christopherfd179292009-08-27 18:07:15 +000012758
Chris Lattnercee56e72009-03-13 05:53:31 +000012759 if (isFastMultiplier) {
12760 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
12761 if (NeedsCondInvert) // Invert the condition if needed.
12762 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12763 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012764
Chris Lattnercee56e72009-03-13 05:53:31 +000012765 // Zero extend the condition if needed.
12766 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12767 Cond);
12768 // Scale the condition by the difference.
12769 if (Diff != 1)
12770 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12771 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012772
Chris Lattnercee56e72009-03-13 05:53:31 +000012773 // Add the base if non-zero.
12774 if (FalseC->getAPIntValue() != 0)
12775 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12776 SDValue(FalseC, 0));
12777 return Cond;
12778 }
Eric Christopherfd179292009-08-27 18:07:15 +000012779 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012780 }
12781 }
Eric Christopherfd179292009-08-27 18:07:15 +000012782
Dan Gohman475871a2008-07-27 21:46:04 +000012783 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000012784}
12785
Chris Lattnerd1980a52009-03-12 06:52:53 +000012786/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
12787static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
12788 TargetLowering::DAGCombinerInfo &DCI) {
12789 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000012790
Chris Lattnerd1980a52009-03-12 06:52:53 +000012791 // If the flag operand isn't dead, don't touch this CMOV.
12792 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
12793 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000012794
Evan Chengb5a55d92011-05-24 01:48:22 +000012795 SDValue FalseOp = N->getOperand(0);
12796 SDValue TrueOp = N->getOperand(1);
12797 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
12798 SDValue Cond = N->getOperand(3);
12799 if (CC == X86::COND_E || CC == X86::COND_NE) {
12800 switch (Cond.getOpcode()) {
12801 default: break;
12802 case X86ISD::BSR:
12803 case X86ISD::BSF:
12804 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
12805 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
12806 return (CC == X86::COND_E) ? FalseOp : TrueOp;
12807 }
12808 }
12809
Chris Lattnerd1980a52009-03-12 06:52:53 +000012810 // If this is a select between two integer constants, try to do some
12811 // optimizations. Note that the operands are ordered the opposite of SELECT
12812 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000012813 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
12814 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012815 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
12816 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000012817 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
12818 CC = X86::GetOppositeBranchCondition(CC);
12819 std::swap(TrueC, FalseC);
12820 }
Eric Christopherfd179292009-08-27 18:07:15 +000012821
Chris Lattnerd1980a52009-03-12 06:52:53 +000012822 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012823 // This is efficient for any integer data type (including i8/i16) and
12824 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012825 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012826 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12827 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012828
Chris Lattnerd1980a52009-03-12 06:52:53 +000012829 // Zero extend the condition if needed.
12830 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012831
Chris Lattnerd1980a52009-03-12 06:52:53 +000012832 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
12833 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012834 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012835 if (N->getNumValues() == 2) // Dead flag value?
12836 return DCI.CombineTo(N, Cond, SDValue());
12837 return Cond;
12838 }
Eric Christopherfd179292009-08-27 18:07:15 +000012839
Chris Lattnercee56e72009-03-13 05:53:31 +000012840 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
12841 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000012842 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012843 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12844 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012845
Chris Lattner97a29a52009-03-13 05:22:11 +000012846 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012847 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12848 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012849 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12850 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000012851
Chris Lattner97a29a52009-03-13 05:22:11 +000012852 if (N->getNumValues() == 2) // Dead flag value?
12853 return DCI.CombineTo(N, Cond, SDValue());
12854 return Cond;
12855 }
Eric Christopherfd179292009-08-27 18:07:15 +000012856
Chris Lattnercee56e72009-03-13 05:53:31 +000012857 // Optimize cases that will turn into an LEA instruction. This requires
12858 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012859 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012860 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012861 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012862
Chris Lattnercee56e72009-03-13 05:53:31 +000012863 bool isFastMultiplier = false;
12864 if (Diff < 10) {
12865 switch ((unsigned char)Diff) {
12866 default: break;
12867 case 1: // result = add base, cond
12868 case 2: // result = lea base( , cond*2)
12869 case 3: // result = lea base(cond, cond*2)
12870 case 4: // result = lea base( , cond*4)
12871 case 5: // result = lea base(cond, cond*4)
12872 case 8: // result = lea base( , cond*8)
12873 case 9: // result = lea base(cond, cond*8)
12874 isFastMultiplier = true;
12875 break;
12876 }
12877 }
Eric Christopherfd179292009-08-27 18:07:15 +000012878
Chris Lattnercee56e72009-03-13 05:53:31 +000012879 if (isFastMultiplier) {
12880 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012881 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12882 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000012883 // Zero extend the condition if needed.
12884 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12885 Cond);
12886 // Scale the condition by the difference.
12887 if (Diff != 1)
12888 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12889 DAG.getConstant(Diff, Cond.getValueType()));
12890
12891 // Add the base if non-zero.
12892 if (FalseC->getAPIntValue() != 0)
12893 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12894 SDValue(FalseC, 0));
12895 if (N->getNumValues() == 2) // Dead flag value?
12896 return DCI.CombineTo(N, Cond, SDValue());
12897 return Cond;
12898 }
Eric Christopherfd179292009-08-27 18:07:15 +000012899 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012900 }
12901 }
12902 return SDValue();
12903}
12904
12905
Evan Cheng0b0cd912009-03-28 05:57:29 +000012906/// PerformMulCombine - Optimize a single multiply with constant into two
12907/// in order to implement it with two cheaper instructions, e.g.
12908/// LEA + SHL, LEA + LEA.
12909static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
12910 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000012911 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
12912 return SDValue();
12913
Owen Andersone50ed302009-08-10 22:56:29 +000012914 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012915 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000012916 return SDValue();
12917
12918 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
12919 if (!C)
12920 return SDValue();
12921 uint64_t MulAmt = C->getZExtValue();
12922 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
12923 return SDValue();
12924
12925 uint64_t MulAmt1 = 0;
12926 uint64_t MulAmt2 = 0;
12927 if ((MulAmt % 9) == 0) {
12928 MulAmt1 = 9;
12929 MulAmt2 = MulAmt / 9;
12930 } else if ((MulAmt % 5) == 0) {
12931 MulAmt1 = 5;
12932 MulAmt2 = MulAmt / 5;
12933 } else if ((MulAmt % 3) == 0) {
12934 MulAmt1 = 3;
12935 MulAmt2 = MulAmt / 3;
12936 }
12937 if (MulAmt2 &&
12938 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
12939 DebugLoc DL = N->getDebugLoc();
12940
12941 if (isPowerOf2_64(MulAmt2) &&
12942 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
12943 // If second multiplifer is pow2, issue it first. We want the multiply by
12944 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
12945 // is an add.
12946 std::swap(MulAmt1, MulAmt2);
12947
12948 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000012949 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000012950 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000012951 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000012952 else
Evan Cheng73f24c92009-03-30 21:36:47 +000012953 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000012954 DAG.getConstant(MulAmt1, VT));
12955
Eric Christopherfd179292009-08-27 18:07:15 +000012956 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000012957 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000012958 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000012959 else
Evan Cheng73f24c92009-03-30 21:36:47 +000012960 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000012961 DAG.getConstant(MulAmt2, VT));
12962
12963 // Do not add new nodes to DAG combiner worklist.
12964 DCI.CombineTo(N, NewMul, false);
12965 }
12966 return SDValue();
12967}
12968
Evan Chengad9c0a32009-12-15 00:53:42 +000012969static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
12970 SDValue N0 = N->getOperand(0);
12971 SDValue N1 = N->getOperand(1);
12972 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
12973 EVT VT = N0.getValueType();
12974
12975 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
12976 // since the result of setcc_c is all zero's or all ones.
12977 if (N1C && N0.getOpcode() == ISD::AND &&
12978 N0.getOperand(1).getOpcode() == ISD::Constant) {
12979 SDValue N00 = N0.getOperand(0);
12980 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
12981 ((N00.getOpcode() == ISD::ANY_EXTEND ||
12982 N00.getOpcode() == ISD::ZERO_EXTEND) &&
12983 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
12984 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
12985 APInt ShAmt = N1C->getAPIntValue();
12986 Mask = Mask.shl(ShAmt);
12987 if (Mask != 0)
12988 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
12989 N00, DAG.getConstant(Mask, VT));
12990 }
12991 }
12992
12993 return SDValue();
12994}
Evan Cheng0b0cd912009-03-28 05:57:29 +000012995
Nate Begeman740ab032009-01-26 00:52:55 +000012996/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
12997/// when possible.
12998static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
12999 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013000 EVT VT = N->getValueType(0);
13001 if (!VT.isVector() && VT.isInteger() &&
13002 N->getOpcode() == ISD::SHL)
13003 return PerformSHLCombine(N, DAG);
13004
Nate Begeman740ab032009-01-26 00:52:55 +000013005 // On X86 with SSE2 support, we can transform this to a vector shift if
13006 // all elements are shifted by the same amount. We can't do this in legalize
13007 // because the a constant vector is typically transformed to a constant pool
13008 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000013009 if (!(Subtarget->hasSSE2() || Subtarget->hasAVX()))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013010 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013011
Owen Anderson825b72b2009-08-11 20:47:22 +000013012 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013013 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013014
Mon P Wang3becd092009-01-28 08:12:05 +000013015 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013016 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013017 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013018 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013019 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13020 unsigned NumElts = VT.getVectorNumElements();
13021 unsigned i = 0;
13022 for (; i != NumElts; ++i) {
13023 SDValue Arg = ShAmtOp.getOperand(i);
13024 if (Arg.getOpcode() == ISD::UNDEF) continue;
13025 BaseShAmt = Arg;
13026 break;
13027 }
13028 for (; i != NumElts; ++i) {
13029 SDValue Arg = ShAmtOp.getOperand(i);
13030 if (Arg.getOpcode() == ISD::UNDEF) continue;
13031 if (Arg != BaseShAmt) {
13032 return SDValue();
13033 }
13034 }
13035 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013036 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013037 SDValue InVec = ShAmtOp.getOperand(0);
13038 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13039 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13040 unsigned i = 0;
13041 for (; i != NumElts; ++i) {
13042 SDValue Arg = InVec.getOperand(i);
13043 if (Arg.getOpcode() == ISD::UNDEF) continue;
13044 BaseShAmt = Arg;
13045 break;
13046 }
13047 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13048 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013049 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013050 if (C->getZExtValue() == SplatIdx)
13051 BaseShAmt = InVec.getOperand(1);
13052 }
13053 }
13054 if (BaseShAmt.getNode() == 0)
13055 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13056 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013057 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013058 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013059
Mon P Wangefa42202009-09-03 19:56:25 +000013060 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013061 if (EltVT.bitsGT(MVT::i32))
13062 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13063 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013064 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013065
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013066 // The shift amount is identical so we can do a vector shift.
13067 SDValue ValOp = N->getOperand(0);
13068 switch (N->getOpcode()) {
13069 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013070 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013071 break;
13072 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013073 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013074 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013075 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013076 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013077 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013078 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013079 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013080 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013081 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013082 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013083 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013084 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013085 break;
13086 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013087 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013088 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013089 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013090 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013091 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013092 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013093 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013094 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013095 break;
13096 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013097 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013098 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013099 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013100 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013101 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013102 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013103 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013104 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013105 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013106 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013107 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013108 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013109 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013110 }
13111 return SDValue();
13112}
13113
Nate Begemanb65c1752010-12-17 22:55:37 +000013114
Stuart Hastings865f0932011-06-03 23:53:54 +000013115// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13116// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13117// and friends. Likewise for OR -> CMPNEQSS.
13118static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13119 TargetLowering::DAGCombinerInfo &DCI,
13120 const X86Subtarget *Subtarget) {
13121 unsigned opcode;
13122
13123 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13124 // we're requiring SSE2 for both.
13125 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
13126 SDValue N0 = N->getOperand(0);
13127 SDValue N1 = N->getOperand(1);
13128 SDValue CMP0 = N0->getOperand(1);
13129 SDValue CMP1 = N1->getOperand(1);
13130 DebugLoc DL = N->getDebugLoc();
13131
13132 // The SETCCs should both refer to the same CMP.
13133 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13134 return SDValue();
13135
13136 SDValue CMP00 = CMP0->getOperand(0);
13137 SDValue CMP01 = CMP0->getOperand(1);
13138 EVT VT = CMP00.getValueType();
13139
13140 if (VT == MVT::f32 || VT == MVT::f64) {
13141 bool ExpectingFlags = false;
13142 // Check for any users that want flags:
13143 for (SDNode::use_iterator UI = N->use_begin(),
13144 UE = N->use_end();
13145 !ExpectingFlags && UI != UE; ++UI)
13146 switch (UI->getOpcode()) {
13147 default:
13148 case ISD::BR_CC:
13149 case ISD::BRCOND:
13150 case ISD::SELECT:
13151 ExpectingFlags = true;
13152 break;
13153 case ISD::CopyToReg:
13154 case ISD::SIGN_EXTEND:
13155 case ISD::ZERO_EXTEND:
13156 case ISD::ANY_EXTEND:
13157 break;
13158 }
13159
13160 if (!ExpectingFlags) {
13161 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13162 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13163
13164 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13165 X86::CondCode tmp = cc0;
13166 cc0 = cc1;
13167 cc1 = tmp;
13168 }
13169
13170 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13171 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13172 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13173 X86ISD::NodeType NTOperator = is64BitFP ?
13174 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13175 // FIXME: need symbolic constants for these magic numbers.
13176 // See X86ATTInstPrinter.cpp:printSSECC().
13177 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13178 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13179 DAG.getConstant(x86cc, MVT::i8));
13180 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13181 OnesOrZeroesF);
13182 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13183 DAG.getConstant(1, MVT::i32));
13184 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13185 return OneBitOfTruth;
13186 }
13187 }
13188 }
13189 }
13190 return SDValue();
13191}
13192
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013193/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13194/// so it can be folded inside ANDNP.
13195static bool CanFoldXORWithAllOnes(const SDNode *N) {
13196 EVT VT = N->getValueType(0);
13197
13198 // Match direct AllOnes for 128 and 256-bit vectors
13199 if (ISD::isBuildVectorAllOnes(N))
13200 return true;
13201
13202 // Look through a bit convert.
13203 if (N->getOpcode() == ISD::BITCAST)
13204 N = N->getOperand(0).getNode();
13205
13206 // Sometimes the operand may come from a insert_subvector building a 256-bit
13207 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013208 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013209 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13210 SDValue V1 = N->getOperand(0);
13211 SDValue V2 = N->getOperand(1);
13212
13213 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13214 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13215 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13216 ISD::isBuildVectorAllOnes(V2.getNode()))
13217 return true;
13218 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013219
13220 return false;
13221}
13222
Nate Begemanb65c1752010-12-17 22:55:37 +000013223static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13224 TargetLowering::DAGCombinerInfo &DCI,
13225 const X86Subtarget *Subtarget) {
13226 if (DCI.isBeforeLegalizeOps())
13227 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013228
Stuart Hastings865f0932011-06-03 23:53:54 +000013229 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13230 if (R.getNode())
13231 return R;
13232
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013233 // Want to form ANDNP nodes:
13234 // 1) In the hopes of then easily combining them with OR and AND nodes
13235 // to form PBLEND/PSIGN.
13236 // 2) To match ANDN packed intrinsics
Nate Begemanb65c1752010-12-17 22:55:37 +000013237 EVT VT = N->getValueType(0);
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013238 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013239 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013240
Nate Begemanb65c1752010-12-17 22:55:37 +000013241 SDValue N0 = N->getOperand(0);
13242 SDValue N1 = N->getOperand(1);
13243 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013244
Nate Begemanb65c1752010-12-17 22:55:37 +000013245 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013246 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013247 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13248 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013249 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013250
13251 // Check RHS for vnot
13252 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013253 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13254 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013255 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013256
Nate Begemanb65c1752010-12-17 22:55:37 +000013257 return SDValue();
13258}
13259
Evan Cheng760d1942010-01-04 21:22:48 +000013260static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013261 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013262 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013263 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013264 return SDValue();
13265
Stuart Hastings865f0932011-06-03 23:53:54 +000013266 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13267 if (R.getNode())
13268 return R;
13269
Evan Cheng760d1942010-01-04 21:22:48 +000013270 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000013271 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000013272 return SDValue();
13273
Evan Cheng760d1942010-01-04 21:22:48 +000013274 SDValue N0 = N->getOperand(0);
13275 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013276
Nate Begemanb65c1752010-12-17 22:55:37 +000013277 // look for psign/blend
13278 if (Subtarget->hasSSSE3()) {
13279 if (VT == MVT::v2i64) {
13280 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013281 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000013282 std::swap(N0, N1);
13283 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013284 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013285 SDValue Mask = N1.getOperand(0);
13286 SDValue X = N1.getOperand(1);
13287 SDValue Y;
13288 if (N0.getOperand(0) == Mask)
13289 Y = N0.getOperand(1);
13290 if (N0.getOperand(1) == Mask)
13291 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013292
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013293 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000013294 if (!Y.getNode())
13295 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013296
Nate Begemanb65c1752010-12-17 22:55:37 +000013297 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13298 if (Mask.getOpcode() != ISD::BITCAST ||
13299 X.getOpcode() != ISD::BITCAST ||
13300 Y.getOpcode() != ISD::BITCAST)
13301 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013302
Nate Begemanb65c1752010-12-17 22:55:37 +000013303 // Look through mask bitcast.
13304 Mask = Mask.getOperand(0);
13305 EVT MaskVT = Mask.getValueType();
13306
13307 // Validate that the Mask operand is a vector sra node. The sra node
13308 // will be an intrinsic.
13309 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13310 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013311
Nate Begemanb65c1752010-12-17 22:55:37 +000013312 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13313 // there is no psrai.b
13314 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13315 case Intrinsic::x86_sse2_psrai_w:
13316 case Intrinsic::x86_sse2_psrai_d:
13317 break;
13318 default: return SDValue();
13319 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013320
Nate Begemanb65c1752010-12-17 22:55:37 +000013321 // Check that the SRA is all signbits.
13322 SDValue SraC = Mask.getOperand(2);
13323 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13324 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13325 if ((SraAmt + 1) != EltBits)
13326 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013327
Nate Begemanb65c1752010-12-17 22:55:37 +000013328 DebugLoc DL = N->getDebugLoc();
13329
13330 // Now we know we at least have a plendvb with the mask val. See if
13331 // we can form a psignb/w/d.
13332 // psign = x.type == y.type == mask.type && y = sub(0, x);
13333 X = X.getOperand(0);
13334 Y = Y.getOperand(0);
13335 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13336 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
13337 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
13338 unsigned Opc = 0;
13339 switch (EltBits) {
13340 case 8: Opc = X86ISD::PSIGNB; break;
13341 case 16: Opc = X86ISD::PSIGNW; break;
13342 case 32: Opc = X86ISD::PSIGND; break;
13343 default: break;
13344 }
13345 if (Opc) {
13346 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
13347 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
13348 }
13349 }
13350 // PBLENDVB only available on SSE 4.1
13351 if (!Subtarget->hasSSE41())
13352 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013353
Nate Begemanb65c1752010-12-17 22:55:37 +000013354 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
13355 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
13356 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000013357 Mask = DAG.getNode(ISD::VSELECT, DL, MVT::v16i8, Mask, X, Y);
Nate Begemanb65c1752010-12-17 22:55:37 +000013358 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
13359 }
13360 }
13361 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013362
Nate Begemanb65c1752010-12-17 22:55:37 +000013363 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013364 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13365 std::swap(N0, N1);
13366 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13367 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013368 if (!N0.hasOneUse() || !N1.hasOneUse())
13369 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013370
13371 SDValue ShAmt0 = N0.getOperand(1);
13372 if (ShAmt0.getValueType() != MVT::i8)
13373 return SDValue();
13374 SDValue ShAmt1 = N1.getOperand(1);
13375 if (ShAmt1.getValueType() != MVT::i8)
13376 return SDValue();
13377 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13378 ShAmt0 = ShAmt0.getOperand(0);
13379 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13380 ShAmt1 = ShAmt1.getOperand(0);
13381
13382 DebugLoc DL = N->getDebugLoc();
13383 unsigned Opc = X86ISD::SHLD;
13384 SDValue Op0 = N0.getOperand(0);
13385 SDValue Op1 = N1.getOperand(0);
13386 if (ShAmt0.getOpcode() == ISD::SUB) {
13387 Opc = X86ISD::SHRD;
13388 std::swap(Op0, Op1);
13389 std::swap(ShAmt0, ShAmt1);
13390 }
13391
Evan Cheng8b1190a2010-04-28 01:18:01 +000013392 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013393 if (ShAmt1.getOpcode() == ISD::SUB) {
13394 SDValue Sum = ShAmt1.getOperand(0);
13395 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013396 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13397 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13398 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13399 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013400 return DAG.getNode(Opc, DL, VT,
13401 Op0, Op1,
13402 DAG.getNode(ISD::TRUNCATE, DL,
13403 MVT::i8, ShAmt0));
13404 }
13405 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13406 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13407 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013408 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013409 return DAG.getNode(Opc, DL, VT,
13410 N0.getOperand(0), N1.getOperand(0),
13411 DAG.getNode(ISD::TRUNCATE, DL,
13412 MVT::i8, ShAmt0));
13413 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013414
Evan Cheng760d1942010-01-04 21:22:48 +000013415 return SDValue();
13416}
13417
Chris Lattner149a4e52008-02-22 02:09:43 +000013418/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013419static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000013420 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000013421 StoreSDNode *St = cast<StoreSDNode>(N);
13422 EVT VT = St->getValue().getValueType();
13423 EVT StVT = St->getMemoryVT();
13424 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000013425 SDValue StoredVal = St->getOperand(1);
13426 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13427
13428 // If we are saving a concatination of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000013429 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
13430 // 128-bit ones. If in the future the cost becomes only one memory access the
13431 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000013432 if (VT.getSizeInBits() == 256 &&
13433 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
13434 StoredVal.getNumOperands() == 2) {
13435
13436 SDValue Value0 = StoredVal.getOperand(0);
13437 SDValue Value1 = StoredVal.getOperand(1);
13438
13439 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
13440 SDValue Ptr0 = St->getBasePtr();
13441 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
13442
13443 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
13444 St->getPointerInfo(), St->isVolatile(),
13445 St->isNonTemporal(), St->getAlignment());
13446 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
13447 St->getPointerInfo(), St->isVolatile(),
13448 St->isNonTemporal(), St->getAlignment());
13449 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
13450 }
Nadav Rotem614061b2011-08-10 19:30:14 +000013451
13452 // Optimize trunc store (of multiple scalars) to shuffle and store.
13453 // First, pack all of the elements in one place. Next, store to memory
13454 // in fewer chunks.
13455 if (St->isTruncatingStore() && VT.isVector()) {
13456 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13457 unsigned NumElems = VT.getVectorNumElements();
13458 assert(StVT != VT && "Cannot truncate to the same type");
13459 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
13460 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
13461
13462 // From, To sizes and ElemCount must be pow of two
13463 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
13464 // We are going to use the original vector elt for storing.
13465 // accumulated smaller vector elements must be a multiple of bigger size.
13466 if (0 != (NumElems * ToSz) % FromSz) return SDValue();
13467 unsigned SizeRatio = FromSz / ToSz;
13468
13469 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
13470
13471 // Create a type on which we perform the shuffle
13472 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
13473 StVT.getScalarType(), NumElems*SizeRatio);
13474
13475 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
13476
13477 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
13478 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13479 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
13480
13481 // Can't shuffle using an illegal type
13482 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13483
13484 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
13485 DAG.getUNDEF(WideVec.getValueType()),
13486 ShuffleVec.data());
13487 // At this point all of the data is stored at the bottom of the
13488 // register. We now need to save it to mem.
13489
13490 // Find the largest store unit
13491 MVT StoreType = MVT::i8;
13492 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13493 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13494 MVT Tp = (MVT::SimpleValueType)tp;
13495 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
13496 StoreType = Tp;
13497 }
13498
13499 // Bitcast the original vector into a vector of store-size units
13500 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
13501 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
13502 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
13503 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
13504 SmallVector<SDValue, 8> Chains;
13505 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
13506 TLI.getPointerTy());
13507 SDValue Ptr = St->getBasePtr();
13508
13509 // Perform one or more big stores into memory.
13510 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
13511 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
13512 StoreType, ShuffWide,
13513 DAG.getIntPtrConstant(i));
13514 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
13515 St->getPointerInfo(), St->isVolatile(),
13516 St->isNonTemporal(), St->getAlignment());
13517 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
13518 Chains.push_back(Ch);
13519 }
13520
13521 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
13522 Chains.size());
13523 }
13524
13525
Chris Lattner149a4e52008-02-22 02:09:43 +000013526 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
13527 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000013528 // A preferable solution to the general problem is to figure out the right
13529 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000013530
13531 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000013532 if (VT.getSizeInBits() != 64)
13533 return SDValue();
13534
Devang Patel578efa92009-06-05 21:57:13 +000013535 const Function *F = DAG.getMachineFunction().getFunction();
13536 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000013537 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000013538 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000013539 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000013540 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000013541 isa<LoadSDNode>(St->getValue()) &&
13542 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
13543 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013544 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013545 LoadSDNode *Ld = 0;
13546 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000013547 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000013548 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013549 // Must be a store of a load. We currently handle two cases: the load
13550 // is a direct child, and it's under an intervening TokenFactor. It is
13551 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000013552 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000013553 Ld = cast<LoadSDNode>(St->getChain());
13554 else if (St->getValue().hasOneUse() &&
13555 ChainVal->getOpcode() == ISD::TokenFactor) {
13556 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013557 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000013558 TokenFactorIndex = i;
13559 Ld = cast<LoadSDNode>(St->getValue());
13560 } else
13561 Ops.push_back(ChainVal->getOperand(i));
13562 }
13563 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000013564
Evan Cheng536e6672009-03-12 05:59:15 +000013565 if (!Ld || !ISD::isNormalLoad(Ld))
13566 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013567
Evan Cheng536e6672009-03-12 05:59:15 +000013568 // If this is not the MMX case, i.e. we are just turning i64 load/store
13569 // into f64 load/store, avoid the transformation if there are multiple
13570 // uses of the loaded value.
13571 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
13572 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013573
Evan Cheng536e6672009-03-12 05:59:15 +000013574 DebugLoc LdDL = Ld->getDebugLoc();
13575 DebugLoc StDL = N->getDebugLoc();
13576 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
13577 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
13578 // pair instead.
13579 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013580 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000013581 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
13582 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013583 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013584 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000013585 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000013586 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000013587 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000013588 Ops.size());
13589 }
Evan Cheng536e6672009-03-12 05:59:15 +000013590 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013591 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013592 St->isVolatile(), St->isNonTemporal(),
13593 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000013594 }
Evan Cheng536e6672009-03-12 05:59:15 +000013595
13596 // Otherwise, lower to two pairs of 32-bit loads / stores.
13597 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013598 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
13599 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013600
Owen Anderson825b72b2009-08-11 20:47:22 +000013601 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013602 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013603 Ld->isVolatile(), Ld->isNonTemporal(),
13604 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000013605 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013606 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000013607 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013608 MinAlign(Ld->getAlignment(), 4));
13609
13610 SDValue NewChain = LoLd.getValue(1);
13611 if (TokenFactorIndex != -1) {
13612 Ops.push_back(LoLd);
13613 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000013614 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000013615 Ops.size());
13616 }
13617
13618 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013619 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
13620 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013621
13622 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013623 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013624 St->isVolatile(), St->isNonTemporal(),
13625 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013626 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013627 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000013628 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013629 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013630 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000013631 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000013632 }
Dan Gohman475871a2008-07-27 21:46:04 +000013633 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000013634}
13635
Chris Lattner6cf73262008-01-25 06:14:17 +000013636/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
13637/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013638static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000013639 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
13640 // F[X]OR(0.0, x) -> x
13641 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000013642 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13643 if (C->getValueAPF().isPosZero())
13644 return N->getOperand(1);
13645 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13646 if (C->getValueAPF().isPosZero())
13647 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000013648 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013649}
13650
13651/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013652static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000013653 // FAND(0.0, x) -> 0.0
13654 // FAND(x, 0.0) -> 0.0
13655 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13656 if (C->getValueAPF().isPosZero())
13657 return N->getOperand(0);
13658 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13659 if (C->getValueAPF().isPosZero())
13660 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000013661 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013662}
13663
Dan Gohmane5af2d32009-01-29 01:59:02 +000013664static SDValue PerformBTCombine(SDNode *N,
13665 SelectionDAG &DAG,
13666 TargetLowering::DAGCombinerInfo &DCI) {
13667 // BT ignores high bits in the bit index operand.
13668 SDValue Op1 = N->getOperand(1);
13669 if (Op1.hasOneUse()) {
13670 unsigned BitWidth = Op1.getValueSizeInBits();
13671 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
13672 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013673 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
13674 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000013675 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000013676 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
13677 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
13678 DCI.CommitTargetLoweringOpt(TLO);
13679 }
13680 return SDValue();
13681}
Chris Lattner83e6c992006-10-04 06:57:07 +000013682
Eli Friedman7a5e5552009-06-07 06:52:44 +000013683static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
13684 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013685 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000013686 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000013687 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000013688 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000013689 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000013690 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013691 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000013692 }
13693 return SDValue();
13694}
13695
Evan Cheng2e489c42009-12-16 00:53:11 +000013696static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
13697 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
13698 // (and (i32 x86isd::setcc_carry), 1)
13699 // This eliminates the zext. This transformation is necessary because
13700 // ISD::SETCC is always legalized to i8.
13701 DebugLoc dl = N->getDebugLoc();
13702 SDValue N0 = N->getOperand(0);
13703 EVT VT = N->getValueType(0);
13704 if (N0.getOpcode() == ISD::AND &&
13705 N0.hasOneUse() &&
13706 N0.getOperand(0).hasOneUse()) {
13707 SDValue N00 = N0.getOperand(0);
13708 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
13709 return SDValue();
13710 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
13711 if (!C || C->getZExtValue() != 1)
13712 return SDValue();
13713 return DAG.getNode(ISD::AND, dl, VT,
13714 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
13715 N00.getOperand(0), N00.getOperand(1)),
13716 DAG.getConstant(1, VT));
13717 }
13718
13719 return SDValue();
13720}
13721
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013722// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
13723static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
13724 unsigned X86CC = N->getConstantOperandVal(0);
13725 SDValue EFLAG = N->getOperand(1);
13726 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013727
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013728 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
13729 // a zext and produces an all-ones bit which is more useful than 0/1 in some
13730 // cases.
13731 if (X86CC == X86::COND_B)
13732 return DAG.getNode(ISD::AND, DL, MVT::i8,
13733 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
13734 DAG.getConstant(X86CC, MVT::i8), EFLAG),
13735 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013736
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013737 return SDValue();
13738}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013739
Benjamin Kramer1396c402011-06-18 11:09:41 +000013740static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
13741 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013742 SDValue Op0 = N->getOperand(0);
13743 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
13744 // a 32-bit target where SSE doesn't support i64->FP operations.
13745 if (Op0.getOpcode() == ISD::LOAD) {
13746 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
13747 EVT VT = Ld->getValueType(0);
13748 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
13749 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
13750 !XTLI->getSubtarget()->is64Bit() &&
13751 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000013752 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
13753 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013754 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
13755 return FILDChain;
13756 }
13757 }
13758 return SDValue();
13759}
13760
Chris Lattner23a01992010-12-20 01:37:09 +000013761// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
13762static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
13763 X86TargetLowering::DAGCombinerInfo &DCI) {
13764 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
13765 // the result is either zero or one (depending on the input carry bit).
13766 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
13767 if (X86::isZeroNode(N->getOperand(0)) &&
13768 X86::isZeroNode(N->getOperand(1)) &&
13769 // We don't have a good way to replace an EFLAGS use, so only do this when
13770 // dead right now.
13771 SDValue(N, 1).use_empty()) {
13772 DebugLoc DL = N->getDebugLoc();
13773 EVT VT = N->getValueType(0);
13774 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
13775 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
13776 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
13777 DAG.getConstant(X86::COND_B,MVT::i8),
13778 N->getOperand(2)),
13779 DAG.getConstant(1, VT));
13780 return DCI.CombineTo(N, Res1, CarryOut);
13781 }
13782
13783 return SDValue();
13784}
13785
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013786// fold (add Y, (sete X, 0)) -> adc 0, Y
13787// (add Y, (setne X, 0)) -> sbb -1, Y
13788// (sub (sete X, 0), Y) -> sbb 0, Y
13789// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013790static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013791 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013792
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013793 // Look through ZExts.
13794 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
13795 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
13796 return SDValue();
13797
13798 SDValue SetCC = Ext.getOperand(0);
13799 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
13800 return SDValue();
13801
13802 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
13803 if (CC != X86::COND_E && CC != X86::COND_NE)
13804 return SDValue();
13805
13806 SDValue Cmp = SetCC.getOperand(1);
13807 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000013808 !X86::isZeroNode(Cmp.getOperand(1)) ||
13809 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000013810 return SDValue();
13811
13812 SDValue CmpOp0 = Cmp.getOperand(0);
13813 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
13814 DAG.getConstant(1, CmpOp0.getValueType()));
13815
13816 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
13817 if (CC == X86::COND_NE)
13818 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
13819 DL, OtherVal.getValueType(), OtherVal,
13820 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
13821 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
13822 DL, OtherVal.getValueType(), OtherVal,
13823 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
13824}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013825
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013826static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG) {
13827 SDValue Op0 = N->getOperand(0);
13828 SDValue Op1 = N->getOperand(1);
13829
13830 // X86 can't encode an immediate LHS of a sub. See if we can push the
13831 // negation into a preceding instruction.
13832 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013833 // If the RHS of the sub is a XOR with one use and a constant, invert the
13834 // immediate. Then add one to the LHS of the sub so we can turn
13835 // X-Y -> X+~Y+1, saving one register.
13836 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
13837 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000013838 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013839 EVT VT = Op0.getValueType();
13840 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
13841 Op1.getOperand(0),
13842 DAG.getConstant(~XorC, VT));
13843 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000013844 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013845 }
13846 }
13847
13848 return OptimizeConditionalInDecrement(N, DAG);
13849}
13850
Dan Gohman475871a2008-07-27 21:46:04 +000013851SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000013852 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013853 SelectionDAG &DAG = DCI.DAG;
13854 switch (N->getOpcode()) {
13855 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013856 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013857 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000013858 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013859 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000013860 case ISD::ADD: return OptimizeConditionalInDecrement(N, DAG);
13861 case ISD::SUB: return PerformSubCombine(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000013862 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000013863 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000013864 case ISD::SHL:
13865 case ISD::SRA:
13866 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000013867 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000013868 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000013869 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000013870 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Chris Lattner6cf73262008-01-25 06:14:17 +000013871 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000013872 case X86ISD::FOR: return PerformFORCombine(N, DAG);
13873 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000013874 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000013875 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000013876 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013877 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013878 case X86ISD::SHUFPS: // Handle all target specific shuffles
13879 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000013880 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013881 case X86ISD::PUNPCKHBW:
13882 case X86ISD::PUNPCKHWD:
13883 case X86ISD::PUNPCKHDQ:
13884 case X86ISD::PUNPCKHQDQ:
13885 case X86ISD::UNPCKHPS:
13886 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +000013887 case X86ISD::VUNPCKHPSY:
13888 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013889 case X86ISD::PUNPCKLBW:
13890 case X86ISD::PUNPCKLWD:
13891 case X86ISD::PUNPCKLDQ:
13892 case X86ISD::PUNPCKLQDQ:
13893 case X86ISD::UNPCKLPS:
13894 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000013895 case X86ISD::VUNPCKLPSY:
13896 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013897 case X86ISD::MOVHLPS:
13898 case X86ISD::MOVLHPS:
13899 case X86ISD::PSHUFD:
13900 case X86ISD::PSHUFHW:
13901 case X86ISD::PSHUFLW:
13902 case X86ISD::MOVSS:
13903 case X86ISD::MOVSD:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000013904 case X86ISD::VPERMILPS:
13905 case X86ISD::VPERMILPSY:
13906 case X86ISD::VPERMILPD:
13907 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000013908 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013909 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013910 }
13911
Dan Gohman475871a2008-07-27 21:46:04 +000013912 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013913}
13914
Evan Chenge5b51ac2010-04-17 06:13:15 +000013915/// isTypeDesirableForOp - Return true if the target has native support for
13916/// the specified value type and it is 'desirable' to use the type for the
13917/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
13918/// instruction encodings are longer and some i16 instructions are slow.
13919bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
13920 if (!isTypeLegal(VT))
13921 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013922 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000013923 return true;
13924
13925 switch (Opc) {
13926 default:
13927 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000013928 case ISD::LOAD:
13929 case ISD::SIGN_EXTEND:
13930 case ISD::ZERO_EXTEND:
13931 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000013932 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000013933 case ISD::SRL:
13934 case ISD::SUB:
13935 case ISD::ADD:
13936 case ISD::MUL:
13937 case ISD::AND:
13938 case ISD::OR:
13939 case ISD::XOR:
13940 return false;
13941 }
13942}
13943
13944/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000013945/// beneficial for dag combiner to promote the specified node. If true, it
13946/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000013947bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000013948 EVT VT = Op.getValueType();
13949 if (VT != MVT::i16)
13950 return false;
13951
Evan Cheng4c26e932010-04-19 19:29:22 +000013952 bool Promote = false;
13953 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000013954 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000013955 default: break;
13956 case ISD::LOAD: {
13957 LoadSDNode *LD = cast<LoadSDNode>(Op);
13958 // If the non-extending load has a single use and it's not live out, then it
13959 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013960 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
13961 Op.hasOneUse()*/) {
13962 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
13963 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
13964 // The only case where we'd want to promote LOAD (rather then it being
13965 // promoted as an operand is when it's only use is liveout.
13966 if (UI->getOpcode() != ISD::CopyToReg)
13967 return false;
13968 }
13969 }
Evan Cheng4c26e932010-04-19 19:29:22 +000013970 Promote = true;
13971 break;
13972 }
13973 case ISD::SIGN_EXTEND:
13974 case ISD::ZERO_EXTEND:
13975 case ISD::ANY_EXTEND:
13976 Promote = true;
13977 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013978 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000013979 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000013980 SDValue N0 = Op.getOperand(0);
13981 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000013982 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000013983 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000013984 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013985 break;
13986 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000013987 case ISD::ADD:
13988 case ISD::MUL:
13989 case ISD::AND:
13990 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000013991 case ISD::XOR:
13992 Commute = true;
13993 // fallthrough
13994 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000013995 SDValue N0 = Op.getOperand(0);
13996 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000013997 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000013998 return false;
13999 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014000 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014001 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014002 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014003 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014004 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014005 }
14006 }
14007
14008 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014009 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014010}
14011
Evan Cheng60c07e12006-07-05 22:17:51 +000014012//===----------------------------------------------------------------------===//
14013// X86 Inline Assembly Support
14014//===----------------------------------------------------------------------===//
14015
Chris Lattnerb8105652009-07-20 17:51:36 +000014016bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14017 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014018
14019 std::string AsmStr = IA->getAsmString();
14020
14021 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014022 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014023 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014024
14025 switch (AsmPieces.size()) {
14026 default: return false;
14027 case 1:
14028 AsmStr = AsmPieces[0];
14029 AsmPieces.clear();
14030 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
14031
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014032 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000014033 // we will turn this bswap into something that will be lowered to logical ops
14034 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14035 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014036 // bswap $0
14037 if (AsmPieces.size() == 2 &&
14038 (AsmPieces[0] == "bswap" ||
14039 AsmPieces[0] == "bswapq" ||
14040 AsmPieces[0] == "bswapl") &&
14041 (AsmPieces[1] == "$0" ||
14042 AsmPieces[1] == "${0:q}")) {
14043 // No need to check constraints, nothing other than the equivalent of
14044 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014045 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014046 if (!Ty || Ty->getBitWidth() % 16 != 0)
14047 return false;
14048 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014049 }
14050 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014051 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014052 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014053 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014054 AsmPieces[1] == "$$8," &&
14055 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014056 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14057 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014058 const std::string &ConstraintsStr = IA->getConstraintString();
14059 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014060 std::sort(AsmPieces.begin(), AsmPieces.end());
14061 if (AsmPieces.size() == 4 &&
14062 AsmPieces[0] == "~{cc}" &&
14063 AsmPieces[1] == "~{dirflag}" &&
14064 AsmPieces[2] == "~{flags}" &&
14065 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014066 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014067 if (!Ty || Ty->getBitWidth() % 16 != 0)
14068 return false;
14069 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014070 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014071 }
14072 break;
14073 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014074 if (CI->getType()->isIntegerTy(32) &&
14075 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14076 SmallVector<StringRef, 4> Words;
14077 SplitString(AsmPieces[0], Words, " \t,");
14078 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14079 Words[2] == "${0:w}") {
14080 Words.clear();
14081 SplitString(AsmPieces[1], Words, " \t,");
14082 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14083 Words[2] == "$0") {
14084 Words.clear();
14085 SplitString(AsmPieces[2], Words, " \t,");
14086 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14087 Words[2] == "${0:w}") {
14088 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014089 const std::string &ConstraintsStr = IA->getConstraintString();
14090 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014091 std::sort(AsmPieces.begin(), AsmPieces.end());
14092 if (AsmPieces.size() == 4 &&
14093 AsmPieces[0] == "~{cc}" &&
14094 AsmPieces[1] == "~{dirflag}" &&
14095 AsmPieces[2] == "~{flags}" &&
14096 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014097 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014098 if (!Ty || Ty->getBitWidth() % 16 != 0)
14099 return false;
14100 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014101 }
14102 }
14103 }
14104 }
14105 }
Evan Cheng55d42002011-01-08 01:24:27 +000014106
14107 if (CI->getType()->isIntegerTy(64)) {
14108 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14109 if (Constraints.size() >= 2 &&
14110 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14111 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14112 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14113 SmallVector<StringRef, 4> Words;
14114 SplitString(AsmPieces[0], Words, " \t");
14115 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014116 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014117 SplitString(AsmPieces[1], Words, " \t");
14118 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14119 Words.clear();
14120 SplitString(AsmPieces[2], Words, " \t,");
14121 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14122 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014123 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014124 if (!Ty || Ty->getBitWidth() % 16 != 0)
14125 return false;
14126 return IntrinsicLowering::LowerToByteSwap(CI);
14127 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014128 }
14129 }
14130 }
14131 }
14132 break;
14133 }
14134 return false;
14135}
14136
14137
14138
Chris Lattnerf4dff842006-07-11 02:54:03 +000014139/// getConstraintType - Given a constraint letter, return the type of
14140/// constraint it is for this target.
14141X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014142X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14143 if (Constraint.size() == 1) {
14144 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014145 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014146 case 'q':
14147 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014148 case 'f':
14149 case 't':
14150 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014151 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014152 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014153 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014154 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014155 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014156 case 'a':
14157 case 'b':
14158 case 'c':
14159 case 'd':
14160 case 'S':
14161 case 'D':
14162 case 'A':
14163 return C_Register;
14164 case 'I':
14165 case 'J':
14166 case 'K':
14167 case 'L':
14168 case 'M':
14169 case 'N':
14170 case 'G':
14171 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014172 case 'e':
14173 case 'Z':
14174 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014175 default:
14176 break;
14177 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014178 }
Chris Lattner4234f572007-03-25 02:14:49 +000014179 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014180}
14181
John Thompson44ab89e2010-10-29 17:29:13 +000014182/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014183/// This object must already have been set up with the operand type
14184/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014185TargetLowering::ConstraintWeight
14186 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014187 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014188 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014189 Value *CallOperandVal = info.CallOperandVal;
14190 // If we don't have a value, we can't do a match,
14191 // but allow it at the lowest weight.
14192 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014193 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014194 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014195 // Look at the constraint type.
14196 switch (*constraint) {
14197 default:
John Thompson44ab89e2010-10-29 17:29:13 +000014198 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
14199 case 'R':
14200 case 'q':
14201 case 'Q':
14202 case 'a':
14203 case 'b':
14204 case 'c':
14205 case 'd':
14206 case 'S':
14207 case 'D':
14208 case 'A':
14209 if (CallOperandVal->getType()->isIntegerTy())
14210 weight = CW_SpecificReg;
14211 break;
14212 case 'f':
14213 case 't':
14214 case 'u':
14215 if (type->isFloatingPointTy())
14216 weight = CW_SpecificReg;
14217 break;
14218 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000014219 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000014220 weight = CW_SpecificReg;
14221 break;
14222 case 'x':
14223 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014224 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000014225 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014226 break;
14227 case 'I':
14228 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
14229 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000014230 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014231 }
14232 break;
John Thompson44ab89e2010-10-29 17:29:13 +000014233 case 'J':
14234 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14235 if (C->getZExtValue() <= 63)
14236 weight = CW_Constant;
14237 }
14238 break;
14239 case 'K':
14240 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14241 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
14242 weight = CW_Constant;
14243 }
14244 break;
14245 case 'L':
14246 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14247 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
14248 weight = CW_Constant;
14249 }
14250 break;
14251 case 'M':
14252 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14253 if (C->getZExtValue() <= 3)
14254 weight = CW_Constant;
14255 }
14256 break;
14257 case 'N':
14258 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14259 if (C->getZExtValue() <= 0xff)
14260 weight = CW_Constant;
14261 }
14262 break;
14263 case 'G':
14264 case 'C':
14265 if (dyn_cast<ConstantFP>(CallOperandVal)) {
14266 weight = CW_Constant;
14267 }
14268 break;
14269 case 'e':
14270 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14271 if ((C->getSExtValue() >= -0x80000000LL) &&
14272 (C->getSExtValue() <= 0x7fffffffLL))
14273 weight = CW_Constant;
14274 }
14275 break;
14276 case 'Z':
14277 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14278 if (C->getZExtValue() <= 0xffffffff)
14279 weight = CW_Constant;
14280 }
14281 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014282 }
14283 return weight;
14284}
14285
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014286/// LowerXConstraint - try to replace an X constraint, which matches anything,
14287/// with another that has more specific requirements based on the type of the
14288/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000014289const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000014290LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000014291 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
14292 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000014293 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014294 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000014295 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014296 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000014297 return "x";
14298 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014299
Chris Lattner5e764232008-04-26 23:02:14 +000014300 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014301}
14302
Chris Lattner48884cd2007-08-25 00:47:38 +000014303/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
14304/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000014305void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000014306 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000014307 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000014308 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000014309 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000014310
Eric Christopher100c8332011-06-02 23:16:42 +000014311 // Only support length 1 constraints for now.
14312 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000014313
Eric Christopher100c8332011-06-02 23:16:42 +000014314 char ConstraintLetter = Constraint[0];
14315 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014316 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000014317 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000014318 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014319 if (C->getZExtValue() <= 31) {
14320 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014321 break;
14322 }
Devang Patel84f7fd22007-03-17 00:13:28 +000014323 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014324 return;
Evan Cheng364091e2008-09-22 23:57:37 +000014325 case 'J':
14326 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014327 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000014328 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14329 break;
14330 }
14331 }
14332 return;
14333 case 'K':
14334 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014335 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000014336 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14337 break;
14338 }
14339 }
14340 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000014341 case 'N':
14342 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014343 if (C->getZExtValue() <= 255) {
14344 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014345 break;
14346 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000014347 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014348 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000014349 case 'e': {
14350 // 32-bit signed value
14351 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014352 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14353 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014354 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014355 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000014356 break;
14357 }
14358 // FIXME gcc accepts some relocatable values here too, but only in certain
14359 // memory models; it's complicated.
14360 }
14361 return;
14362 }
14363 case 'Z': {
14364 // 32-bit unsigned value
14365 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014366 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14367 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014368 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14369 break;
14370 }
14371 }
14372 // FIXME gcc accepts some relocatable values here too, but only in certain
14373 // memory models; it's complicated.
14374 return;
14375 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014376 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014377 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000014378 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014379 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014380 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000014381 break;
14382 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014383
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014384 // In any sort of PIC mode addresses need to be computed at runtime by
14385 // adding in a register or some sort of table lookup. These can't
14386 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000014387 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014388 return;
14389
Chris Lattnerdc43a882007-05-03 16:52:29 +000014390 // If we are in non-pic codegen mode, we allow the address of a global (with
14391 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000014392 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014393 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000014394
Chris Lattner49921962009-05-08 18:23:14 +000014395 // Match either (GA), (GA+C), (GA+C1+C2), etc.
14396 while (1) {
14397 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
14398 Offset += GA->getOffset();
14399 break;
14400 } else if (Op.getOpcode() == ISD::ADD) {
14401 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14402 Offset += C->getZExtValue();
14403 Op = Op.getOperand(0);
14404 continue;
14405 }
14406 } else if (Op.getOpcode() == ISD::SUB) {
14407 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14408 Offset += -C->getZExtValue();
14409 Op = Op.getOperand(0);
14410 continue;
14411 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014412 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014413
Chris Lattner49921962009-05-08 18:23:14 +000014414 // Otherwise, this isn't something we can handle, reject it.
14415 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014416 }
Eric Christopherfd179292009-08-27 18:07:15 +000014417
Dan Gohman46510a72010-04-15 01:51:59 +000014418 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014419 // If we require an extra load to get this address, as in PIC mode, we
14420 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000014421 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
14422 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014423 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000014424
Devang Patel0d881da2010-07-06 22:08:15 +000014425 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
14426 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000014427 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014428 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014429 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014430
Gabor Greifba36cb52008-08-28 21:40:38 +000014431 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000014432 Ops.push_back(Result);
14433 return;
14434 }
Dale Johannesen1784d162010-06-25 21:55:36 +000014435 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014436}
14437
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014438std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000014439X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000014440 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000014441 // First, see if this is a constraint that directly corresponds to an LLVM
14442 // register class.
14443 if (Constraint.size() == 1) {
14444 // GCC Constraint Letters
14445 switch (Constraint[0]) {
14446 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000014447 // TODO: Slight differences here in allocation order and leaving
14448 // RIP in the class. Do they matter any more here than they do
14449 // in the normal allocation?
14450 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
14451 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014452 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014453 return std::make_pair(0U, X86::GR32RegisterClass);
14454 else if (VT == MVT::i16)
14455 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014456 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014457 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014458 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000014459 return std::make_pair(0U, X86::GR64RegisterClass);
14460 break;
14461 }
14462 // 32-bit fallthrough
14463 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014464 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014465 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
14466 else if (VT == MVT::i16)
14467 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014468 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014469 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
14470 else if (VT == MVT::i64)
14471 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
14472 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014473 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000014474 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014475 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000014476 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014477 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000014478 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000014479 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000014480 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000014481 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014482 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014483 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014484 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
14485 if (VT == MVT::i16)
14486 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
14487 if (VT == MVT::i32 || !Subtarget->is64Bit())
14488 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
14489 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014490 case 'f': // FP Stack registers.
14491 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
14492 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000014493 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014494 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014495 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014496 return std::make_pair(0U, X86::RFP64RegisterClass);
14497 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000014498 case 'y': // MMX_REGS if MMX allowed.
14499 if (!Subtarget->hasMMX()) break;
14500 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014501 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014502 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014503 // FALL THROUGH.
14504 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014505 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000014506
Owen Anderson825b72b2009-08-11 20:47:22 +000014507 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000014508 default: break;
14509 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014510 case MVT::f32:
14511 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000014512 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014513 case MVT::f64:
14514 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000014515 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014516 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014517 case MVT::v16i8:
14518 case MVT::v8i16:
14519 case MVT::v4i32:
14520 case MVT::v2i64:
14521 case MVT::v4f32:
14522 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000014523 return std::make_pair(0U, X86::VR128RegisterClass);
14524 }
Chris Lattnerad043e82007-04-09 05:11:28 +000014525 break;
14526 }
14527 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014528
Chris Lattnerf76d1802006-07-31 23:26:50 +000014529 // Use the default implementation in TargetLowering to convert the register
14530 // constraint into a member of a register class.
14531 std::pair<unsigned, const TargetRegisterClass*> Res;
14532 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000014533
14534 // Not found as a standard register?
14535 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014536 // Map st(0) -> st(7) -> ST0
14537 if (Constraint.size() == 7 && Constraint[0] == '{' &&
14538 tolower(Constraint[1]) == 's' &&
14539 tolower(Constraint[2]) == 't' &&
14540 Constraint[3] == '(' &&
14541 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
14542 Constraint[5] == ')' &&
14543 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000014544
Chris Lattner56d77c72009-09-13 22:41:48 +000014545 Res.first = X86::ST0+Constraint[4]-'0';
14546 Res.second = X86::RFP80RegisterClass;
14547 return Res;
14548 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014549
Chris Lattner56d77c72009-09-13 22:41:48 +000014550 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014551 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000014552 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000014553 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014554 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000014555 }
Chris Lattner56d77c72009-09-13 22:41:48 +000014556
14557 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014558 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014559 Res.first = X86::EFLAGS;
14560 Res.second = X86::CCRRegisterClass;
14561 return Res;
14562 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014563
Dale Johannesen330169f2008-11-13 21:52:36 +000014564 // 'A' means EAX + EDX.
14565 if (Constraint == "A") {
14566 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000014567 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014568 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000014569 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000014570 return Res;
14571 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014572
Chris Lattnerf76d1802006-07-31 23:26:50 +000014573 // Otherwise, check to see if this is a register class of the wrong value
14574 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
14575 // turn into {ax},{dx}.
14576 if (Res.second->hasType(VT))
14577 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014578
Chris Lattnerf76d1802006-07-31 23:26:50 +000014579 // All of the single-register GCC register classes map their values onto
14580 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
14581 // really want an 8-bit or 32-bit register, map to the appropriate register
14582 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000014583 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014584 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014585 unsigned DestReg = 0;
14586 switch (Res.first) {
14587 default: break;
14588 case X86::AX: DestReg = X86::AL; break;
14589 case X86::DX: DestReg = X86::DL; break;
14590 case X86::CX: DestReg = X86::CL; break;
14591 case X86::BX: DestReg = X86::BL; break;
14592 }
14593 if (DestReg) {
14594 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014595 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014596 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014597 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014598 unsigned DestReg = 0;
14599 switch (Res.first) {
14600 default: break;
14601 case X86::AX: DestReg = X86::EAX; break;
14602 case X86::DX: DestReg = X86::EDX; break;
14603 case X86::CX: DestReg = X86::ECX; break;
14604 case X86::BX: DestReg = X86::EBX; break;
14605 case X86::SI: DestReg = X86::ESI; break;
14606 case X86::DI: DestReg = X86::EDI; break;
14607 case X86::BP: DestReg = X86::EBP; break;
14608 case X86::SP: DestReg = X86::ESP; break;
14609 }
14610 if (DestReg) {
14611 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014612 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014613 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014614 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014615 unsigned DestReg = 0;
14616 switch (Res.first) {
14617 default: break;
14618 case X86::AX: DestReg = X86::RAX; break;
14619 case X86::DX: DestReg = X86::RDX; break;
14620 case X86::CX: DestReg = X86::RCX; break;
14621 case X86::BX: DestReg = X86::RBX; break;
14622 case X86::SI: DestReg = X86::RSI; break;
14623 case X86::DI: DestReg = X86::RDI; break;
14624 case X86::BP: DestReg = X86::RBP; break;
14625 case X86::SP: DestReg = X86::RSP; break;
14626 }
14627 if (DestReg) {
14628 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014629 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014630 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000014631 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000014632 } else if (Res.second == X86::FR32RegisterClass ||
14633 Res.second == X86::FR64RegisterClass ||
14634 Res.second == X86::VR128RegisterClass) {
14635 // Handle references to XMM physical registers that got mapped into the
14636 // wrong class. This can happen with constraints like {xmm0} where the
14637 // target independent register mapper will just pick the first match it can
14638 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000014639 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014640 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000014641 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014642 Res.second = X86::FR64RegisterClass;
14643 else if (X86::VR128RegisterClass->hasType(VT))
14644 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000014645 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014646
Chris Lattnerf76d1802006-07-31 23:26:50 +000014647 return Res;
14648}