blob: fc2d5ce64e8bcca6f456f0bec1b943de85c6267c [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000041#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000044#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000045#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000048#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000050#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000053#include "llvm/Support/raw_ostream.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000054#include "llvm/Target/TargetOptions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000055using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000056using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000057
Evan Chengb1712452010-01-27 06:25:16 +000058STATISTIC(NumTailCalls, "Number of tail calls");
59
Evan Cheng10e86422008-04-25 19:11:04 +000060// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000061static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000062 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000063
David Greenea5f26012011-02-07 19:36:54 +000064static SDValue Insert128BitVector(SDValue Result,
65 SDValue Vec,
66 SDValue Idx,
67 SelectionDAG &DAG,
68 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000069
David Greenea5f26012011-02-07 19:36:54 +000070static SDValue Extract128BitVector(SDValue Vec,
71 SDValue Idx,
72 SelectionDAG &DAG,
73 DebugLoc dl);
74
75/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
76/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000077/// simple subregister reference. Idx is an index in the 128 bits we
78/// want. It need not be aligned to a 128-bit bounday. That makes
79/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000080static SDValue Extract128BitVector(SDValue Vec,
81 SDValue Idx,
82 SelectionDAG &DAG,
83 DebugLoc dl) {
84 EVT VT = Vec.getValueType();
85 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000086 EVT ElVT = VT.getVectorElementType();
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000087 int Factor = VT.getSizeInBits()/128;
88 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
89 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000090
91 // Extract from UNDEF is UNDEF.
92 if (Vec.getOpcode() == ISD::UNDEF)
93 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
94
95 if (isa<ConstantSDNode>(Idx)) {
96 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
97
98 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
99 // we can match to VEXTRACTF128.
100 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
101
102 // This is the index of the first element of the 128-bit chunk
103 // we want.
104 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
105 * ElemsPerChunk);
106
107 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000108 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
109 VecIdx);
110
111 return Result;
112 }
113
114 return SDValue();
115}
116
117/// Generate a DAG to put 128-bits into a vector > 128 bits. This
118/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000119/// simple superregister reference. Idx is an index in the 128 bits
120/// we want. It need not be aligned to a 128-bit bounday. That makes
121/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000122static SDValue Insert128BitVector(SDValue Result,
123 SDValue Vec,
124 SDValue Idx,
125 SelectionDAG &DAG,
126 DebugLoc dl) {
127 if (isa<ConstantSDNode>(Idx)) {
128 EVT VT = Vec.getValueType();
129 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
130
131 EVT ElVT = VT.getVectorElementType();
David Greenea5f26012011-02-07 19:36:54 +0000132 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
David Greenea5f26012011-02-07 19:36:54 +0000133 EVT ResultVT = Result.getValueType();
134
135 // Insert the relevant 128 bits.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000136 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000137
138 // This is the index of the first element of the 128-bit chunk
139 // we want.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000140 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
David Greenea5f26012011-02-07 19:36:54 +0000141 * ElemsPerChunk);
142
143 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000144 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
145 VecIdx);
146 return Result;
147 }
148
149 return SDValue();
150}
151
Chris Lattnerf0144122009-07-28 03:13:23 +0000152static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000153 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
154 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000155
Evan Cheng2bffee22011-02-01 01:14:13 +0000156 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000157 if (is64Bit)
158 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000159 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000160 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000161
Evan Cheng203576a2011-07-20 19:50:42 +0000162 if (Subtarget->isTargetELF())
163 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000164 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000165 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000166 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000167}
168
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000169X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000170 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000171 Subtarget = &TM.getSubtarget<X86Subtarget>();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000172 X86ScalarSSEf64 = Subtarget->hasXMMInt();
173 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000174 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000175
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000176 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000177 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000178
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000179 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000180 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000181
182 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000183 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000184 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
185 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000186
Eric Christopherde5e1012011-03-11 01:05:58 +0000187 // For 64-bit since we have so many registers use the ILP scheduler, for
188 // 32-bit code use the register pressure specific scheduling.
189 if (Subtarget->is64Bit())
190 setSchedulingPreference(Sched::ILP);
191 else
192 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000193 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000194
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000195 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000196 // Setup Windows compiler runtime calls.
197 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000198 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000199 setLibcallName(RTLIB::SREM_I64, "_allrem");
200 setLibcallName(RTLIB::UREM_I64, "_aullrem");
201 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000202 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000203 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000204 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000205 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000206 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
207 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
208 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000209 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
210 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000211 }
212
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000213 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000214 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000215 setUseUnderscoreSetJmp(false);
216 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000217 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000218 // MS runtime is weird: it exports _setjmp, but longjmp!
219 setUseUnderscoreSetJmp(true);
220 setUseUnderscoreLongJmp(false);
221 } else {
222 setUseUnderscoreSetJmp(true);
223 setUseUnderscoreLongJmp(true);
224 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000225
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000226 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000228 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000230 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000232
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000234
Scott Michelfdc40a02009-02-17 22:15:04 +0000235 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000237 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000239 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
241 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000242
243 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
245 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
246 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
247 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
248 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
249 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000250
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000251 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
252 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000253 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
254 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
255 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000256
Evan Cheng25ab6902006-09-08 06:48:29 +0000257 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
259 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000260 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000261 // We have an algorithm for SSE2->double, and we turn this into a
262 // 64-bit FILD followed by conditional FADD for other targets.
263 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000264 // We have an algorithm for SSE2, and we turn this into a 64-bit
265 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000266 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000267 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000268
269 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
270 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
272 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000273
Devang Patel6a784892009-06-05 18:48:29 +0000274 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000275 // SSE has no i16 to fp conversion, only i32
276 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000278 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000280 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
282 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000283 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000284 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
286 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000287 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000288
Dale Johannesen73328d12007-09-19 23:55:34 +0000289 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
290 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
292 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000293
Evan Cheng02568ff2006-01-30 22:13:22 +0000294 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
295 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
297 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000298
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000299 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000301 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000303 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
305 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000306 }
307
308 // Handle FP_TO_UINT by promoting the destination to a larger signed
309 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
311 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
312 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000313
Evan Cheng25ab6902006-09-08 06:48:29 +0000314 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
316 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000317 } else if (!UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000318 // Since AVX is a superset of SSE3, only check for SSE here.
319 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 // Expand FP_TO_UINT into a select.
321 // FIXME: We would like to use a Custom expander here eventually to do
322 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000325 // With SSE3 we can use fisttpll to convert to a signed i64; without
326 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000328 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000329
Chris Lattner399610a2006-12-05 18:22:22 +0000330 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000331 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000332 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
333 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000334 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000335 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000336 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000337 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000338 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000339 }
Chris Lattner21f66852005-12-23 05:15:23 +0000340
Dan Gohmanb00ee212008-02-18 19:34:53 +0000341 // Scalar integer divide and remainder are lowered to use operations that
342 // produce two results, to match the available instructions. This exposes
343 // the two-result form to trivial CSE, which is able to combine x/y and x%y
344 // into a single instruction.
345 //
346 // Scalar integer multiply-high is also lowered to use two-result
347 // operations, to match the available instructions. However, plain multiply
348 // (low) operations are left as Legal, as there are single-result
349 // instructions for this in x86. Using the two-result multiply instructions
350 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000351 for (unsigned i = 0, e = 4; i != e; ++i) {
352 MVT VT = IntVTs[i];
353 setOperationAction(ISD::MULHS, VT, Expand);
354 setOperationAction(ISD::MULHU, VT, Expand);
355 setOperationAction(ISD::SDIV, VT, Expand);
356 setOperationAction(ISD::UDIV, VT, Expand);
357 setOperationAction(ISD::SREM, VT, Expand);
358 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000359
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000360 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000361 setOperationAction(ISD::ADDC, VT, Custom);
362 setOperationAction(ISD::ADDE, VT, Custom);
363 setOperationAction(ISD::SUBC, VT, Custom);
364 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000365 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000366
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
368 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
369 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
370 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000371 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
375 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
376 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
377 setOperationAction(ISD::FREM , MVT::f32 , Expand);
378 setOperationAction(ISD::FREM , MVT::f64 , Expand);
379 setOperationAction(ISD::FREM , MVT::f80 , Expand);
380 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000381
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
383 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000384 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
385 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
387 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000388 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000389 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
390 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000391 }
392
Benjamin Kramer1292c222010-12-04 20:32:23 +0000393 if (Subtarget->hasPOPCNT()) {
394 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
395 } else {
396 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
397 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
398 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
399 if (Subtarget->is64Bit())
400 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
401 }
402
Owen Anderson825b72b2009-08-11 20:47:22 +0000403 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
404 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000405
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000406 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000407 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000408 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000409 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000410 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
412 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
413 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
414 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
415 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000416 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000417 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
418 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
419 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
420 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000421 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000422 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000423 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000424 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000426
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000427 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000428 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
429 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
430 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
431 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000432 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
434 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000435 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000436 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
438 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
439 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
440 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000441 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000442 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000443 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
445 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
446 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000447 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000448 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
449 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
450 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000451 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000452
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000453 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000454 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000455
Eric Christopher9a9d2752010-07-22 02:48:34 +0000456 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000457 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000458
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000459 // On X86 and X86-64, atomic operations are lowered to locked instructions.
460 // Locked instructions, in turn, have implicit fence semantics (all memory
461 // operations are flushed before issuing the locked instruction, and they
462 // are not buffered), so we can fold away the common pattern of
463 // fence-atomic-fence.
464 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000465
Mon P Wang63307c32008-05-05 19:05:59 +0000466 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000467 for (unsigned i = 0, e = 4; i != e; ++i) {
468 MVT VT = IntVTs[i];
469 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
470 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000471 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000472 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000473
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000474 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000475 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000476 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
477 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
478 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
479 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
480 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
481 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
482 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000483 }
484
Eli Friedman43f51ae2011-08-26 21:21:21 +0000485 if (Subtarget->hasCmpxchg16b()) {
486 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
487 }
488
Evan Cheng3c992d22006-03-07 02:02:57 +0000489 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000490 if (!Subtarget->isTargetDarwin() &&
491 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000492 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000493 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000494 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000495
Owen Anderson825b72b2009-08-11 20:47:22 +0000496 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
497 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
498 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
499 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000500 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000501 setExceptionPointerRegister(X86::RAX);
502 setExceptionSelectorRegister(X86::RDX);
503 } else {
504 setExceptionPointerRegister(X86::EAX);
505 setExceptionSelectorRegister(X86::EDX);
506 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000507 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
508 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000509
Duncan Sands4a544a72011-09-06 13:37:06 +0000510 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
511 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000512
Owen Anderson825b72b2009-08-11 20:47:22 +0000513 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000514
Nate Begemanacc398c2006-01-25 18:21:52 +0000515 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 setOperationAction(ISD::VASTART , MVT::Other, Custom);
517 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000518 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 setOperationAction(ISD::VAARG , MVT::Other, Custom);
520 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000521 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 setOperationAction(ISD::VAARG , MVT::Other, Expand);
523 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000524 }
Evan Chengae642192007-03-02 23:16:35 +0000525
Owen Anderson825b72b2009-08-11 20:47:22 +0000526 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
527 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000528
529 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
530 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
531 MVT::i64 : MVT::i32, Custom);
532 else if (EnableSegmentedStacks)
533 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
534 MVT::i64 : MVT::i32, Custom);
535 else
536 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
537 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000538
Evan Chengc7ce29b2009-02-13 22:36:38 +0000539 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000540 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000541 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000542 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
543 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000544
Evan Cheng223547a2006-01-31 22:28:30 +0000545 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 setOperationAction(ISD::FABS , MVT::f64, Custom);
547 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000548
549 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 setOperationAction(ISD::FNEG , MVT::f64, Custom);
551 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000552
Evan Cheng68c47cb2007-01-05 07:55:56 +0000553 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
555 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000556
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000557 // Lower this to FGETSIGNx86 plus an AND.
558 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
559 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
560
Evan Chengd25e9e82006-02-02 00:28:23 +0000561 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::FSIN , MVT::f64, Expand);
563 setOperationAction(ISD::FCOS , MVT::f64, Expand);
564 setOperationAction(ISD::FSIN , MVT::f32, Expand);
565 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000566
Chris Lattnera54aa942006-01-29 06:26:08 +0000567 // Expand FP immediates into loads from the stack, except for the special
568 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000569 addLegalFPImmediate(APFloat(+0.0)); // xorpd
570 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000571 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000572 // Use SSE for f32, x87 for f64.
573 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000574 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
575 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000576
577 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000578 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000579
580 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000581 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000582
Owen Anderson825b72b2009-08-11 20:47:22 +0000583 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000584
585 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
587 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000588
589 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000590 setOperationAction(ISD::FSIN , MVT::f32, Expand);
591 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592
Nate Begemane1795842008-02-14 08:57:00 +0000593 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000594 addLegalFPImmediate(APFloat(+0.0f)); // xorps
595 addLegalFPImmediate(APFloat(+0.0)); // FLD0
596 addLegalFPImmediate(APFloat(+1.0)); // FLD1
597 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
598 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
599
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000600 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
602 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000603 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000604 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000605 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000606 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000607 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
608 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000609
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
611 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
612 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
613 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000614
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000615 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
617 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000618 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000619 addLegalFPImmediate(APFloat(+0.0)); // FLD0
620 addLegalFPImmediate(APFloat(+1.0)); // FLD1
621 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
622 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000623 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
624 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
625 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
626 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000627 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000628
Cameron Zwarich33390842011-07-08 21:39:21 +0000629 // We don't support FMA.
630 setOperationAction(ISD::FMA, MVT::f64, Expand);
631 setOperationAction(ISD::FMA, MVT::f32, Expand);
632
Dale Johannesen59a58732007-08-05 18:49:15 +0000633 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000634 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
636 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
637 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000638 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000639 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000640 addLegalFPImmediate(TmpFlt); // FLD0
641 TmpFlt.changeSign();
642 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000643
644 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000645 APFloat TmpFlt2(+1.0);
646 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
647 &ignored);
648 addLegalFPImmediate(TmpFlt2); // FLD1
649 TmpFlt2.changeSign();
650 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
651 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000652
Evan Chengc7ce29b2009-02-13 22:36:38 +0000653 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
655 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000656 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000657
658 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000659 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000660
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000661 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
663 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
664 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000665
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::FLOG, MVT::f80, Expand);
667 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
668 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
669 setOperationAction(ISD::FEXP, MVT::f80, Expand);
670 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000671
Mon P Wangf007a8b2008-11-06 05:31:54 +0000672 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000673 // (for widening) or expand (for scalarization). Then we will selectively
674 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
676 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
677 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
678 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
679 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
680 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
681 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
682 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
683 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
684 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
685 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
686 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000692 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000693 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
694 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000695 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000716 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000717 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000726 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000727 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000731 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000732 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
733 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
734 setTruncStoreAction((MVT::SimpleValueType)VT,
735 (MVT::SimpleValueType)InnerVT, Expand);
736 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
737 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
738 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000739 }
740
Evan Chengc7ce29b2009-02-13 22:36:38 +0000741 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
742 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000743 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000744 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000745 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000746 }
747
Dale Johannesen0488fb62010-09-30 23:57:10 +0000748 // MMX-sized vectors (other than x86mmx) are expected to be expanded
749 // into smaller operations.
750 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
751 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
752 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
753 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
754 setOperationAction(ISD::AND, MVT::v8i8, Expand);
755 setOperationAction(ISD::AND, MVT::v4i16, Expand);
756 setOperationAction(ISD::AND, MVT::v2i32, Expand);
757 setOperationAction(ISD::AND, MVT::v1i64, Expand);
758 setOperationAction(ISD::OR, MVT::v8i8, Expand);
759 setOperationAction(ISD::OR, MVT::v4i16, Expand);
760 setOperationAction(ISD::OR, MVT::v2i32, Expand);
761 setOperationAction(ISD::OR, MVT::v1i64, Expand);
762 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
763 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
764 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
765 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
766 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
767 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
768 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
769 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
770 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
771 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
772 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
773 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
774 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000775 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
776 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
777 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
778 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000779
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000780 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000781 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000782
Owen Anderson825b72b2009-08-11 20:47:22 +0000783 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
784 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
785 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
786 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
787 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
788 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
789 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
790 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
791 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
792 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
793 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000794 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000795 }
796
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000797 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000798 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000799
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000800 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
801 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000802 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
803 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
804 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
805 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000806
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
808 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
809 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
810 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
811 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
812 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
813 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
814 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
815 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
816 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
817 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
818 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
819 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
820 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
821 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
822 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000823
Nadav Rotem354efd82011-09-18 14:57:03 +0000824 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000825 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
826 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
827 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000828
Owen Anderson825b72b2009-08-11 20:47:22 +0000829 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
830 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
831 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
832 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
833 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000834
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000835 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
836 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
837 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
838 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
839 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
840
Evan Cheng2c3ae372006-04-12 21:21:57 +0000841 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
843 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000844 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000845 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000846 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000847 // Do not attempt to custom lower non-128-bit vectors
848 if (!VT.is128BitVector())
849 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000850 setOperationAction(ISD::BUILD_VECTOR,
851 VT.getSimpleVT().SimpleTy, Custom);
852 setOperationAction(ISD::VECTOR_SHUFFLE,
853 VT.getSimpleVT().SimpleTy, Custom);
854 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
855 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000856 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000857
Owen Anderson825b72b2009-08-11 20:47:22 +0000858 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
859 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
860 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
861 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
862 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
863 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000864
Nate Begemancdd1eec2008-02-12 22:51:28 +0000865 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000866 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
867 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000868 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000869
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000870 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
872 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000873 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000874
875 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000876 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000877 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000878
Owen Andersond6662ad2009-08-10 20:46:15 +0000879 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000881 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000882 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000883 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000885 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000886 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000887 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000889 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000890
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000892
Evan Cheng2c3ae372006-04-12 21:21:57 +0000893 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000894 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
895 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
896 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
897 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000898
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
900 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000901 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000902
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000903 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000904 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
905 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
906 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
907 setOperationAction(ISD::FRINT, MVT::f32, Legal);
908 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
909 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
910 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
911 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
912 setOperationAction(ISD::FRINT, MVT::f64, Legal);
913 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
914
Nate Begeman14d12ca2008-02-11 04:19:36 +0000915 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000916 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000917
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000918 // Can turn SHL into an integer multiply.
919 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000920 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000921
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000922 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
923 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
924 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
925 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
926 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000927
Nate Begeman14d12ca2008-02-11 04:19:36 +0000928 // i8 and i16 vectors are custom , because the source register and source
929 // source memory operand types are not the same width. f32 vectors are
930 // custom since the immediate controlling the insert encodes additional
931 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000932 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
933 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
934 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
935 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000936
Owen Anderson825b72b2009-08-11 20:47:22 +0000937 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
938 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
939 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
940 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000941
942 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000943 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
944 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000945 }
946 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000947
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000948 if (Subtarget->hasXMMInt()) {
Nadav Rotem43012222011-05-11 08:12:09 +0000949 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
950 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
951 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000952 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000953
954 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
955 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
956 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
957
958 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
959 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
960 }
961
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000962 if (Subtarget->hasSSE42() || Subtarget->hasAVX())
Duncan Sands28b77e92011-09-06 19:07:46 +0000963 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000964
David Greene9b9838d2009-06-29 16:47:10 +0000965 if (!UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000966 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
967 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
968 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
969 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
970 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
971 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000972
Owen Anderson825b72b2009-08-11 20:47:22 +0000973 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000974 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
975 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000976
Owen Anderson825b72b2009-08-11 20:47:22 +0000977 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
978 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
979 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
980 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
981 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
982 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000983
Owen Anderson825b72b2009-08-11 20:47:22 +0000984 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
985 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
986 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
987 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
988 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
989 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000990
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000991 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
992 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +0000993 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000994
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +0000995 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
996 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
997 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
998 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
999 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
1000 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
1001
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001002 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1003 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1004 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1005 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1006
1007 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1008 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1009 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1010 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1011
1012 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
1013 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
1014
Duncan Sands28b77e92011-09-06 19:07:46 +00001015 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1016 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1017 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1018 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001019
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001020 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1021 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1022 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1023
Nadav Rotemfbad25e2011-09-11 15:02:23 +00001024 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1025 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1026 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1027 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001028
Craig Topper13894fa2011-08-24 06:14:18 +00001029 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1030 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1031 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1032 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1033
1034 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1035 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1036 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1037 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1038
1039 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1040 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1041 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1042 // Don't lower v32i8 because there is no 128-bit byte mul
1043
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001044 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001045 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001046 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1047 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1048 EVT VT = SVT;
1049
1050 // Extract subvector is special because the value type
1051 // (result) is 128-bit but the source is 256-bit wide.
1052 if (VT.is128BitVector())
1053 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1054
1055 // Do not attempt to custom lower other non-256-bit vectors
1056 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001057 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001058
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001059 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1060 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1061 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1062 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001063 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001064 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001065 }
1066
David Greene54d8eba2011-01-27 22:38:56 +00001067 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001068 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1069 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1070 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001071
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001072 // Do not attempt to promote non-256-bit vectors
1073 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001074 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001075
1076 setOperationAction(ISD::AND, SVT, Promote);
1077 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1078 setOperationAction(ISD::OR, SVT, Promote);
1079 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1080 setOperationAction(ISD::XOR, SVT, Promote);
1081 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1082 setOperationAction(ISD::LOAD, SVT, Promote);
1083 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1084 setOperationAction(ISD::SELECT, SVT, Promote);
1085 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001086 }
David Greene9b9838d2009-06-29 16:47:10 +00001087 }
1088
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001089 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1090 // of this type with custom code.
1091 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1092 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1093 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1094 }
1095
Evan Cheng6be2c582006-04-05 23:38:46 +00001096 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001097 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001098
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001099
Eli Friedman962f5492010-06-02 19:35:46 +00001100 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1101 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001102 //
Eli Friedman962f5492010-06-02 19:35:46 +00001103 // FIXME: We really should do custom legalization for addition and
1104 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1105 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001106 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1107 // Add/Sub/Mul with overflow operations are custom lowered.
1108 MVT VT = IntVTs[i];
1109 setOperationAction(ISD::SADDO, VT, Custom);
1110 setOperationAction(ISD::UADDO, VT, Custom);
1111 setOperationAction(ISD::SSUBO, VT, Custom);
1112 setOperationAction(ISD::USUBO, VT, Custom);
1113 setOperationAction(ISD::SMULO, VT, Custom);
1114 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001115 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001116
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001117 // There are no 8-bit 3-address imul/mul instructions
1118 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1119 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001120
Evan Chengd54f2d52009-03-31 19:38:51 +00001121 if (!Subtarget->is64Bit()) {
1122 // These libcalls are not available in 32-bit.
1123 setLibcallName(RTLIB::SHL_I128, 0);
1124 setLibcallName(RTLIB::SRL_I128, 0);
1125 setLibcallName(RTLIB::SRA_I128, 0);
1126 }
1127
Evan Cheng206ee9d2006-07-07 08:33:52 +00001128 // We have target-specific dag combine patterns for the following nodes:
1129 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001130 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001131 setTargetDAGCombine(ISD::BUILD_VECTOR);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001132 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001133 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001134 setTargetDAGCombine(ISD::SHL);
1135 setTargetDAGCombine(ISD::SRA);
1136 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001137 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001138 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001139 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001140 setTargetDAGCombine(ISD::FADD);
1141 setTargetDAGCombine(ISD::FSUB);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001142 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001143 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001144 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001145 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001146 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001147 if (Subtarget->is64Bit())
1148 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001149
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001150 computeRegisterProperties();
1151
Evan Cheng05219282011-01-06 06:52:41 +00001152 // On Darwin, -Os means optimize for size without hurting performance,
1153 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001154 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001155 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001156 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001157 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1158 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1159 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001160 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001161 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001162
1163 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001164}
1165
Scott Michel5b8f82e2008-03-10 15:42:14 +00001166
Duncan Sands28b77e92011-09-06 19:07:46 +00001167EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1168 if (!VT.isVector()) return MVT::i8;
1169 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001170}
1171
1172
Evan Cheng29286502008-01-23 23:17:41 +00001173/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1174/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001175static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001176 if (MaxAlign == 16)
1177 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001178 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001179 if (VTy->getBitWidth() == 128)
1180 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001181 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001182 unsigned EltAlign = 0;
1183 getMaxByValAlign(ATy->getElementType(), EltAlign);
1184 if (EltAlign > MaxAlign)
1185 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001186 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001187 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1188 unsigned EltAlign = 0;
1189 getMaxByValAlign(STy->getElementType(i), EltAlign);
1190 if (EltAlign > MaxAlign)
1191 MaxAlign = EltAlign;
1192 if (MaxAlign == 16)
1193 break;
1194 }
1195 }
1196 return;
1197}
1198
1199/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1200/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001201/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1202/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001203unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001204 if (Subtarget->is64Bit()) {
1205 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001206 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001207 if (TyAlign > 8)
1208 return TyAlign;
1209 return 8;
1210 }
1211
Evan Cheng29286502008-01-23 23:17:41 +00001212 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001213 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001214 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001215 return Align;
1216}
Chris Lattner2b02a442007-02-25 08:29:00 +00001217
Evan Chengf0df0312008-05-15 08:39:06 +00001218/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001219/// and store operations as a result of memset, memcpy, and memmove
1220/// lowering. If DstAlign is zero that means it's safe to destination
1221/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1222/// means there isn't a need to check it against alignment requirement,
1223/// probably because the source does not need to be loaded. If
1224/// 'NonScalarIntSafe' is true, that means it's safe to return a
1225/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1226/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1227/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001228/// It returns EVT::Other if the type should be determined using generic
1229/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001230EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001231X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1232 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001233 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001234 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001235 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001236 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1237 // linux. This is because the stack realignment code can't handle certain
1238 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001239 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001240 if (NonScalarIntSafe &&
1241 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001242 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001243 (Subtarget->isUnalignedMemAccessFast() ||
1244 ((DstAlign == 0 || DstAlign >= 16) &&
1245 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001246 Subtarget->getStackAlignment() >= 16) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001247 if (Subtarget->hasAVX() &&
1248 Subtarget->getStackAlignment() >= 32)
1249 return MVT::v8f32;
1250 if (Subtarget->hasXMMInt())
Evan Cheng255f20f2010-04-01 06:04:33 +00001251 return MVT::v4i32;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001252 if (Subtarget->hasXMM())
Evan Cheng255f20f2010-04-01 06:04:33 +00001253 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001254 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001255 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001256 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001257 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001258 // Do not use f64 to lower memcpy if source is string constant. It's
1259 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001260 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001261 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001262 }
Evan Chengf0df0312008-05-15 08:39:06 +00001263 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001264 return MVT::i64;
1265 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001266}
1267
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001268/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1269/// current function. The returned value is a member of the
1270/// MachineJumpTableInfo::JTEntryKind enum.
1271unsigned X86TargetLowering::getJumpTableEncoding() const {
1272 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1273 // symbol.
1274 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1275 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001276 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001277
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001278 // Otherwise, use the normal jump table encoding heuristics.
1279 return TargetLowering::getJumpTableEncoding();
1280}
1281
Chris Lattnerc64daab2010-01-26 05:02:42 +00001282const MCExpr *
1283X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1284 const MachineBasicBlock *MBB,
1285 unsigned uid,MCContext &Ctx) const{
1286 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1287 Subtarget->isPICStyleGOT());
1288 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1289 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001290 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1291 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001292}
1293
Evan Chengcc415862007-11-09 01:32:10 +00001294/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1295/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001296SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001297 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001298 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001299 // This doesn't have DebugLoc associated with it, but is not really the
1300 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001301 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001302 return Table;
1303}
1304
Chris Lattner589c6f62010-01-26 06:28:43 +00001305/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1306/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1307/// MCExpr.
1308const MCExpr *X86TargetLowering::
1309getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1310 MCContext &Ctx) const {
1311 // X86-64 uses RIP relative addressing based on the jump table label.
1312 if (Subtarget->isPICStyleRIPRel())
1313 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1314
1315 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001316 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001317}
1318
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001319// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001320std::pair<const TargetRegisterClass*, uint8_t>
1321X86TargetLowering::findRepresentativeClass(EVT VT) const{
1322 const TargetRegisterClass *RRC = 0;
1323 uint8_t Cost = 1;
1324 switch (VT.getSimpleVT().SimpleTy) {
1325 default:
1326 return TargetLowering::findRepresentativeClass(VT);
1327 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1328 RRC = (Subtarget->is64Bit()
1329 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1330 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001331 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001332 RRC = X86::VR64RegisterClass;
1333 break;
1334 case MVT::f32: case MVT::f64:
1335 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1336 case MVT::v4f32: case MVT::v2f64:
1337 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1338 case MVT::v4f64:
1339 RRC = X86::VR128RegisterClass;
1340 break;
1341 }
1342 return std::make_pair(RRC, Cost);
1343}
1344
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001345bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1346 unsigned &Offset) const {
1347 if (!Subtarget->isTargetLinux())
1348 return false;
1349
1350 if (Subtarget->is64Bit()) {
1351 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1352 Offset = 0x28;
1353 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1354 AddressSpace = 256;
1355 else
1356 AddressSpace = 257;
1357 } else {
1358 // %gs:0x14 on i386
1359 Offset = 0x14;
1360 AddressSpace = 256;
1361 }
1362 return true;
1363}
1364
1365
Chris Lattner2b02a442007-02-25 08:29:00 +00001366//===----------------------------------------------------------------------===//
1367// Return Value Calling Convention Implementation
1368//===----------------------------------------------------------------------===//
1369
Chris Lattner59ed56b2007-02-28 04:55:35 +00001370#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001371
Michael J. Spencerec38de22010-10-10 22:04:20 +00001372bool
Eric Christopher471e4222011-06-08 23:55:35 +00001373X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1374 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001375 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001376 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001377 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001378 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001379 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001380 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001381}
1382
Dan Gohman98ca4f22009-08-05 01:29:28 +00001383SDValue
1384X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001385 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001386 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001387 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001388 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001389 MachineFunction &MF = DAG.getMachineFunction();
1390 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001391
Chris Lattner9774c912007-02-27 05:28:59 +00001392 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001393 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001394 RVLocs, *DAG.getContext());
1395 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001396
Evan Chengdcea1632010-02-04 02:40:39 +00001397 // Add the regs to the liveout set for the function.
1398 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1399 for (unsigned i = 0; i != RVLocs.size(); ++i)
1400 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1401 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001402
Dan Gohman475871a2008-07-27 21:46:04 +00001403 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001404
Dan Gohman475871a2008-07-27 21:46:04 +00001405 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001406 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1407 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001408 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1409 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001410
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001411 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001412 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1413 CCValAssign &VA = RVLocs[i];
1414 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001415 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001416 EVT ValVT = ValToCopy.getValueType();
1417
Dale Johannesenc4510512010-09-24 19:05:48 +00001418 // If this is x86-64, and we disabled SSE, we can't return FP values,
1419 // or SSE or MMX vectors.
1420 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1421 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001422 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001423 report_fatal_error("SSE register return with SSE disabled");
1424 }
1425 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1426 // llvm-gcc has never done it right and no one has noticed, so this
1427 // should be OK for now.
1428 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001429 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001430 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001431
Chris Lattner447ff682008-03-11 03:23:40 +00001432 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1433 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001434 if (VA.getLocReg() == X86::ST0 ||
1435 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001436 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1437 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001438 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001439 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001440 RetOps.push_back(ValToCopy);
1441 // Don't emit a copytoreg.
1442 continue;
1443 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001444
Evan Cheng242b38b2009-02-23 09:03:22 +00001445 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1446 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001447 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001448 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001449 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001450 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001451 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1452 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001453 // If we don't have SSE2 available, convert to v4f32 so the generated
1454 // register is legal.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001455 if (!Subtarget->hasXMMInt())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001456 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001457 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001458 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001459 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001460
Dale Johannesendd64c412009-02-04 00:33:20 +00001461 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001462 Flag = Chain.getValue(1);
1463 }
Dan Gohman61a92132008-04-21 23:59:07 +00001464
1465 // The x86-64 ABI for returning structs by value requires that we copy
1466 // the sret argument into %rax for the return. We saved the argument into
1467 // a virtual register in the entry block, so now we copy the value out
1468 // and into %rax.
1469 if (Subtarget->is64Bit() &&
1470 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1471 MachineFunction &MF = DAG.getMachineFunction();
1472 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1473 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001474 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001475 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001476 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001477
Dale Johannesendd64c412009-02-04 00:33:20 +00001478 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001479 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001480
1481 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001482 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001483 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001484
Chris Lattner447ff682008-03-11 03:23:40 +00001485 RetOps[0] = Chain; // Update chain.
1486
1487 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001488 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001489 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001490
1491 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001492 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001493}
1494
Evan Cheng3d2125c2010-11-30 23:55:39 +00001495bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1496 if (N->getNumValues() != 1)
1497 return false;
1498 if (!N->hasNUsesOfValue(1, 0))
1499 return false;
1500
1501 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001502 if (Copy->getOpcode() != ISD::CopyToReg &&
1503 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001504 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001505
1506 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001507 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001508 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001509 if (UI->getOpcode() != X86ISD::RET_FLAG)
1510 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001511 HasRet = true;
1512 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001513
Evan Cheng1bf891a2010-12-01 22:59:46 +00001514 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001515}
1516
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001517EVT
1518X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001519 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001520 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001521 // TODO: Is this also valid on 32-bit?
1522 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001523 ReturnMVT = MVT::i8;
1524 else
1525 ReturnMVT = MVT::i32;
1526
1527 EVT MinVT = getRegisterType(Context, ReturnMVT);
1528 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001529}
1530
Dan Gohman98ca4f22009-08-05 01:29:28 +00001531/// LowerCallResult - Lower the result values of a call into the
1532/// appropriate copies out of appropriate physical registers.
1533///
1534SDValue
1535X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001536 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001537 const SmallVectorImpl<ISD::InputArg> &Ins,
1538 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001539 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001540
Chris Lattnere32bbf62007-02-28 07:09:55 +00001541 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001542 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001543 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001544 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1545 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001546 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001547
Chris Lattner3085e152007-02-25 08:59:22 +00001548 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001549 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001550 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001551 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001552
Torok Edwin3f142c32009-02-01 18:15:56 +00001553 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001554 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001555 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001556 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001557 }
1558
Evan Cheng79fb3b42009-02-20 20:43:02 +00001559 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001560
1561 // If this is a call to a function that returns an fp value on the floating
1562 // point stack, we must guarantee the the value is popped from the stack, so
1563 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001564 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001565 // instead.
1566 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1567 // If we prefer to use the value in xmm registers, copy it out as f80 and
1568 // use a truncate to move it from fp stack reg to xmm reg.
1569 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001570 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001571 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1572 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001573 Val = Chain.getValue(0);
1574
1575 // Round the f80 to the right size, which also moves it to the appropriate
1576 // xmm register.
1577 if (CopyVT != VA.getValVT())
1578 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1579 // This truncation won't change the value.
1580 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001581 } else {
1582 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1583 CopyVT, InFlag).getValue(1);
1584 Val = Chain.getValue(0);
1585 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001586 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001587 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001588 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001589
Dan Gohman98ca4f22009-08-05 01:29:28 +00001590 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001591}
1592
1593
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001594//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001595// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001596//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001597// StdCall calling convention seems to be standard for many Windows' API
1598// routines and around. It differs from C calling convention just a little:
1599// callee should clean up the stack, not caller. Symbols should be also
1600// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001601// For info on fast calling convention see Fast Calling Convention (tail call)
1602// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001603
Dan Gohman98ca4f22009-08-05 01:29:28 +00001604/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001605/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001606static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1607 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001608 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001609
Dan Gohman98ca4f22009-08-05 01:29:28 +00001610 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001611}
1612
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001613/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001614/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001615static bool
1616ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1617 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001618 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001619
Dan Gohman98ca4f22009-08-05 01:29:28 +00001620 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001621}
1622
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001623/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1624/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001625/// the specific parameter attribute. The copy will be passed as a byval
1626/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001627static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001628CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001629 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1630 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001631 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001632
Dale Johannesendd64c412009-02-04 00:33:20 +00001633 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001634 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001635 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001636}
1637
Chris Lattner29689432010-03-11 00:22:57 +00001638/// IsTailCallConvention - Return true if the calling convention is one that
1639/// supports tail call optimization.
1640static bool IsTailCallConvention(CallingConv::ID CC) {
1641 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1642}
1643
Evan Cheng485fafc2011-03-21 01:19:09 +00001644bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1645 if (!CI->isTailCall())
1646 return false;
1647
1648 CallSite CS(CI);
1649 CallingConv::ID CalleeCC = CS.getCallingConv();
1650 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1651 return false;
1652
1653 return true;
1654}
1655
Evan Cheng0c439eb2010-01-27 00:07:07 +00001656/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1657/// a tailcall target by changing its ABI.
1658static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001659 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001660}
1661
Dan Gohman98ca4f22009-08-05 01:29:28 +00001662SDValue
1663X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001664 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001665 const SmallVectorImpl<ISD::InputArg> &Ins,
1666 DebugLoc dl, SelectionDAG &DAG,
1667 const CCValAssign &VA,
1668 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001669 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001670 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001671 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001672 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001673 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001674 EVT ValVT;
1675
1676 // If value is passed by pointer we have address passed instead of the value
1677 // itself.
1678 if (VA.getLocInfo() == CCValAssign::Indirect)
1679 ValVT = VA.getLocVT();
1680 else
1681 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001682
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001683 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001684 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001685 // In case of tail call optimization mark all arguments mutable. Since they
1686 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001687 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001688 unsigned Bytes = Flags.getByValSize();
1689 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1690 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001691 return DAG.getFrameIndex(FI, getPointerTy());
1692 } else {
1693 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001694 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001695 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1696 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001697 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001698 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001699 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001700}
1701
Dan Gohman475871a2008-07-27 21:46:04 +00001702SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001703X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001704 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001705 bool isVarArg,
1706 const SmallVectorImpl<ISD::InputArg> &Ins,
1707 DebugLoc dl,
1708 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001709 SmallVectorImpl<SDValue> &InVals)
1710 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001711 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001712 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001713
Gordon Henriksen86737662008-01-05 16:56:59 +00001714 const Function* Fn = MF.getFunction();
1715 if (Fn->hasExternalLinkage() &&
1716 Subtarget->isTargetCygMing() &&
1717 Fn->getName() == "main")
1718 FuncInfo->setForceFramePointer(true);
1719
Evan Cheng1bc78042006-04-26 01:20:17 +00001720 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001721 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001722 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001723
Chris Lattner29689432010-03-11 00:22:57 +00001724 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1725 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001726
Chris Lattner638402b2007-02-28 07:00:42 +00001727 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001728 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001729 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001730 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001731
1732 // Allocate shadow area for Win64
1733 if (IsWin64) {
1734 CCInfo.AllocateStack(32, 8);
1735 }
1736
Duncan Sands45907662010-10-31 13:21:44 +00001737 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001738
Chris Lattnerf39f7712007-02-28 05:46:49 +00001739 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001740 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001741 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1742 CCValAssign &VA = ArgLocs[i];
1743 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1744 // places.
1745 assert(VA.getValNo() != LastVal &&
1746 "Don't support value assigned to multiple locs yet");
1747 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001748
Chris Lattnerf39f7712007-02-28 05:46:49 +00001749 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001750 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001751 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001752 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001753 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001754 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001755 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001756 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001757 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001758 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001759 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001760 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1761 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001762 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001763 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001764 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001765 RC = X86::VR64RegisterClass;
1766 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001767 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001768
Devang Patel68e6bee2011-02-21 23:21:26 +00001769 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001770 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001771
Chris Lattnerf39f7712007-02-28 05:46:49 +00001772 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1773 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1774 // right size.
1775 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001776 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001777 DAG.getValueType(VA.getValVT()));
1778 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001779 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001780 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001781 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001782 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001783
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001784 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001785 // Handle MMX values passed in XMM regs.
1786 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001787 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1788 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001789 } else
1790 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001791 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001792 } else {
1793 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001794 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001795 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001796
1797 // If value is passed via pointer - do a load.
1798 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001799 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1800 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001801
Dan Gohman98ca4f22009-08-05 01:29:28 +00001802 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001803 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001804
Dan Gohman61a92132008-04-21 23:59:07 +00001805 // The x86-64 ABI for returning structs by value requires that we copy
1806 // the sret argument into %rax for the return. Save the argument into
1807 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001808 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001809 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1810 unsigned Reg = FuncInfo->getSRetReturnReg();
1811 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001812 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001813 FuncInfo->setSRetReturnReg(Reg);
1814 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001815 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001816 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001817 }
1818
Chris Lattnerf39f7712007-02-28 05:46:49 +00001819 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001820 // Align stack specially for tail calls.
1821 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001822 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001823
Evan Cheng1bc78042006-04-26 01:20:17 +00001824 // If the function takes variable number of arguments, make a frame index for
1825 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001826 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001827 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1828 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001829 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001830 }
1831 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001832 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1833
1834 // FIXME: We should really autogenerate these arrays
1835 static const unsigned GPR64ArgRegsWin64[] = {
1836 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001837 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001838 static const unsigned GPR64ArgRegs64Bit[] = {
1839 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1840 };
1841 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001842 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1843 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1844 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001845 const unsigned *GPR64ArgRegs;
1846 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001847
1848 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001849 // The XMM registers which might contain var arg parameters are shadowed
1850 // in their paired GPR. So we only need to save the GPR to their home
1851 // slots.
1852 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001853 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001854 } else {
1855 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1856 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001857
1858 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001859 }
1860 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1861 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001862
Devang Patel578efa92009-06-05 21:57:13 +00001863 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001864 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001865 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001866 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001867 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001868 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001869 // Kernel mode asks for SSE to be disabled, so don't push them
1870 // on the stack.
1871 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001872
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001873 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001874 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001875 // Get to the caller-allocated home save location. Add 8 to account
1876 // for the return address.
1877 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001878 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001879 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001880 // Fixup to set vararg frame on shadow area (4 x i64).
1881 if (NumIntRegs < 4)
1882 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001883 } else {
1884 // For X86-64, if there are vararg parameters that are passed via
1885 // registers, then we must store them to their spots on the stack so they
1886 // may be loaded by deferencing the result of va_next.
1887 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1888 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1889 FuncInfo->setRegSaveFrameIndex(
1890 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001891 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001892 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001893
Gordon Henriksen86737662008-01-05 16:56:59 +00001894 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001895 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001896 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1897 getPointerTy());
1898 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001899 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001900 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1901 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001902 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001903 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001904 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001905 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001906 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001907 MachinePointerInfo::getFixedStack(
1908 FuncInfo->getRegSaveFrameIndex(), Offset),
1909 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001910 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001911 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001912 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001913
Dan Gohmanface41a2009-08-16 21:24:25 +00001914 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1915 // Now store the XMM (fp + vector) parameter registers.
1916 SmallVector<SDValue, 11> SaveXMMOps;
1917 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001918
Devang Patel68e6bee2011-02-21 23:21:26 +00001919 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001920 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1921 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001922
Dan Gohman1e93df62010-04-17 14:41:14 +00001923 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1924 FuncInfo->getRegSaveFrameIndex()));
1925 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1926 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001927
Dan Gohmanface41a2009-08-16 21:24:25 +00001928 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001929 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001930 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001931 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1932 SaveXMMOps.push_back(Val);
1933 }
1934 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1935 MVT::Other,
1936 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001937 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001938
1939 if (!MemOps.empty())
1940 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1941 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001942 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001943 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001944
Gordon Henriksen86737662008-01-05 16:56:59 +00001945 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001946 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001947 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001948 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001949 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001950 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001951 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001952 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001953 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001954
Gordon Henriksen86737662008-01-05 16:56:59 +00001955 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001956 // RegSaveFrameIndex is X86-64 only.
1957 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001958 if (CallConv == CallingConv::X86_FastCall ||
1959 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001960 // fastcc functions can't have varargs.
1961 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001962 }
Evan Cheng25caf632006-05-23 21:06:34 +00001963
Rafael Espindola76927d752011-08-30 19:39:58 +00001964 FuncInfo->setArgumentStackSize(StackSize);
1965
Dan Gohman98ca4f22009-08-05 01:29:28 +00001966 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001967}
1968
Dan Gohman475871a2008-07-27 21:46:04 +00001969SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001970X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1971 SDValue StackPtr, SDValue Arg,
1972 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001973 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001974 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001975 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001976 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001977 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001978 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001979 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001980
1981 return DAG.getStore(Chain, dl, Arg, PtrOff,
1982 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001983 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001984}
1985
Bill Wendling64e87322009-01-16 19:25:27 +00001986/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001987/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001988SDValue
1989X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001990 SDValue &OutRetAddr, SDValue Chain,
1991 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001992 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001993 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001994 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001995 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001996
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001997 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001998 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1999 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002000 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002001}
2002
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002003/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002004/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002005static SDValue
2006EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002007 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002008 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002009 // Store the return address to the appropriate stack slot.
2010 if (!FPDiff) return Chain;
2011 // Calculate the new stack slot for the return address.
2012 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002013 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002014 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002015 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002016 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002017 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002018 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002019 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002020 return Chain;
2021}
2022
Dan Gohman98ca4f22009-08-05 01:29:28 +00002023SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002024X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002025 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002026 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002027 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002028 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002029 const SmallVectorImpl<ISD::InputArg> &Ins,
2030 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002031 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002032 MachineFunction &MF = DAG.getMachineFunction();
2033 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002034 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002035 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002036 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002037
Evan Cheng5f941932010-02-05 02:21:12 +00002038 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002039 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002040 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2041 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002042 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002043
2044 // Sibcalls are automatically detected tailcalls which do not require
2045 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002046 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002047 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002048
2049 if (isTailCall)
2050 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002051 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002052
Chris Lattner29689432010-03-11 00:22:57 +00002053 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2054 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002055
Chris Lattner638402b2007-02-28 07:00:42 +00002056 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002057 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002058 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002059 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002060
2061 // Allocate shadow area for Win64
2062 if (IsWin64) {
2063 CCInfo.AllocateStack(32, 8);
2064 }
2065
Duncan Sands45907662010-10-31 13:21:44 +00002066 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002067
Chris Lattner423c5f42007-02-28 05:31:48 +00002068 // Get a count of how many bytes are to be pushed on the stack.
2069 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002070 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002071 // This is a sibcall. The memory operands are available in caller's
2072 // own caller's stack.
2073 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002074 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002075 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002076
Gordon Henriksen86737662008-01-05 16:56:59 +00002077 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002078 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002079 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002080 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002081 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2082 FPDiff = NumBytesCallerPushed - NumBytes;
2083
2084 // Set the delta of movement of the returnaddr stackslot.
2085 // But only set if delta is greater than previous delta.
2086 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2087 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2088 }
2089
Evan Chengf22f9b32010-02-06 03:28:46 +00002090 if (!IsSibcall)
2091 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002092
Dan Gohman475871a2008-07-27 21:46:04 +00002093 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002094 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002095 if (isTailCall && FPDiff)
2096 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2097 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002098
Dan Gohman475871a2008-07-27 21:46:04 +00002099 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2100 SmallVector<SDValue, 8> MemOpChains;
2101 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002102
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002103 // Walk the register/memloc assignments, inserting copies/loads. In the case
2104 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002105 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2106 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002107 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002108 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002109 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002110 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002111
Chris Lattner423c5f42007-02-28 05:31:48 +00002112 // Promote the value if needed.
2113 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002114 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002115 case CCValAssign::Full: break;
2116 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002117 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002118 break;
2119 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002120 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002121 break;
2122 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002123 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2124 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002125 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002126 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2127 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002128 } else
2129 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2130 break;
2131 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002132 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002133 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002134 case CCValAssign::Indirect: {
2135 // Store the argument.
2136 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002137 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002138 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002139 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002140 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002141 Arg = SpillSlot;
2142 break;
2143 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002144 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002145
Chris Lattner423c5f42007-02-28 05:31:48 +00002146 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002147 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2148 if (isVarArg && IsWin64) {
2149 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2150 // shadow reg if callee is a varargs function.
2151 unsigned ShadowReg = 0;
2152 switch (VA.getLocReg()) {
2153 case X86::XMM0: ShadowReg = X86::RCX; break;
2154 case X86::XMM1: ShadowReg = X86::RDX; break;
2155 case X86::XMM2: ShadowReg = X86::R8; break;
2156 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002157 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002158 if (ShadowReg)
2159 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002160 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002161 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002162 assert(VA.isMemLoc());
2163 if (StackPtr.getNode() == 0)
2164 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2165 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2166 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002167 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002168 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002169
Evan Cheng32fe1032006-05-25 00:59:30 +00002170 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002171 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002172 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002173
Evan Cheng347d5f72006-04-28 21:29:37 +00002174 // Build a sequence of copy-to-reg nodes chained together with token chain
2175 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002176 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002177 // Tail call byval lowering might overwrite argument registers so in case of
2178 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002179 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002180 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002181 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002182 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002183 InFlag = Chain.getValue(1);
2184 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002185
Chris Lattner88e1fd52009-07-09 04:24:46 +00002186 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002187 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2188 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002189 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002190 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2191 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002192 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002193 InFlag);
2194 InFlag = Chain.getValue(1);
2195 } else {
2196 // If we are tail calling and generating PIC/GOT style code load the
2197 // address of the callee into ECX. The value in ecx is used as target of
2198 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2199 // for tail calls on PIC/GOT architectures. Normally we would just put the
2200 // address of GOT into ebx and then call target@PLT. But for tail calls
2201 // ebx would be restored (since ebx is callee saved) before jumping to the
2202 // target@PLT.
2203
2204 // Note: The actual moving to ECX is done further down.
2205 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2206 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2207 !G->getGlobal()->hasProtectedVisibility())
2208 Callee = LowerGlobalAddress(Callee, DAG);
2209 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002210 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002211 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002212 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002213
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002214 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002215 // From AMD64 ABI document:
2216 // For calls that may call functions that use varargs or stdargs
2217 // (prototype-less calls or calls to functions containing ellipsis (...) in
2218 // the declaration) %al is used as hidden argument to specify the number
2219 // of SSE registers used. The contents of %al do not need to match exactly
2220 // the number of registers, but must be an ubound on the number of SSE
2221 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002222
Gordon Henriksen86737662008-01-05 16:56:59 +00002223 // Count the number of XMM registers allocated.
2224 static const unsigned XMMArgRegs[] = {
2225 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2226 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2227 };
2228 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002229 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002230 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002231
Dale Johannesendd64c412009-02-04 00:33:20 +00002232 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002233 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002234 InFlag = Chain.getValue(1);
2235 }
2236
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002237
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002238 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002239 if (isTailCall) {
2240 // Force all the incoming stack arguments to be loaded from the stack
2241 // before any new outgoing arguments are stored to the stack, because the
2242 // outgoing stack slots may alias the incoming argument stack slots, and
2243 // the alias isn't otherwise explicit. This is slightly more conservative
2244 // than necessary, because it means that each store effectively depends
2245 // on every argument instead of just those arguments it would clobber.
2246 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2247
Dan Gohman475871a2008-07-27 21:46:04 +00002248 SmallVector<SDValue, 8> MemOpChains2;
2249 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002250 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002251 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002252 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002253 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002254 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2255 CCValAssign &VA = ArgLocs[i];
2256 if (VA.isRegLoc())
2257 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002258 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002259 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002260 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002261 // Create frame index.
2262 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002263 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002264 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002265 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002266
Duncan Sands276dcbd2008-03-21 09:14:45 +00002267 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002268 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002269 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002270 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002271 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002272 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002273 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002274
Dan Gohman98ca4f22009-08-05 01:29:28 +00002275 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2276 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002277 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002278 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002279 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002280 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002281 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002282 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002283 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002284 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002285 }
2286 }
2287
2288 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002289 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002290 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002291
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002292 // Copy arguments to their registers.
2293 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002294 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002295 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002296 InFlag = Chain.getValue(1);
2297 }
Dan Gohman475871a2008-07-27 21:46:04 +00002298 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002299
Gordon Henriksen86737662008-01-05 16:56:59 +00002300 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002301 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002302 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002303 }
2304
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002305 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2306 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2307 // In the 64-bit large code model, we have to make all calls
2308 // through a register, since the call instruction's 32-bit
2309 // pc-relative offset may not be large enough to hold the whole
2310 // address.
2311 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002312 // If the callee is a GlobalAddress node (quite common, every direct call
2313 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2314 // it.
2315
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002316 // We should use extra load for direct calls to dllimported functions in
2317 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002318 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002319 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002320 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002321 bool ExtraLoad = false;
2322 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002323
Chris Lattner48a7d022009-07-09 05:02:21 +00002324 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2325 // external symbols most go through the PLT in PIC mode. If the symbol
2326 // has hidden or protected visibility, or if it is static or local, then
2327 // we don't need to use the PLT - we can directly call it.
2328 if (Subtarget->isTargetELF() &&
2329 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002330 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002331 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002332 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002333 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002334 (!Subtarget->getTargetTriple().isMacOSX() ||
2335 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002336 // PC-relative references to external symbols should go through $stub,
2337 // unless we're building with the leopard linker or later, which
2338 // automatically synthesizes these stubs.
2339 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002340 } else if (Subtarget->isPICStyleRIPRel() &&
2341 isa<Function>(GV) &&
2342 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2343 // If the function is marked as non-lazy, generate an indirect call
2344 // which loads from the GOT directly. This avoids runtime overhead
2345 // at the cost of eager binding (and one extra byte of encoding).
2346 OpFlags = X86II::MO_GOTPCREL;
2347 WrapperKind = X86ISD::WrapperRIP;
2348 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002349 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002350
Devang Patel0d881da2010-07-06 22:08:15 +00002351 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002352 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002353
2354 // Add a wrapper if needed.
2355 if (WrapperKind != ISD::DELETED_NODE)
2356 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2357 // Add extra indirection if needed.
2358 if (ExtraLoad)
2359 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2360 MachinePointerInfo::getGOT(),
2361 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002362 }
Bill Wendling056292f2008-09-16 21:48:12 +00002363 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002364 unsigned char OpFlags = 0;
2365
Evan Cheng1bf891a2010-12-01 22:59:46 +00002366 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2367 // external symbols should go through the PLT.
2368 if (Subtarget->isTargetELF() &&
2369 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2370 OpFlags = X86II::MO_PLT;
2371 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002372 (!Subtarget->getTargetTriple().isMacOSX() ||
2373 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002374 // PC-relative references to external symbols should go through $stub,
2375 // unless we're building with the leopard linker or later, which
2376 // automatically synthesizes these stubs.
2377 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002378 }
Eric Christopherfd179292009-08-27 18:07:15 +00002379
Chris Lattner48a7d022009-07-09 05:02:21 +00002380 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2381 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002382 }
2383
Chris Lattnerd96d0722007-02-25 06:40:16 +00002384 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002385 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002386 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002387
Evan Chengf22f9b32010-02-06 03:28:46 +00002388 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002389 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2390 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002391 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002392 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002393
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002394 Ops.push_back(Chain);
2395 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002396
Dan Gohman98ca4f22009-08-05 01:29:28 +00002397 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002398 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002399
Gordon Henriksen86737662008-01-05 16:56:59 +00002400 // Add argument registers to the end of the list so that they are known live
2401 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002402 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2403 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2404 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002405
Evan Cheng586ccac2008-03-18 23:36:35 +00002406 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002407 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002408 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2409
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002410 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002411 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002412 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002413
Gabor Greifba36cb52008-08-28 21:40:38 +00002414 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002415 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002416
Dan Gohman98ca4f22009-08-05 01:29:28 +00002417 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002418 // We used to do:
2419 //// If this is the first return lowered for this function, add the regs
2420 //// to the liveout set for the function.
2421 // This isn't right, although it's probably harmless on x86; liveouts
2422 // should be computed from returns not tail calls. Consider a void
2423 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002424 return DAG.getNode(X86ISD::TC_RETURN, dl,
2425 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002426 }
2427
Dale Johannesenace16102009-02-03 19:33:06 +00002428 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002429 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002430
Chris Lattner2d297092006-05-23 18:50:38 +00002431 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002432 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002433 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002434 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002435 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002436 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002437 // pops the hidden struct pointer, so we have to push it back.
2438 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002439 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002440 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002441 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002442
Gordon Henriksenae636f82008-01-03 16:47:34 +00002443 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002444 if (!IsSibcall) {
2445 Chain = DAG.getCALLSEQ_END(Chain,
2446 DAG.getIntPtrConstant(NumBytes, true),
2447 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2448 true),
2449 InFlag);
2450 InFlag = Chain.getValue(1);
2451 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002452
Chris Lattner3085e152007-02-25 08:59:22 +00002453 // Handle result values, copying them out of physregs into vregs that we
2454 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002455 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2456 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002457}
2458
Evan Cheng25ab6902006-09-08 06:48:29 +00002459
2460//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002461// Fast Calling Convention (tail call) implementation
2462//===----------------------------------------------------------------------===//
2463
2464// Like std call, callee cleans arguments, convention except that ECX is
2465// reserved for storing the tail called function address. Only 2 registers are
2466// free for argument passing (inreg). Tail call optimization is performed
2467// provided:
2468// * tailcallopt is enabled
2469// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002470// On X86_64 architecture with GOT-style position independent code only local
2471// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002472// To keep the stack aligned according to platform abi the function
2473// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2474// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002475// If a tail called function callee has more arguments than the caller the
2476// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002477// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002478// original REtADDR, but before the saved framepointer or the spilled registers
2479// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2480// stack layout:
2481// arg1
2482// arg2
2483// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002484// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002485// move area ]
2486// (possible EBP)
2487// ESI
2488// EDI
2489// local1 ..
2490
2491/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2492/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002493unsigned
2494X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2495 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002496 MachineFunction &MF = DAG.getMachineFunction();
2497 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002498 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002499 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002500 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002501 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002502 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002503 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2504 // Number smaller than 12 so just add the difference.
2505 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2506 } else {
2507 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002508 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002509 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002510 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002511 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002512}
2513
Evan Cheng5f941932010-02-05 02:21:12 +00002514/// MatchingStackOffset - Return true if the given stack call argument is
2515/// already available in the same position (relatively) of the caller's
2516/// incoming argument stack.
2517static
2518bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2519 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2520 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002521 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2522 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002523 if (Arg.getOpcode() == ISD::CopyFromReg) {
2524 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002525 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002526 return false;
2527 MachineInstr *Def = MRI->getVRegDef(VR);
2528 if (!Def)
2529 return false;
2530 if (!Flags.isByVal()) {
2531 if (!TII->isLoadFromStackSlot(Def, FI))
2532 return false;
2533 } else {
2534 unsigned Opcode = Def->getOpcode();
2535 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2536 Def->getOperand(1).isFI()) {
2537 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002538 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002539 } else
2540 return false;
2541 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002542 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2543 if (Flags.isByVal())
2544 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002545 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002546 // define @foo(%struct.X* %A) {
2547 // tail call @bar(%struct.X* byval %A)
2548 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002549 return false;
2550 SDValue Ptr = Ld->getBasePtr();
2551 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2552 if (!FINode)
2553 return false;
2554 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002555 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002556 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002557 FI = FINode->getIndex();
2558 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002559 } else
2560 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002561
Evan Cheng4cae1332010-03-05 08:38:04 +00002562 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002563 if (!MFI->isFixedObjectIndex(FI))
2564 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002565 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002566}
2567
Dan Gohman98ca4f22009-08-05 01:29:28 +00002568/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2569/// for tail call optimization. Targets which want to do tail call
2570/// optimization should implement this function.
2571bool
2572X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002573 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002574 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002575 bool isCalleeStructRet,
2576 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002577 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002578 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002579 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002580 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002581 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002582 CalleeCC != CallingConv::C)
2583 return false;
2584
Evan Cheng7096ae42010-01-29 06:45:59 +00002585 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002586 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002587 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002588 CallingConv::ID CallerCC = CallerF->getCallingConv();
2589 bool CCMatch = CallerCC == CalleeCC;
2590
Dan Gohman1797ed52010-02-08 20:27:50 +00002591 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002592 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002593 return true;
2594 return false;
2595 }
2596
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002597 // Look for obvious safe cases to perform tail call optimization that do not
2598 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002599
Evan Cheng2c12cb42010-03-26 16:26:03 +00002600 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2601 // emit a special epilogue.
2602 if (RegInfo->needsStackRealignment(MF))
2603 return false;
2604
Evan Chenga375d472010-03-15 18:54:48 +00002605 // Also avoid sibcall optimization if either caller or callee uses struct
2606 // return semantics.
2607 if (isCalleeStructRet || isCallerStructRet)
2608 return false;
2609
Chad Rosier2416da32011-06-24 21:15:36 +00002610 // An stdcall caller is expected to clean up its arguments; the callee
2611 // isn't going to do that.
2612 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2613 return false;
2614
Chad Rosier871f6642011-05-18 19:59:50 +00002615 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002616 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002617 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002618
2619 // Optimizing for varargs on Win64 is unlikely to be safe without
2620 // additional testing.
2621 if (Subtarget->isTargetWin64())
2622 return false;
2623
Chad Rosier871f6642011-05-18 19:59:50 +00002624 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002625 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2626 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002627
Chad Rosier871f6642011-05-18 19:59:50 +00002628 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2629 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2630 if (!ArgLocs[i].isRegLoc())
2631 return false;
2632 }
2633
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002634 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2635 // Therefore if it's not used by the call it is not safe to optimize this into
2636 // a sibcall.
2637 bool Unused = false;
2638 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2639 if (!Ins[i].Used) {
2640 Unused = true;
2641 break;
2642 }
2643 }
2644 if (Unused) {
2645 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002646 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2647 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002648 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002649 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002650 CCValAssign &VA = RVLocs[i];
2651 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2652 return false;
2653 }
2654 }
2655
Evan Cheng13617962010-04-30 01:12:32 +00002656 // If the calling conventions do not match, then we'd better make sure the
2657 // results are returned in the same way as what the caller expects.
2658 if (!CCMatch) {
2659 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002660 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2661 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002662 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2663
2664 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002665 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2666 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002667 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2668
2669 if (RVLocs1.size() != RVLocs2.size())
2670 return false;
2671 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2672 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2673 return false;
2674 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2675 return false;
2676 if (RVLocs1[i].isRegLoc()) {
2677 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2678 return false;
2679 } else {
2680 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2681 return false;
2682 }
2683 }
2684 }
2685
Evan Chenga6bff982010-01-30 01:22:00 +00002686 // If the callee takes no arguments then go on to check the results of the
2687 // call.
2688 if (!Outs.empty()) {
2689 // Check if stack adjustment is needed. For now, do not do this if any
2690 // argument is passed on the stack.
2691 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002692 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2693 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002694
2695 // Allocate shadow area for Win64
2696 if (Subtarget->isTargetWin64()) {
2697 CCInfo.AllocateStack(32, 8);
2698 }
2699
Duncan Sands45907662010-10-31 13:21:44 +00002700 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002701 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002702 MachineFunction &MF = DAG.getMachineFunction();
2703 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2704 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002705
2706 // Check if the arguments are already laid out in the right way as
2707 // the caller's fixed stack objects.
2708 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002709 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2710 const X86InstrInfo *TII =
2711 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002712 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2713 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002714 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002715 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002716 if (VA.getLocInfo() == CCValAssign::Indirect)
2717 return false;
2718 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002719 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2720 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002721 return false;
2722 }
2723 }
2724 }
Evan Cheng9c044672010-05-29 01:35:22 +00002725
2726 // If the tailcall address may be in a register, then make sure it's
2727 // possible to register allocate for it. In 32-bit, the call address can
2728 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002729 // callee-saved registers are restored. These happen to be the same
2730 // registers used to pass 'inreg' arguments so watch out for those.
2731 if (!Subtarget->is64Bit() &&
2732 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002733 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002734 unsigned NumInRegs = 0;
2735 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2736 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002737 if (!VA.isRegLoc())
2738 continue;
2739 unsigned Reg = VA.getLocReg();
2740 switch (Reg) {
2741 default: break;
2742 case X86::EAX: case X86::EDX: case X86::ECX:
2743 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002744 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002745 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002746 }
2747 }
2748 }
Evan Chenga6bff982010-01-30 01:22:00 +00002749 }
Evan Chengb1712452010-01-27 06:25:16 +00002750
Evan Cheng86809cc2010-02-03 03:28:02 +00002751 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002752}
2753
Dan Gohman3df24e62008-09-03 23:12:08 +00002754FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002755X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2756 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002757}
2758
2759
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002760//===----------------------------------------------------------------------===//
2761// Other Lowering Hooks
2762//===----------------------------------------------------------------------===//
2763
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002764static bool MayFoldLoad(SDValue Op) {
2765 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2766}
2767
2768static bool MayFoldIntoStore(SDValue Op) {
2769 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2770}
2771
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002772static bool isTargetShuffle(unsigned Opcode) {
2773 switch(Opcode) {
2774 default: return false;
2775 case X86ISD::PSHUFD:
2776 case X86ISD::PSHUFHW:
2777 case X86ISD::PSHUFLW:
2778 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002779 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002780 case X86ISD::SHUFPS:
2781 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002782 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002783 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002784 case X86ISD::MOVLPS:
2785 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002786 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002787 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002788 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002789 case X86ISD::MOVSS:
2790 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002791 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002792 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002793 case X86ISD::VUNPCKLPSY:
2794 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002795 case X86ISD::PUNPCKLWD:
2796 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002797 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002798 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002799 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002800 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002801 case X86ISD::VUNPCKHPSY:
2802 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002803 case X86ISD::PUNPCKHWD:
2804 case X86ISD::PUNPCKHBW:
2805 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002806 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002807 case X86ISD::VPERMILPS:
2808 case X86ISD::VPERMILPSY:
2809 case X86ISD::VPERMILPD:
2810 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002811 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002812 return true;
2813 }
2814 return false;
2815}
2816
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002817static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002818 SDValue V1, SelectionDAG &DAG) {
2819 switch(Opc) {
2820 default: llvm_unreachable("Unknown x86 shuffle node");
2821 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002822 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002823 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002824 return DAG.getNode(Opc, dl, VT, V1);
2825 }
2826
2827 return SDValue();
2828}
2829
2830static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002831 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002832 switch(Opc) {
2833 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002834 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002835 case X86ISD::PSHUFHW:
2836 case X86ISD::PSHUFLW:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002837 case X86ISD::VPERMILPS:
2838 case X86ISD::VPERMILPSY:
2839 case X86ISD::VPERMILPD:
2840 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002841 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2842 }
2843
2844 return SDValue();
2845}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002846
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002847static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2848 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2849 switch(Opc) {
2850 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002851 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002852 case X86ISD::SHUFPD:
2853 case X86ISD::SHUFPS:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002854 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002855 return DAG.getNode(Opc, dl, VT, V1, V2,
2856 DAG.getConstant(TargetMask, MVT::i8));
2857 }
2858 return SDValue();
2859}
2860
2861static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2862 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2863 switch(Opc) {
2864 default: llvm_unreachable("Unknown x86 shuffle node");
2865 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002866 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002867 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002868 case X86ISD::MOVLPS:
2869 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002870 case X86ISD::MOVSS:
2871 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002872 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002873 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002874 case X86ISD::VUNPCKLPSY:
2875 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002876 case X86ISD::PUNPCKLWD:
2877 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002878 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002879 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002880 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002881 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002882 case X86ISD::VUNPCKHPSY:
2883 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002884 case X86ISD::PUNPCKHWD:
2885 case X86ISD::PUNPCKHBW:
2886 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002887 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002888 return DAG.getNode(Opc, dl, VT, V1, V2);
2889 }
2890 return SDValue();
2891}
2892
Dan Gohmand858e902010-04-17 15:26:15 +00002893SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002894 MachineFunction &MF = DAG.getMachineFunction();
2895 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2896 int ReturnAddrIndex = FuncInfo->getRAIndex();
2897
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002898 if (ReturnAddrIndex == 0) {
2899 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002900 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002901 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002902 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002903 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002904 }
2905
Evan Cheng25ab6902006-09-08 06:48:29 +00002906 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002907}
2908
2909
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002910bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2911 bool hasSymbolicDisplacement) {
2912 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002913 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002914 return false;
2915
2916 // If we don't have a symbolic displacement - we don't have any extra
2917 // restrictions.
2918 if (!hasSymbolicDisplacement)
2919 return true;
2920
2921 // FIXME: Some tweaks might be needed for medium code model.
2922 if (M != CodeModel::Small && M != CodeModel::Kernel)
2923 return false;
2924
2925 // For small code model we assume that latest object is 16MB before end of 31
2926 // bits boundary. We may also accept pretty large negative constants knowing
2927 // that all objects are in the positive half of address space.
2928 if (M == CodeModel::Small && Offset < 16*1024*1024)
2929 return true;
2930
2931 // For kernel code model we know that all object resist in the negative half
2932 // of 32bits address space. We may not accept negative offsets, since they may
2933 // be just off and we may accept pretty large positive ones.
2934 if (M == CodeModel::Kernel && Offset > 0)
2935 return true;
2936
2937 return false;
2938}
2939
Evan Chengef41ff62011-06-23 17:54:54 +00002940/// isCalleePop - Determines whether the callee is required to pop its
2941/// own arguments. Callee pop is necessary to support tail calls.
2942bool X86::isCalleePop(CallingConv::ID CallingConv,
2943 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2944 if (IsVarArg)
2945 return false;
2946
2947 switch (CallingConv) {
2948 default:
2949 return false;
2950 case CallingConv::X86_StdCall:
2951 return !is64Bit;
2952 case CallingConv::X86_FastCall:
2953 return !is64Bit;
2954 case CallingConv::X86_ThisCall:
2955 return !is64Bit;
2956 case CallingConv::Fast:
2957 return TailCallOpt;
2958 case CallingConv::GHC:
2959 return TailCallOpt;
2960 }
2961}
2962
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002963/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2964/// specific condition code, returning the condition code and the LHS/RHS of the
2965/// comparison to make.
2966static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2967 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002968 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002969 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2970 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2971 // X > -1 -> X == 0, jump !sign.
2972 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002973 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002974 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2975 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002976 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002977 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002978 // X < 1 -> X <= 0
2979 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002980 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002981 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002982 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002983
Evan Chengd9558e02006-01-06 00:43:03 +00002984 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002985 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002986 case ISD::SETEQ: return X86::COND_E;
2987 case ISD::SETGT: return X86::COND_G;
2988 case ISD::SETGE: return X86::COND_GE;
2989 case ISD::SETLT: return X86::COND_L;
2990 case ISD::SETLE: return X86::COND_LE;
2991 case ISD::SETNE: return X86::COND_NE;
2992 case ISD::SETULT: return X86::COND_B;
2993 case ISD::SETUGT: return X86::COND_A;
2994 case ISD::SETULE: return X86::COND_BE;
2995 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002996 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002997 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002998
Chris Lattner4c78e022008-12-23 23:42:27 +00002999 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003000
Chris Lattner4c78e022008-12-23 23:42:27 +00003001 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003002 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3003 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003004 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3005 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003006 }
3007
Chris Lattner4c78e022008-12-23 23:42:27 +00003008 switch (SetCCOpcode) {
3009 default: break;
3010 case ISD::SETOLT:
3011 case ISD::SETOLE:
3012 case ISD::SETUGT:
3013 case ISD::SETUGE:
3014 std::swap(LHS, RHS);
3015 break;
3016 }
3017
3018 // On a floating point condition, the flags are set as follows:
3019 // ZF PF CF op
3020 // 0 | 0 | 0 | X > Y
3021 // 0 | 0 | 1 | X < Y
3022 // 1 | 0 | 0 | X == Y
3023 // 1 | 1 | 1 | unordered
3024 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003025 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003026 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003027 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003028 case ISD::SETOLT: // flipped
3029 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003030 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003031 case ISD::SETOLE: // flipped
3032 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003033 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003034 case ISD::SETUGT: // flipped
3035 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003036 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003037 case ISD::SETUGE: // flipped
3038 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003039 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003040 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003041 case ISD::SETNE: return X86::COND_NE;
3042 case ISD::SETUO: return X86::COND_P;
3043 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003044 case ISD::SETOEQ:
3045 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003046 }
Evan Chengd9558e02006-01-06 00:43:03 +00003047}
3048
Evan Cheng4a460802006-01-11 00:33:36 +00003049/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3050/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003051/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003052static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003053 switch (X86CC) {
3054 default:
3055 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003056 case X86::COND_B:
3057 case X86::COND_BE:
3058 case X86::COND_E:
3059 case X86::COND_P:
3060 case X86::COND_A:
3061 case X86::COND_AE:
3062 case X86::COND_NE:
3063 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003064 return true;
3065 }
3066}
3067
Evan Chengeb2f9692009-10-27 19:56:55 +00003068/// isFPImmLegal - Returns true if the target can instruction select the
3069/// specified FP immediate natively. If false, the legalizer will
3070/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003071bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003072 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3073 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3074 return true;
3075 }
3076 return false;
3077}
3078
Nate Begeman9008ca62009-04-27 18:41:29 +00003079/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3080/// the specified range (L, H].
3081static bool isUndefOrInRange(int Val, int Low, int Hi) {
3082 return (Val < 0) || (Val >= Low && Val < Hi);
3083}
3084
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003085/// isUndefOrInRange - Return true if every element in Mask, begining
3086/// from position Pos and ending in Pos+Size, falls within the specified
3087/// range (L, L+Pos]. or is undef.
3088static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3089 int Pos, int Size, int Low, int Hi) {
3090 for (int i = Pos, e = Pos+Size; i != e; ++i)
3091 if (!isUndefOrInRange(Mask[i], Low, Hi))
3092 return false;
3093 return true;
3094}
3095
Nate Begeman9008ca62009-04-27 18:41:29 +00003096/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3097/// specified value.
3098static bool isUndefOrEqual(int Val, int CmpVal) {
3099 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003100 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003101 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003102}
3103
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003104/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3105/// from position Pos and ending in Pos+Size, falls within the specified
3106/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003107static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3108 int Pos, int Size, int Low) {
3109 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3110 if (!isUndefOrEqual(Mask[i], Low))
3111 return false;
3112 return true;
3113}
3114
Nate Begeman9008ca62009-04-27 18:41:29 +00003115/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3116/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3117/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003118static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003119 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003120 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003121 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003122 return (Mask[0] < 2 && Mask[1] < 2);
3123 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003124}
3125
Nate Begeman9008ca62009-04-27 18:41:29 +00003126bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003127 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003128 N->getMask(M);
3129 return ::isPSHUFDMask(M, N->getValueType(0));
3130}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003131
Nate Begeman9008ca62009-04-27 18:41:29 +00003132/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3133/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003134static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003135 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003136 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003137
Nate Begeman9008ca62009-04-27 18:41:29 +00003138 // Lower quadword copied in order or undef.
3139 for (int i = 0; i != 4; ++i)
3140 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003141 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003142
Evan Cheng506d3df2006-03-29 23:07:14 +00003143 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003144 for (int i = 4; i != 8; ++i)
3145 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003146 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003147
Evan Cheng506d3df2006-03-29 23:07:14 +00003148 return true;
3149}
3150
Nate Begeman9008ca62009-04-27 18:41:29 +00003151bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003152 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003153 N->getMask(M);
3154 return ::isPSHUFHWMask(M, N->getValueType(0));
3155}
Evan Cheng506d3df2006-03-29 23:07:14 +00003156
Nate Begeman9008ca62009-04-27 18:41:29 +00003157/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3158/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003159static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003160 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003161 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003162
Rafael Espindola15684b22009-04-24 12:40:33 +00003163 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003164 for (int i = 4; i != 8; ++i)
3165 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003166 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003167
Rafael Espindola15684b22009-04-24 12:40:33 +00003168 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003169 for (int i = 0; i != 4; ++i)
3170 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003171 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003172
Rafael Espindola15684b22009-04-24 12:40:33 +00003173 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003174}
3175
Nate Begeman9008ca62009-04-27 18:41:29 +00003176bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003177 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003178 N->getMask(M);
3179 return ::isPSHUFLWMask(M, N->getValueType(0));
3180}
3181
Nate Begemana09008b2009-10-19 02:17:23 +00003182/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3183/// is suitable for input to PALIGNR.
3184static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003185 bool hasSSSE3OrAVX) {
Nate Begemana09008b2009-10-19 02:17:23 +00003186 int i, e = VT.getVectorNumElements();
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003187 if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
3188 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003189
Nate Begemana09008b2009-10-19 02:17:23 +00003190 // Do not handle v2i64 / v2f64 shuffles with palignr.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003191 if (e < 4 || !hasSSSE3OrAVX)
Nate Begemana09008b2009-10-19 02:17:23 +00003192 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003193
Nate Begemana09008b2009-10-19 02:17:23 +00003194 for (i = 0; i != e; ++i)
3195 if (Mask[i] >= 0)
3196 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003197
Nate Begemana09008b2009-10-19 02:17:23 +00003198 // All undef, not a palignr.
3199 if (i == e)
3200 return false;
3201
Eli Friedman63f8dde2011-07-25 21:36:45 +00003202 // Make sure we're shifting in the right direction.
3203 if (Mask[i] <= i)
3204 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003205
3206 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003207
Nate Begemana09008b2009-10-19 02:17:23 +00003208 // Check the rest of the elements to see if they are consecutive.
3209 for (++i; i != e; ++i) {
3210 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003211 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003212 return false;
3213 }
3214 return true;
3215}
3216
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003217/// isVSHUFPSYMask - Return true if the specified VECTOR_SHUFFLE operand
3218/// specifies a shuffle of elements that is suitable for input to 256-bit
3219/// VSHUFPSY.
3220static bool isVSHUFPSYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3221 const X86Subtarget *Subtarget) {
3222 int NumElems = VT.getVectorNumElements();
3223
3224 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3225 return false;
3226
3227 if (NumElems != 8)
3228 return false;
3229
3230 // VSHUFPSY divides the resulting vector into 4 chunks.
3231 // The sources are also splitted into 4 chunks, and each destination
3232 // chunk must come from a different source chunk.
3233 //
3234 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3235 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3236 //
3237 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3238 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3239 //
3240 int QuarterSize = NumElems/4;
3241 int HalfSize = QuarterSize*2;
3242 for (int i = 0; i < QuarterSize; ++i)
3243 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3244 return false;
3245 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3246 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3247 return false;
3248
3249 // The mask of the second half must be the same as the first but with
3250 // the appropriate offsets. This works in the same way as VPERMILPS
3251 // works with masks.
3252 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3253 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3254 return false;
3255 int FstHalfIdx = i-HalfSize;
3256 if (Mask[FstHalfIdx] < 0)
3257 continue;
3258 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3259 return false;
3260 }
3261 for (int i = QuarterSize*3; i < NumElems; ++i) {
3262 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3263 return false;
3264 int FstHalfIdx = i-HalfSize;
3265 if (Mask[FstHalfIdx] < 0)
3266 continue;
3267 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3268 return false;
3269
3270 }
3271
3272 return true;
3273}
3274
3275/// getShuffleVSHUFPSYImmediate - Return the appropriate immediate to shuffle
3276/// the specified VECTOR_MASK mask with VSHUFPSY instruction.
3277static unsigned getShuffleVSHUFPSYImmediate(SDNode *N) {
3278 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3279 EVT VT = SVOp->getValueType(0);
3280 int NumElems = VT.getVectorNumElements();
3281
3282 assert(NumElems == 8 && VT.getSizeInBits() == 256 &&
3283 "Only supports v8i32 and v8f32 types");
3284
3285 int HalfSize = NumElems/2;
3286 unsigned Mask = 0;
3287 for (int i = 0; i != NumElems ; ++i) {
3288 if (SVOp->getMaskElt(i) < 0)
3289 continue;
3290 // The mask of the first half must be equal to the second one.
3291 unsigned Shamt = (i%HalfSize)*2;
3292 unsigned Elt = SVOp->getMaskElt(i) % HalfSize;
3293 Mask |= Elt << Shamt;
3294 }
3295
3296 return Mask;
3297}
3298
3299/// isVSHUFPDYMask - Return true if the specified VECTOR_SHUFFLE operand
3300/// specifies a shuffle of elements that is suitable for input to 256-bit
3301/// VSHUFPDY. This shuffle doesn't have the same restriction as the PS
3302/// version and the mask of the second half isn't binded with the first
3303/// one.
3304static bool isVSHUFPDYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3305 const X86Subtarget *Subtarget) {
3306 int NumElems = VT.getVectorNumElements();
3307
3308 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3309 return false;
3310
3311 if (NumElems != 4)
3312 return false;
3313
3314 // VSHUFPSY divides the resulting vector into 4 chunks.
3315 // The sources are also splitted into 4 chunks, and each destination
3316 // chunk must come from a different source chunk.
3317 //
3318 // SRC1 => X3 X2 X1 X0
3319 // SRC2 => Y3 Y2 Y1 Y0
3320 //
3321 // DST => Y2..Y3, X2..X3, Y1..Y0, X1..X0
3322 //
3323 int QuarterSize = NumElems/4;
3324 int HalfSize = QuarterSize*2;
3325 for (int i = 0; i < QuarterSize; ++i)
3326 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3327 return false;
3328 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3329 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3330 return false;
3331 for (int i = QuarterSize*2; i < QuarterSize*3; ++i)
3332 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3333 return false;
3334 for (int i = QuarterSize*3; i < NumElems; ++i)
3335 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3336 return false;
3337
3338 return true;
3339}
3340
3341/// getShuffleVSHUFPDYImmediate - Return the appropriate immediate to shuffle
3342/// the specified VECTOR_MASK mask with VSHUFPDY instruction.
3343static unsigned getShuffleVSHUFPDYImmediate(SDNode *N) {
3344 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3345 EVT VT = SVOp->getValueType(0);
3346 int NumElems = VT.getVectorNumElements();
3347
3348 assert(NumElems == 4 && VT.getSizeInBits() == 256 &&
3349 "Only supports v4i64 and v4f64 types");
3350
3351 int HalfSize = NumElems/2;
3352 unsigned Mask = 0;
3353 for (int i = 0; i != NumElems ; ++i) {
3354 if (SVOp->getMaskElt(i) < 0)
3355 continue;
3356 int Elt = SVOp->getMaskElt(i) % HalfSize;
3357 Mask |= Elt << i;
3358 }
3359
3360 return Mask;
3361}
3362
Evan Cheng14aed5e2006-03-24 01:18:28 +00003363/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003364/// specifies a shuffle of elements that is suitable for input to 128-bit
3365/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003366static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003367 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003368
3369 if (VT.getSizeInBits() != 128)
3370 return false;
3371
Nate Begeman9008ca62009-04-27 18:41:29 +00003372 if (NumElems != 2 && NumElems != 4)
3373 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003374
Nate Begeman9008ca62009-04-27 18:41:29 +00003375 int Half = NumElems / 2;
3376 for (int i = 0; i < Half; ++i)
3377 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003378 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003379 for (int i = Half; i < NumElems; ++i)
3380 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003381 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003382
Evan Cheng14aed5e2006-03-24 01:18:28 +00003383 return true;
3384}
3385
Nate Begeman9008ca62009-04-27 18:41:29 +00003386bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3387 SmallVector<int, 8> M;
3388 N->getMask(M);
3389 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003390}
3391
Evan Cheng213d2cf2007-05-17 18:45:50 +00003392/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003393/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3394/// half elements to come from vector 1 (which would equal the dest.) and
3395/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003396static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003397 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003398
3399 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003400 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003401
Nate Begeman9008ca62009-04-27 18:41:29 +00003402 int Half = NumElems / 2;
3403 for (int i = 0; i < Half; ++i)
3404 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003405 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003406 for (int i = Half; i < NumElems; ++i)
3407 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003408 return false;
3409 return true;
3410}
3411
Nate Begeman9008ca62009-04-27 18:41:29 +00003412static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3413 SmallVector<int, 8> M;
3414 N->getMask(M);
3415 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003416}
3417
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003418/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3419/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003420bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003421 EVT VT = N->getValueType(0);
3422 unsigned NumElems = VT.getVectorNumElements();
3423
3424 if (VT.getSizeInBits() != 128)
3425 return false;
3426
3427 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003428 return false;
3429
Evan Cheng2064a2b2006-03-28 06:50:32 +00003430 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003431 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3432 isUndefOrEqual(N->getMaskElt(1), 7) &&
3433 isUndefOrEqual(N->getMaskElt(2), 2) &&
3434 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003435}
3436
Nate Begeman0b10b912009-11-07 23:17:15 +00003437/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3438/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3439/// <2, 3, 2, 3>
3440bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003441 EVT VT = N->getValueType(0);
3442 unsigned NumElems = VT.getVectorNumElements();
3443
3444 if (VT.getSizeInBits() != 128)
3445 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003446
Nate Begeman0b10b912009-11-07 23:17:15 +00003447 if (NumElems != 4)
3448 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003449
Nate Begeman0b10b912009-11-07 23:17:15 +00003450 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003451 isUndefOrEqual(N->getMaskElt(1), 3) &&
3452 isUndefOrEqual(N->getMaskElt(2), 2) &&
3453 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003454}
3455
Evan Cheng5ced1d82006-04-06 23:23:56 +00003456/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3457/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003458bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3459 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003460
Evan Cheng5ced1d82006-04-06 23:23:56 +00003461 if (NumElems != 2 && NumElems != 4)
3462 return false;
3463
Evan Chengc5cdff22006-04-07 21:53:05 +00003464 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003465 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003466 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003467
Evan Chengc5cdff22006-04-07 21:53:05 +00003468 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003469 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003470 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003471
3472 return true;
3473}
3474
Nate Begeman0b10b912009-11-07 23:17:15 +00003475/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3476/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3477bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003478 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003479
David Greenea20244d2011-03-02 17:23:43 +00003480 if ((NumElems != 2 && NumElems != 4)
3481 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003482 return false;
3483
Evan Chengc5cdff22006-04-07 21:53:05 +00003484 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003485 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003486 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003487
Nate Begeman9008ca62009-04-27 18:41:29 +00003488 for (unsigned i = 0; i < NumElems/2; ++i)
3489 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003490 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003491
3492 return true;
3493}
3494
Evan Cheng0038e592006-03-28 00:39:58 +00003495/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3496/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003497static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003498 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003499 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003500
3501 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3502 "Unsupported vector type for unpckh");
3503
3504 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng0038e592006-03-28 00:39:58 +00003505 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003506
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003507 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3508 // independently on 128-bit lanes.
3509 unsigned NumLanes = VT.getSizeInBits()/128;
3510 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003511
3512 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003513 unsigned End = NumLaneElts;
3514 for (unsigned s = 0; s < NumLanes; ++s) {
3515 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003516 i != End;
3517 i += 2, ++j) {
3518 int BitI = Mask[i];
3519 int BitI1 = Mask[i+1];
3520 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003521 return false;
David Greenea20244d2011-03-02 17:23:43 +00003522 if (V2IsSplat) {
3523 if (!isUndefOrEqual(BitI1, NumElts))
3524 return false;
3525 } else {
3526 if (!isUndefOrEqual(BitI1, j + NumElts))
3527 return false;
3528 }
Evan Cheng39623da2006-04-20 08:58:49 +00003529 }
David Greenea20244d2011-03-02 17:23:43 +00003530 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003531 Start += NumLaneElts;
3532 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003533 }
David Greenea20244d2011-03-02 17:23:43 +00003534
Evan Cheng0038e592006-03-28 00:39:58 +00003535 return true;
3536}
3537
Nate Begeman9008ca62009-04-27 18:41:29 +00003538bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3539 SmallVector<int, 8> M;
3540 N->getMask(M);
3541 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003542}
3543
Evan Cheng4fcb9222006-03-28 02:43:26 +00003544/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3545/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003546static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003547 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003548 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003549
3550 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3551 "Unsupported vector type for unpckh");
3552
3553 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003554 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003555
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003556 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3557 // independently on 128-bit lanes.
3558 unsigned NumLanes = VT.getSizeInBits()/128;
3559 unsigned NumLaneElts = NumElts/NumLanes;
3560
3561 unsigned Start = 0;
3562 unsigned End = NumLaneElts;
3563 for (unsigned l = 0; l != NumLanes; ++l) {
3564 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3565 i != End; i += 2, ++j) {
3566 int BitI = Mask[i];
3567 int BitI1 = Mask[i+1];
3568 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003569 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003570 if (V2IsSplat) {
3571 if (isUndefOrEqual(BitI1, NumElts))
3572 return false;
3573 } else {
3574 if (!isUndefOrEqual(BitI1, j+NumElts))
3575 return false;
3576 }
Evan Cheng39623da2006-04-20 08:58:49 +00003577 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003578 // Process the next 128 bits.
3579 Start += NumLaneElts;
3580 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003581 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003582 return true;
3583}
3584
Nate Begeman9008ca62009-04-27 18:41:29 +00003585bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3586 SmallVector<int, 8> M;
3587 N->getMask(M);
3588 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003589}
3590
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003591/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3592/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3593/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003594static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003595 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003596 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003597 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003598
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003599 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3600 // FIXME: Need a better way to get rid of this, there's no latency difference
3601 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3602 // the former later. We should also remove the "_undef" special mask.
3603 if (NumElems == 4 && VT.getSizeInBits() == 256)
3604 return false;
3605
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003606 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3607 // independently on 128-bit lanes.
3608 unsigned NumLanes = VT.getSizeInBits() / 128;
3609 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003610
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003611 for (unsigned s = 0; s < NumLanes; ++s) {
3612 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3613 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003614 i += 2, ++j) {
3615 int BitI = Mask[i];
3616 int BitI1 = Mask[i+1];
3617
3618 if (!isUndefOrEqual(BitI, j))
3619 return false;
3620 if (!isUndefOrEqual(BitI1, j))
3621 return false;
3622 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003623 }
David Greenea20244d2011-03-02 17:23:43 +00003624
Rafael Espindola15684b22009-04-24 12:40:33 +00003625 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003626}
3627
Nate Begeman9008ca62009-04-27 18:41:29 +00003628bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3629 SmallVector<int, 8> M;
3630 N->getMask(M);
3631 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3632}
3633
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003634/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3635/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3636/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003637static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003638 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003639 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3640 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003641
Nate Begeman9008ca62009-04-27 18:41:29 +00003642 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3643 int BitI = Mask[i];
3644 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003645 if (!isUndefOrEqual(BitI, j))
3646 return false;
3647 if (!isUndefOrEqual(BitI1, j))
3648 return false;
3649 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003650 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003651}
3652
Nate Begeman9008ca62009-04-27 18:41:29 +00003653bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3654 SmallVector<int, 8> M;
3655 N->getMask(M);
3656 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3657}
3658
Evan Cheng017dcc62006-04-21 01:05:10 +00003659/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3660/// specifies a shuffle of elements that is suitable for input to MOVSS,
3661/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003662static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003663 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003664 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003665
3666 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003667
Nate Begeman9008ca62009-04-27 18:41:29 +00003668 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003669 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003670
Nate Begeman9008ca62009-04-27 18:41:29 +00003671 for (int i = 1; i < NumElts; ++i)
3672 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003673 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003674
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003675 return true;
3676}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003677
Nate Begeman9008ca62009-04-27 18:41:29 +00003678bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3679 SmallVector<int, 8> M;
3680 N->getMask(M);
3681 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003682}
3683
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003684/// isVPERM2F128Mask - Match 256-bit shuffles where the elements are considered
3685/// as permutations between 128-bit chunks or halves. As an example: this
3686/// shuffle bellow:
3687/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3688/// The first half comes from the second half of V1 and the second half from the
3689/// the second half of V2.
3690static bool isVPERM2F128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3691 const X86Subtarget *Subtarget) {
3692 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3693 return false;
3694
3695 // The shuffle result is divided into half A and half B. In total the two
3696 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3697 // B must come from C, D, E or F.
3698 int HalfSize = VT.getVectorNumElements()/2;
3699 bool MatchA = false, MatchB = false;
3700
3701 // Check if A comes from one of C, D, E, F.
3702 for (int Half = 0; Half < 4; ++Half) {
3703 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3704 MatchA = true;
3705 break;
3706 }
3707 }
3708
3709 // Check if B comes from one of C, D, E, F.
3710 for (int Half = 0; Half < 4; ++Half) {
3711 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3712 MatchB = true;
3713 break;
3714 }
3715 }
3716
3717 return MatchA && MatchB;
3718}
3719
3720/// getShuffleVPERM2F128Immediate - Return the appropriate immediate to shuffle
3721/// the specified VECTOR_MASK mask with VPERM2F128 instructions.
3722static unsigned getShuffleVPERM2F128Immediate(SDNode *N) {
3723 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3724 EVT VT = SVOp->getValueType(0);
3725
3726 int HalfSize = VT.getVectorNumElements()/2;
3727
3728 int FstHalf = 0, SndHalf = 0;
3729 for (int i = 0; i < HalfSize; ++i) {
3730 if (SVOp->getMaskElt(i) > 0) {
3731 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3732 break;
3733 }
3734 }
3735 for (int i = HalfSize; i < HalfSize*2; ++i) {
3736 if (SVOp->getMaskElt(i) > 0) {
3737 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3738 break;
3739 }
3740 }
3741
3742 return (FstHalf | (SndHalf << 4));
3743}
3744
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003745/// isVPERMILPDMask - Return true if the specified VECTOR_SHUFFLE operand
3746/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3747/// Note that VPERMIL mask matching is different depending whether theunderlying
3748/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3749/// to the same elements of the low, but to the higher half of the source.
3750/// In VPERMILPD the two lanes could be shuffled independently of each other
3751/// with the same restriction that lanes can't be crossed.
3752static bool isVPERMILPDMask(const SmallVectorImpl<int> &Mask, EVT VT,
3753 const X86Subtarget *Subtarget) {
3754 int NumElts = VT.getVectorNumElements();
3755 int NumLanes = VT.getSizeInBits()/128;
3756
3757 if (!Subtarget->hasAVX())
3758 return false;
3759
3760 // Match any permutation of 128-bit vector with 64-bit types
3761 if (NumLanes == 1 && NumElts != 2)
3762 return false;
3763
3764 // Only match 256-bit with 32 types
3765 if (VT.getSizeInBits() == 256 && NumElts != 4)
3766 return false;
3767
3768 // The mask on the high lane is independent of the low. Both can match
3769 // any element in inside its own lane, but can't cross.
3770 int LaneSize = NumElts/NumLanes;
3771 for (int l = 0; l < NumLanes; ++l)
3772 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3773 int LaneStart = l*LaneSize;
3774 if (!isUndefOrInRange(Mask[i], LaneStart, LaneStart+LaneSize))
3775 return false;
3776 }
3777
3778 return true;
3779}
3780
3781/// isVPERMILPSMask - Return true if the specified VECTOR_SHUFFLE operand
3782/// specifies a shuffle of elements that is suitable for input to VPERMILPS*.
3783/// Note that VPERMIL mask matching is different depending whether theunderlying
3784/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3785/// to the same elements of the low, but to the higher half of the source.
3786/// In VPERMILPD the two lanes could be shuffled independently of each other
3787/// with the same restriction that lanes can't be crossed.
3788static bool isVPERMILPSMask(const SmallVectorImpl<int> &Mask, EVT VT,
3789 const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003790 unsigned NumElts = VT.getVectorNumElements();
3791 unsigned NumLanes = VT.getSizeInBits()/128;
3792
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003793 if (!Subtarget->hasAVX())
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003794 return false;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003795
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003796 // Match any permutation of 128-bit vector with 32-bit types
3797 if (NumLanes == 1 && NumElts != 4)
3798 return false;
3799
3800 // Only match 256-bit with 32 types
3801 if (VT.getSizeInBits() == 256 && NumElts != 8)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003802 return false;
3803
3804 // The mask on the high lane should be the same as the low. Actually,
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003805 // they can differ if any of the corresponding index in a lane is undef
3806 // and the other stays in range.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003807 int LaneSize = NumElts/NumLanes;
3808 for (int i = 0; i < LaneSize; ++i) {
3809 int HighElt = i+LaneSize;
Bruno Cardoso Lopes155a92a2011-08-10 01:54:17 +00003810 bool HighValid = isUndefOrInRange(Mask[HighElt], LaneSize, NumElts);
3811 bool LowValid = isUndefOrInRange(Mask[i], 0, LaneSize);
3812
3813 if (!HighValid || !LowValid)
3814 return false;
3815 if (Mask[i] < 0 || Mask[HighElt] < 0)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003816 continue;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003817 if (Mask[HighElt]-Mask[i] != LaneSize)
3818 return false;
3819 }
3820
3821 return true;
3822}
3823
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003824/// getShuffleVPERMILPSImmediate - Return the appropriate immediate to shuffle
3825/// the specified VECTOR_MASK mask with VPERMILPS* instructions.
3826static unsigned getShuffleVPERMILPSImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003827 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3828 EVT VT = SVOp->getValueType(0);
3829
3830 int NumElts = VT.getVectorNumElements();
3831 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003832 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003833
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003834 // Although the mask is equal for both lanes do it twice to get the cases
3835 // where a mask will match because the same mask element is undef on the
3836 // first half but valid on the second. This would get pathological cases
3837 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003838 unsigned Mask = 0;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003839 for (int l = 0; l < NumLanes; ++l) {
3840 for (int i = 0; i < LaneSize; ++i) {
3841 int MaskElt = SVOp->getMaskElt(i+(l*LaneSize));
3842 if (MaskElt < 0)
3843 continue;
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003844 if (MaskElt >= LaneSize)
3845 MaskElt -= LaneSize;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003846 Mask |= MaskElt << (i*2);
3847 }
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003848 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003849
3850 return Mask;
3851}
3852
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003853/// getShuffleVPERMILPDImmediate - Return the appropriate immediate to shuffle
3854/// the specified VECTOR_MASK mask with VPERMILPD* instructions.
3855static unsigned getShuffleVPERMILPDImmediate(SDNode *N) {
3856 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3857 EVT VT = SVOp->getValueType(0);
3858
3859 int NumElts = VT.getVectorNumElements();
3860 int NumLanes = VT.getSizeInBits()/128;
3861
3862 unsigned Mask = 0;
3863 int LaneSize = NumElts/NumLanes;
3864 for (int l = 0; l < NumLanes; ++l)
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003865 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3866 int MaskElt = SVOp->getMaskElt(i);
3867 if (MaskElt < 0)
3868 continue;
3869 Mask |= (MaskElt-l*LaneSize) << i;
3870 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003871
3872 return Mask;
3873}
3874
Evan Cheng017dcc62006-04-21 01:05:10 +00003875/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3876/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003877/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003878static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003879 bool V2IsSplat = false, bool V2IsUndef = false) {
3880 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003881 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003882 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003883
Nate Begeman9008ca62009-04-27 18:41:29 +00003884 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003885 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003886
Nate Begeman9008ca62009-04-27 18:41:29 +00003887 for (int i = 1; i < NumOps; ++i)
3888 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3889 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3890 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003891 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003892
Evan Cheng39623da2006-04-20 08:58:49 +00003893 return true;
3894}
3895
Nate Begeman9008ca62009-04-27 18:41:29 +00003896static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003897 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003898 SmallVector<int, 8> M;
3899 N->getMask(M);
3900 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003901}
3902
Evan Chengd9539472006-04-14 21:59:03 +00003903/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3904/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003905/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3906bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3907 const X86Subtarget *Subtarget) {
3908 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003909 return false;
3910
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003911 // The second vector must be undef
3912 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3913 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003914
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003915 EVT VT = N->getValueType(0);
3916 unsigned NumElems = VT.getVectorNumElements();
3917
3918 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3919 (VT.getSizeInBits() == 256 && NumElems != 8))
3920 return false;
3921
3922 // "i+1" is the value the indexed mask element must have
3923 for (unsigned i = 0; i < NumElems; i += 2)
3924 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3925 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003926 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003927
3928 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003929}
3930
3931/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3932/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003933/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3934bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3935 const X86Subtarget *Subtarget) {
3936 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003937 return false;
3938
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003939 // The second vector must be undef
3940 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3941 return false;
3942
3943 EVT VT = N->getValueType(0);
3944 unsigned NumElems = VT.getVectorNumElements();
3945
3946 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3947 (VT.getSizeInBits() == 256 && NumElems != 8))
3948 return false;
3949
3950 // "i" is the value the indexed mask element must have
3951 for (unsigned i = 0; i < NumElems; i += 2)
3952 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3953 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003954 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003955
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003956 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003957}
3958
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003959/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3960/// specifies a shuffle of elements that is suitable for input to 256-bit
3961/// version of MOVDDUP.
3962static bool isMOVDDUPYMask(ShuffleVectorSDNode *N,
3963 const X86Subtarget *Subtarget) {
3964 EVT VT = N->getValueType(0);
3965 int NumElts = VT.getVectorNumElements();
3966 bool V2IsUndef = N->getOperand(1).getOpcode() == ISD::UNDEF;
3967
3968 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256 ||
3969 !V2IsUndef || NumElts != 4)
3970 return false;
3971
3972 for (int i = 0; i != NumElts/2; ++i)
3973 if (!isUndefOrEqual(N->getMaskElt(i), 0))
3974 return false;
3975 for (int i = NumElts/2; i != NumElts; ++i)
3976 if (!isUndefOrEqual(N->getMaskElt(i), NumElts/2))
3977 return false;
3978 return true;
3979}
3980
Evan Cheng0b457f02008-09-25 20:50:48 +00003981/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003982/// specifies a shuffle of elements that is suitable for input to 128-bit
3983/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003984bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003985 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003986
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003987 if (VT.getSizeInBits() != 128)
3988 return false;
3989
3990 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003991 for (int i = 0; i < e; ++i)
3992 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003993 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003994 for (int i = 0; i < e; ++i)
3995 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003996 return false;
3997 return true;
3998}
3999
David Greenec38a03e2011-02-03 15:50:00 +00004000/// isVEXTRACTF128Index - Return true if the specified
4001/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4002/// suitable for input to VEXTRACTF128.
4003bool X86::isVEXTRACTF128Index(SDNode *N) {
4004 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4005 return false;
4006
4007 // The index should be aligned on a 128-bit boundary.
4008 uint64_t Index =
4009 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4010
4011 unsigned VL = N->getValueType(0).getVectorNumElements();
4012 unsigned VBits = N->getValueType(0).getSizeInBits();
4013 unsigned ElSize = VBits / VL;
4014 bool Result = (Index * ElSize) % 128 == 0;
4015
4016 return Result;
4017}
4018
David Greeneccacdc12011-02-04 16:08:29 +00004019/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4020/// operand specifies a subvector insert that is suitable for input to
4021/// VINSERTF128.
4022bool X86::isVINSERTF128Index(SDNode *N) {
4023 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4024 return false;
4025
4026 // The index should be aligned on a 128-bit boundary.
4027 uint64_t Index =
4028 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4029
4030 unsigned VL = N->getValueType(0).getVectorNumElements();
4031 unsigned VBits = N->getValueType(0).getSizeInBits();
4032 unsigned ElSize = VBits / VL;
4033 bool Result = (Index * ElSize) % 128 == 0;
4034
4035 return Result;
4036}
4037
Evan Cheng63d33002006-03-22 08:01:21 +00004038/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004039/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00004040unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004041 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4042 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4043
Evan Chengb9df0ca2006-03-22 02:53:00 +00004044 unsigned Shift = (NumOperands == 4) ? 2 : 1;
4045 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00004046 for (int i = 0; i < NumOperands; ++i) {
4047 int Val = SVOp->getMaskElt(NumOperands-i-1);
4048 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00004049 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00004050 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00004051 if (i != NumOperands - 1)
4052 Mask <<= Shift;
4053 }
Evan Cheng63d33002006-03-22 08:01:21 +00004054 return Mask;
4055}
4056
Evan Cheng506d3df2006-03-29 23:07:14 +00004057/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004058/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004059unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004060 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004061 unsigned Mask = 0;
4062 // 8 nodes, but we only care about the last 4.
4063 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004064 int Val = SVOp->getMaskElt(i);
4065 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004066 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00004067 if (i != 4)
4068 Mask <<= 2;
4069 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004070 return Mask;
4071}
4072
4073/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004074/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004075unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004076 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004077 unsigned Mask = 0;
4078 // 8 nodes, but we only care about the first 4.
4079 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004080 int Val = SVOp->getMaskElt(i);
4081 if (Val >= 0)
4082 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004083 if (i != 0)
4084 Mask <<= 2;
4085 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004086 return Mask;
4087}
4088
Nate Begemana09008b2009-10-19 02:17:23 +00004089/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4090/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4091unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4092 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4093 EVT VVT = N->getValueType(0);
4094 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4095 int Val = 0;
4096
4097 unsigned i, e;
4098 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4099 Val = SVOp->getMaskElt(i);
4100 if (Val >= 0)
4101 break;
4102 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004103 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004104 return (Val - i) * EltSize;
4105}
4106
David Greenec38a03e2011-02-03 15:50:00 +00004107/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4108/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4109/// instructions.
4110unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4111 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4112 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4113
4114 uint64_t Index =
4115 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4116
4117 EVT VecVT = N->getOperand(0).getValueType();
4118 EVT ElVT = VecVT.getVectorElementType();
4119
4120 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004121 return Index / NumElemsPerChunk;
4122}
4123
David Greeneccacdc12011-02-04 16:08:29 +00004124/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4125/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4126/// instructions.
4127unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4128 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4129 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4130
4131 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004132 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004133
4134 EVT VecVT = N->getValueType(0);
4135 EVT ElVT = VecVT.getVectorElementType();
4136
4137 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004138 return Index / NumElemsPerChunk;
4139}
4140
Evan Cheng37b73872009-07-30 08:33:02 +00004141/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4142/// constant +0.0.
4143bool X86::isZeroNode(SDValue Elt) {
4144 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004145 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004146 (isa<ConstantFPSDNode>(Elt) &&
4147 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4148}
4149
Nate Begeman9008ca62009-04-27 18:41:29 +00004150/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4151/// their permute mask.
4152static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4153 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004154 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004155 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004156 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004157
Nate Begeman5a5ca152009-04-29 05:20:52 +00004158 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004159 int idx = SVOp->getMaskElt(i);
4160 if (idx < 0)
4161 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004162 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004163 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004164 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004165 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004166 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004167 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4168 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004169}
4170
Evan Cheng779ccea2007-12-07 21:30:01 +00004171/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
4172/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00004173static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004174 unsigned NumElems = VT.getVectorNumElements();
4175 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004176 int idx = Mask[i];
4177 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004178 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004179 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004180 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004181 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004182 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004183 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004184}
4185
Evan Cheng533a0aa2006-04-19 20:35:22 +00004186/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4187/// match movhlps. The lower half elements should come from upper half of
4188/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004189/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004190static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004191 EVT VT = Op->getValueType(0);
4192 if (VT.getSizeInBits() != 128)
4193 return false;
4194 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004195 return false;
4196 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004197 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004198 return false;
4199 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004200 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004201 return false;
4202 return true;
4203}
4204
Evan Cheng5ced1d82006-04-06 23:23:56 +00004205/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004206/// is promoted to a vector. It also returns the LoadSDNode by reference if
4207/// required.
4208static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004209 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4210 return false;
4211 N = N->getOperand(0).getNode();
4212 if (!ISD::isNON_EXTLoad(N))
4213 return false;
4214 if (LD)
4215 *LD = cast<LoadSDNode>(N);
4216 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004217}
4218
Evan Cheng533a0aa2006-04-19 20:35:22 +00004219/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4220/// match movlp{s|d}. The lower half elements should come from lower half of
4221/// V1 (and in order), and the upper half elements should come from the upper
4222/// half of V2 (and in order). And since V1 will become the source of the
4223/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004224static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4225 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004226 EVT VT = Op->getValueType(0);
4227 if (VT.getSizeInBits() != 128)
4228 return false;
4229
Evan Cheng466685d2006-10-09 20:57:25 +00004230 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004231 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004232 // Is V2 is a vector load, don't do this transformation. We will try to use
4233 // load folding shufps op.
4234 if (ISD::isNON_EXTLoad(V2))
4235 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004236
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004237 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004238
Evan Cheng533a0aa2006-04-19 20:35:22 +00004239 if (NumElems != 2 && NumElems != 4)
4240 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004241 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004242 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004243 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004244 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004245 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004246 return false;
4247 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004248}
4249
Evan Cheng39623da2006-04-20 08:58:49 +00004250/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4251/// all the same.
4252static bool isSplatVector(SDNode *N) {
4253 if (N->getOpcode() != ISD::BUILD_VECTOR)
4254 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004255
Dan Gohman475871a2008-07-27 21:46:04 +00004256 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004257 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4258 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004259 return false;
4260 return true;
4261}
4262
Evan Cheng213d2cf2007-05-17 18:45:50 +00004263/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004264/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004265/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004266static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004267 SDValue V1 = N->getOperand(0);
4268 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004269 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4270 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004271 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004272 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004273 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004274 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4275 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004276 if (Opc != ISD::BUILD_VECTOR ||
4277 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004278 return false;
4279 } else if (Idx >= 0) {
4280 unsigned Opc = V1.getOpcode();
4281 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4282 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004283 if (Opc != ISD::BUILD_VECTOR ||
4284 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004285 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004286 }
4287 }
4288 return true;
4289}
4290
4291/// getZeroVector - Returns a vector of specified type with all zero elements.
4292///
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004293static SDValue getZeroVector(EVT VT, bool HasXMMInt, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004294 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004295 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004296
Dale Johannesen0488fb62010-09-30 23:57:10 +00004297 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004298 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004299 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004300 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004301 if (HasXMMInt) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004302 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4303 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4304 } else { // SSE1
4305 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4306 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4307 }
4308 } else if (VT.getSizeInBits() == 256) { // AVX
4309 // 256-bit logic and arithmetic instructions in AVX are
4310 // all floating-point, no support for integer ops. Default
4311 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004312 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004313 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4314 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004315 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004316 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004317}
4318
Chris Lattner8a594482007-11-25 00:24:49 +00004319/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004320/// Always build ones vectors as <4 x i32>. For 256-bit types, use two
4321/// <4 x i32> inserted in a <8 x i32> appropriately. Then bitcast to their
4322/// original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00004323static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004324 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004325 assert((VT.is128BitVector() || VT.is256BitVector())
4326 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004327
Owen Anderson825b72b2009-08-11 20:47:22 +00004328 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004329 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4330 Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004331
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004332 if (VT.is256BitVector()) {
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004333 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4334 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4335 Vec = Insert128BitVector(InsV, Vec,
4336 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4337 }
4338
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004339 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004340}
4341
Evan Cheng39623da2006-04-20 08:58:49 +00004342/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4343/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004344static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004345 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004346 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004347
Evan Cheng39623da2006-04-20 08:58:49 +00004348 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004349 SmallVector<int, 8> MaskVec;
4350 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004351
Nate Begeman5a5ca152009-04-29 05:20:52 +00004352 for (unsigned i = 0; i != NumElems; ++i) {
4353 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004354 MaskVec[i] = NumElems;
4355 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004356 }
Evan Cheng39623da2006-04-20 08:58:49 +00004357 }
Evan Cheng39623da2006-04-20 08:58:49 +00004358 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004359 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4360 SVOp->getOperand(1), &MaskVec[0]);
4361 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004362}
4363
Evan Cheng017dcc62006-04-21 01:05:10 +00004364/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4365/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004366static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004367 SDValue V2) {
4368 unsigned NumElems = VT.getVectorNumElements();
4369 SmallVector<int, 8> Mask;
4370 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004371 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004372 Mask.push_back(i);
4373 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004374}
4375
Nate Begeman9008ca62009-04-27 18:41:29 +00004376/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004377static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004378 SDValue V2) {
4379 unsigned NumElems = VT.getVectorNumElements();
4380 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004381 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004382 Mask.push_back(i);
4383 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004384 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004385 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004386}
4387
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004388/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004389static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004390 SDValue V2) {
4391 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004392 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004393 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004394 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004395 Mask.push_back(i + Half);
4396 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004397 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004398 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004399}
4400
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004401// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004402// a generic shuffle instruction because the target has no such instructions.
4403// Generate shuffles which repeat i16 and i8 several times until they can be
4404// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004405static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004406 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004407 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004408 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004409
Nate Begeman9008ca62009-04-27 18:41:29 +00004410 while (NumElems > 4) {
4411 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004412 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004413 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004414 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004415 EltNo -= NumElems/2;
4416 }
4417 NumElems >>= 1;
4418 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004419 return V;
4420}
Eric Christopherfd179292009-08-27 18:07:15 +00004421
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004422/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4423static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4424 EVT VT = V.getValueType();
4425 DebugLoc dl = V.getDebugLoc();
4426 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4427 && "Vector size not supported");
4428
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004429 if (VT.getSizeInBits() == 128) {
4430 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004431 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004432 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4433 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004434 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004435 // To use VPERMILPS to splat scalars, the second half of indicies must
4436 // refer to the higher part, which is a duplication of the lower one,
4437 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004438 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4439 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004440
4441 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4442 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4443 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004444 }
4445
4446 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4447}
4448
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004449/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004450static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4451 EVT SrcVT = SV->getValueType(0);
4452 SDValue V1 = SV->getOperand(0);
4453 DebugLoc dl = SV->getDebugLoc();
4454
4455 int EltNo = SV->getSplatIndex();
4456 int NumElems = SrcVT.getVectorNumElements();
4457 unsigned Size = SrcVT.getSizeInBits();
4458
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004459 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4460 "Unknown how to promote splat for type");
4461
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004462 // Extract the 128-bit part containing the splat element and update
4463 // the splat element index when it refers to the higher register.
4464 if (Size == 256) {
4465 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4466 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4467 if (Idx > 0)
4468 EltNo -= NumElems/2;
4469 }
4470
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004471 // All i16 and i8 vector types can't be used directly by a generic shuffle
4472 // instruction because the target has no such instruction. Generate shuffles
4473 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004474 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004475 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004476 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004477 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004478
4479 // Recreate the 256-bit vector and place the same 128-bit vector
4480 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004481 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004482 if (Size == 256) {
4483 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4484 DAG.getConstant(0, MVT::i32), DAG, dl);
4485 V1 = Insert128BitVector(InsV, V1,
4486 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4487 }
4488
4489 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004490}
4491
Evan Chengba05f722006-04-21 23:03:30 +00004492/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004493/// vector of zero or undef vector. This produces a shuffle where the low
4494/// element of V2 is swizzled into the zero/undef vector, landing at element
4495/// 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 +00004496static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004497 bool isZero, bool HasXMMInt,
4498 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004499 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004500 SDValue V1 = isZero
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004501 ? getZeroVector(VT, HasXMMInt, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004502 unsigned NumElems = VT.getVectorNumElements();
4503 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004504 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004505 // If this is the insertion idx, put the low elt of V2 here.
4506 MaskVec.push_back(i == Idx ? NumElems : i);
4507 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004508}
4509
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004510/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4511/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004512static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4513 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004514 if (Depth == 6)
4515 return SDValue(); // Limit search depth.
4516
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004517 SDValue V = SDValue(N, 0);
4518 EVT VT = V.getValueType();
4519 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004520
4521 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4522 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4523 Index = SV->getMaskElt(Index);
4524
4525 if (Index < 0)
4526 return DAG.getUNDEF(VT.getVectorElementType());
4527
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004528 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004529 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004530 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004531 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004532
4533 // Recurse into target specific vector shuffles to find scalars.
4534 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004535 int NumElems = VT.getVectorNumElements();
4536 SmallVector<unsigned, 16> ShuffleMask;
4537 SDValue ImmN;
4538
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004539 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004540 case X86ISD::SHUFPS:
4541 case X86ISD::SHUFPD:
4542 ImmN = N->getOperand(N->getNumOperands()-1);
4543 DecodeSHUFPSMask(NumElems,
4544 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4545 ShuffleMask);
4546 break;
4547 case X86ISD::PUNPCKHBW:
4548 case X86ISD::PUNPCKHWD:
4549 case X86ISD::PUNPCKHDQ:
4550 case X86ISD::PUNPCKHQDQ:
4551 DecodePUNPCKHMask(NumElems, ShuffleMask);
4552 break;
4553 case X86ISD::UNPCKHPS:
4554 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00004555 case X86ISD::VUNPCKHPSY:
4556 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004557 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4558 break;
4559 case X86ISD::PUNPCKLBW:
4560 case X86ISD::PUNPCKLWD:
4561 case X86ISD::PUNPCKLDQ:
4562 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004563 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004564 break;
4565 case X86ISD::UNPCKLPS:
4566 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004567 case X86ISD::VUNPCKLPSY:
4568 case X86ISD::VUNPCKLPDY:
4569 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004570 break;
4571 case X86ISD::MOVHLPS:
4572 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4573 break;
4574 case X86ISD::MOVLHPS:
4575 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4576 break;
4577 case X86ISD::PSHUFD:
4578 ImmN = N->getOperand(N->getNumOperands()-1);
4579 DecodePSHUFMask(NumElems,
4580 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4581 ShuffleMask);
4582 break;
4583 case X86ISD::PSHUFHW:
4584 ImmN = N->getOperand(N->getNumOperands()-1);
4585 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4586 ShuffleMask);
4587 break;
4588 case X86ISD::PSHUFLW:
4589 ImmN = N->getOperand(N->getNumOperands()-1);
4590 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4591 ShuffleMask);
4592 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004593 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004594 case X86ISD::MOVSD: {
4595 // The index 0 always comes from the first element of the second source,
4596 // this is why MOVSS and MOVSD are used in the first place. The other
4597 // elements come from the other positions of the first source vector.
4598 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004599 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4600 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004601 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004602 case X86ISD::VPERMILPS:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004603 ImmN = N->getOperand(N->getNumOperands()-1);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004604 DecodeVPERMILPSMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004605 ShuffleMask);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004606 break;
4607 case X86ISD::VPERMILPSY:
4608 ImmN = N->getOperand(N->getNumOperands()-1);
4609 DecodeVPERMILPSMask(8, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4610 ShuffleMask);
4611 break;
4612 case X86ISD::VPERMILPD:
4613 ImmN = N->getOperand(N->getNumOperands()-1);
4614 DecodeVPERMILPDMask(2, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4615 ShuffleMask);
4616 break;
4617 case X86ISD::VPERMILPDY:
4618 ImmN = N->getOperand(N->getNumOperands()-1);
4619 DecodeVPERMILPDMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4620 ShuffleMask);
4621 break;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004622 case X86ISD::VPERM2F128:
4623 ImmN = N->getOperand(N->getNumOperands()-1);
4624 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4625 ShuffleMask);
4626 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004627 case X86ISD::MOVDDUP:
4628 case X86ISD::MOVLHPD:
4629 case X86ISD::MOVLPD:
4630 case X86ISD::MOVLPS:
4631 case X86ISD::MOVSHDUP:
4632 case X86ISD::MOVSLDUP:
4633 case X86ISD::PALIGN:
4634 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004635 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004636 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004637 return SDValue();
4638 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004639
4640 Index = ShuffleMask[Index];
4641 if (Index < 0)
4642 return DAG.getUNDEF(VT.getVectorElementType());
4643
4644 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4645 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4646 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004647 }
4648
4649 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004650 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004651 V = V.getOperand(0);
4652 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004653 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004654
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004655 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004656 return SDValue();
4657 }
4658
4659 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4660 return (Index == 0) ? V.getOperand(0)
4661 : DAG.getUNDEF(VT.getVectorElementType());
4662
4663 if (V.getOpcode() == ISD::BUILD_VECTOR)
4664 return V.getOperand(Index);
4665
4666 return SDValue();
4667}
4668
4669/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4670/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004671/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004672static
4673unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4674 bool ZerosFromLeft, SelectionDAG &DAG) {
4675 int i = 0;
4676
4677 while (i < NumElems) {
4678 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004679 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004680 if (!(Elt.getNode() &&
4681 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4682 break;
4683 ++i;
4684 }
4685
4686 return i;
4687}
4688
4689/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4690/// MaskE correspond consecutively to elements from one of the vector operands,
4691/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4692static
4693bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4694 int OpIdx, int NumElems, unsigned &OpNum) {
4695 bool SeenV1 = false;
4696 bool SeenV2 = false;
4697
4698 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4699 int Idx = SVOp->getMaskElt(i);
4700 // Ignore undef indicies
4701 if (Idx < 0)
4702 continue;
4703
4704 if (Idx < NumElems)
4705 SeenV1 = true;
4706 else
4707 SeenV2 = true;
4708
4709 // Only accept consecutive elements from the same vector
4710 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4711 return false;
4712 }
4713
4714 OpNum = SeenV1 ? 0 : 1;
4715 return true;
4716}
4717
4718/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4719/// logical left shift of a vector.
4720static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4721 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4722 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4723 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4724 false /* check zeros from right */, DAG);
4725 unsigned OpSrc;
4726
4727 if (!NumZeros)
4728 return false;
4729
4730 // Considering the elements in the mask that are not consecutive zeros,
4731 // check if they consecutively come from only one of the source vectors.
4732 //
4733 // V1 = {X, A, B, C} 0
4734 // \ \ \ /
4735 // vector_shuffle V1, V2 <1, 2, 3, X>
4736 //
4737 if (!isShuffleMaskConsecutive(SVOp,
4738 0, // Mask Start Index
4739 NumElems-NumZeros-1, // Mask End Index
4740 NumZeros, // Where to start looking in the src vector
4741 NumElems, // Number of elements in vector
4742 OpSrc)) // Which source operand ?
4743 return false;
4744
4745 isLeft = false;
4746 ShAmt = NumZeros;
4747 ShVal = SVOp->getOperand(OpSrc);
4748 return true;
4749}
4750
4751/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4752/// logical left shift of a vector.
4753static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4754 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4755 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4756 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4757 true /* check zeros from left */, DAG);
4758 unsigned OpSrc;
4759
4760 if (!NumZeros)
4761 return false;
4762
4763 // Considering the elements in the mask that are not consecutive zeros,
4764 // check if they consecutively come from only one of the source vectors.
4765 //
4766 // 0 { A, B, X, X } = V2
4767 // / \ / /
4768 // vector_shuffle V1, V2 <X, X, 4, 5>
4769 //
4770 if (!isShuffleMaskConsecutive(SVOp,
4771 NumZeros, // Mask Start Index
4772 NumElems-1, // Mask End Index
4773 0, // Where to start looking in the src vector
4774 NumElems, // Number of elements in vector
4775 OpSrc)) // Which source operand ?
4776 return false;
4777
4778 isLeft = true;
4779 ShAmt = NumZeros;
4780 ShVal = SVOp->getOperand(OpSrc);
4781 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004782}
4783
4784/// isVectorShift - Returns true if the shuffle can be implemented as a
4785/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004786static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004787 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004788 // Although the logic below support any bitwidth size, there are no
4789 // shift instructions which handle more than 128-bit vectors.
4790 if (SVOp->getValueType(0).getSizeInBits() > 128)
4791 return false;
4792
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004793 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4794 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4795 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004796
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004797 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004798}
4799
Evan Chengc78d3b42006-04-24 18:01:45 +00004800/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4801///
Dan Gohman475871a2008-07-27 21:46:04 +00004802static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004803 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004804 SelectionDAG &DAG,
4805 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004806 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004807 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004808
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004809 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004810 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004811 bool First = true;
4812 for (unsigned i = 0; i < 16; ++i) {
4813 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4814 if (ThisIsNonZero && First) {
4815 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004816 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004817 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004818 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004819 First = false;
4820 }
4821
4822 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004823 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004824 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4825 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004826 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004827 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004828 }
4829 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004830 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4831 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4832 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004833 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004834 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004835 } else
4836 ThisElt = LastElt;
4837
Gabor Greifba36cb52008-08-28 21:40:38 +00004838 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004839 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004840 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004841 }
4842 }
4843
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004844 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004845}
4846
Bill Wendlinga348c562007-03-22 18:42:45 +00004847/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004848///
Dan Gohman475871a2008-07-27 21:46:04 +00004849static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004850 unsigned NumNonZero, unsigned NumZero,
4851 SelectionDAG &DAG,
4852 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004853 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004854 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004855
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004856 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004857 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004858 bool First = true;
4859 for (unsigned i = 0; i < 8; ++i) {
4860 bool isNonZero = (NonZeros & (1 << i)) != 0;
4861 if (isNonZero) {
4862 if (First) {
4863 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004864 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004865 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004866 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004867 First = false;
4868 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004869 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004870 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004871 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004872 }
4873 }
4874
4875 return V;
4876}
4877
Evan Chengf26ffe92008-05-29 08:22:04 +00004878/// getVShift - Return a vector logical shift node.
4879///
Owen Andersone50ed302009-08-10 22:56:29 +00004880static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004881 unsigned NumBits, SelectionDAG &DAG,
4882 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004883 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004884 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004885 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004886 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4887 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004888 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004889 DAG.getConstant(NumBits,
4890 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004891}
4892
Dan Gohman475871a2008-07-27 21:46:04 +00004893SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004894X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004895 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004896
Evan Chengc3630942009-12-09 21:00:30 +00004897 // Check if the scalar load can be widened into a vector load. And if
4898 // the address is "base + cst" see if the cst can be "absorbed" into
4899 // the shuffle mask.
4900 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4901 SDValue Ptr = LD->getBasePtr();
4902 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4903 return SDValue();
4904 EVT PVT = LD->getValueType(0);
4905 if (PVT != MVT::i32 && PVT != MVT::f32)
4906 return SDValue();
4907
4908 int FI = -1;
4909 int64_t Offset = 0;
4910 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4911 FI = FINode->getIndex();
4912 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004913 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004914 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4915 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4916 Offset = Ptr.getConstantOperandVal(1);
4917 Ptr = Ptr.getOperand(0);
4918 } else {
4919 return SDValue();
4920 }
4921
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004922 // FIXME: 256-bit vector instructions don't require a strict alignment,
4923 // improve this code to support it better.
4924 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004925 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004926 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004927 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004928 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004929 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004930 // Can't change the alignment. FIXME: It's possible to compute
4931 // the exact stack offset and reference FI + adjust offset instead.
4932 // If someone *really* cares about this. That's the way to implement it.
4933 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004934 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004935 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004936 }
4937 }
4938
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004939 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004940 // Ptr + (Offset & ~15).
4941 if (Offset < 0)
4942 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004943 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004944 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004945 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004946 if (StartOffset)
4947 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4948 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4949
4950 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004951 int NumElems = VT.getVectorNumElements();
4952
4953 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4954 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4955 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004956 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004957 false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004958
4959 // Canonicalize it to a v4i32 or v8i32 shuffle.
4960 SmallVector<int, 8> Mask;
4961 for (int i = 0; i < NumElems; ++i)
4962 Mask.push_back(EltNo);
4963
4964 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4965 return DAG.getNode(ISD::BITCAST, dl, NVT,
4966 DAG.getVectorShuffle(CanonVT, dl, V1,
4967 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004968 }
4969
4970 return SDValue();
4971}
4972
Michael J. Spencerec38de22010-10-10 22:04:20 +00004973/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4974/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004975/// load which has the same value as a build_vector whose operands are 'elts'.
4976///
4977/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004978///
Nate Begeman1449f292010-03-24 22:19:06 +00004979/// FIXME: we'd also like to handle the case where the last elements are zero
4980/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4981/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004982static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004983 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004984 EVT EltVT = VT.getVectorElementType();
4985 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004986
Nate Begemanfdea31a2010-03-24 20:49:50 +00004987 LoadSDNode *LDBase = NULL;
4988 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004989
Nate Begeman1449f292010-03-24 22:19:06 +00004990 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004991 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004992 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004993 for (unsigned i = 0; i < NumElems; ++i) {
4994 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004995
Nate Begemanfdea31a2010-03-24 20:49:50 +00004996 if (!Elt.getNode() ||
4997 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4998 return SDValue();
4999 if (!LDBase) {
5000 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5001 return SDValue();
5002 LDBase = cast<LoadSDNode>(Elt.getNode());
5003 LastLoadedElt = i;
5004 continue;
5005 }
5006 if (Elt.getOpcode() == ISD::UNDEF)
5007 continue;
5008
5009 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5010 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5011 return SDValue();
5012 LastLoadedElt = i;
5013 }
Nate Begeman1449f292010-03-24 22:19:06 +00005014
5015 // If we have found an entire vector of loads and undefs, then return a large
5016 // load of the entire vector width starting at the base pointer. If we found
5017 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005018 if (LastLoadedElt == NumElems - 1) {
5019 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005020 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005021 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005022 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005023 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005024 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005025 LDBase->isVolatile(), LDBase->isNonTemporal(),
5026 LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00005027 } else if (NumElems == 4 && LastLoadedElt == 1 &&
5028 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005029 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5030 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005031 SDValue ResNode =
5032 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5033 LDBase->getPointerInfo(),
5034 LDBase->getAlignment(),
5035 false/*isVolatile*/, true/*ReadMem*/,
5036 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005037 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005038 }
5039 return SDValue();
5040}
5041
Evan Chengc3630942009-12-09 21:00:30 +00005042SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005043X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005044 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005045
David Greenef125a292011-02-08 19:04:41 +00005046 EVT VT = Op.getValueType();
5047 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005048 unsigned NumElems = Op.getNumOperands();
5049
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005050 // Vectors containing all zeros can be matched by pxor and xorps later
5051 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5052 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5053 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005054 if (Op.getValueType() == MVT::v4i32 ||
5055 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005056 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005057
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005058 return getZeroVector(Op.getValueType(), Subtarget->hasXMMInt(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005059 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005060
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005061 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
5062 // vectors or broken into v4i32 operations on 256-bit vectors.
5063 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
5064 if (Op.getValueType() == MVT::v4i32)
5065 return Op;
5066
5067 return getOnesVector(Op.getValueType(), DAG, dl);
5068 }
5069
Owen Andersone50ed302009-08-10 22:56:29 +00005070 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005071
Evan Cheng0db9fe62006-04-25 20:13:52 +00005072 unsigned NumZero = 0;
5073 unsigned NumNonZero = 0;
5074 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005075 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005076 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005077 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005078 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005079 if (Elt.getOpcode() == ISD::UNDEF)
5080 continue;
5081 Values.insert(Elt);
5082 if (Elt.getOpcode() != ISD::Constant &&
5083 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005084 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005085 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005086 NumZero++;
5087 else {
5088 NonZeros |= (1 << i);
5089 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005090 }
5091 }
5092
Chris Lattner97a2a562010-08-26 05:24:29 +00005093 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5094 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005095 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005096
Chris Lattner67f453a2008-03-09 05:42:06 +00005097 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005098 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005099 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005100 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005101
Chris Lattner62098042008-03-09 01:05:04 +00005102 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5103 // the value are obviously zero, truncate the value to i32 and do the
5104 // insertion that way. Only do this if the value is non-constant or if the
5105 // value is a constant being inserted into element 0. It is cheaper to do
5106 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005107 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005108 (!IsAllConstants || Idx == 0)) {
5109 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005110 // Handle SSE only.
5111 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5112 EVT VecVT = MVT::v4i32;
5113 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005114
Chris Lattner62098042008-03-09 01:05:04 +00005115 // Truncate the value (which may itself be a constant) to i32, and
5116 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005117 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005118 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005119 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005120 Subtarget->hasXMMInt(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005121
Chris Lattner62098042008-03-09 01:05:04 +00005122 // Now we have our 32-bit value zero extended in the low element of
5123 // a vector. If Idx != 0, swizzle it into place.
5124 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005125 SmallVector<int, 4> Mask;
5126 Mask.push_back(Idx);
5127 for (unsigned i = 1; i != VecElts; ++i)
5128 Mask.push_back(i);
5129 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005130 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005131 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005132 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005133 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005134 }
5135 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005136
Chris Lattner19f79692008-03-08 22:59:52 +00005137 // If we have a constant or non-constant insertion into the low element of
5138 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5139 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005140 // depending on what the source datatype is.
5141 if (Idx == 0) {
5142 if (NumZero == 0) {
5143 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005144 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5145 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005146 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5147 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005148 return getShuffleVectorZeroOrUndef(Item, 0, true,Subtarget->hasXMMInt(),
Eli Friedman10415532009-06-06 06:05:10 +00005149 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005150 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5151 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005152 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5153 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005154 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5155 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005156 Subtarget->hasXMMInt(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005157 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005158 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005159 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005160
5161 // Is it a vector logical left shift?
5162 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005163 X86::isZeroNode(Op.getOperand(0)) &&
5164 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005165 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005166 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005167 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005168 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005169 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005170 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005171
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005172 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005173 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005174
Chris Lattner19f79692008-03-08 22:59:52 +00005175 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5176 // is a non-constant being inserted into an element other than the low one,
5177 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5178 // movd/movss) to move this into the low element, then shuffle it into
5179 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005180 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005181 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005182
Evan Cheng0db9fe62006-04-25 20:13:52 +00005183 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005184 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005185 Subtarget->hasXMMInt(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005186 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005187 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005188 MaskVec.push_back(i == Idx ? 0 : 1);
5189 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005190 }
5191 }
5192
Chris Lattner67f453a2008-03-09 05:42:06 +00005193 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005194 if (Values.size() == 1) {
5195 if (EVTBits == 32) {
5196 // Instead of a shuffle like this:
5197 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5198 // Check if it's possible to issue this instead.
5199 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5200 unsigned Idx = CountTrailingZeros_32(NonZeros);
5201 SDValue Item = Op.getOperand(Idx);
5202 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5203 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5204 }
Dan Gohman475871a2008-07-27 21:46:04 +00005205 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005206 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005207
Dan Gohmana3941172007-07-24 22:55:08 +00005208 // A vector full of immediates; various special cases are already
5209 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005210 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005211 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005212
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005213 // For AVX-length vectors, build the individual 128-bit pieces and use
5214 // shuffles to put them in place.
5215 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5216 SmallVector<SDValue, 32> V;
5217 for (unsigned i = 0; i < NumElems; ++i)
5218 V.push_back(Op.getOperand(i));
5219
5220 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5221
5222 // Build both the lower and upper subvector.
5223 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5224 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5225 NumElems/2);
5226
5227 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005228 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5229 DAG.getConstant(0, MVT::i32), DAG, dl);
5230 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005231 DAG, dl);
5232 }
5233
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005234 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005235 if (EVTBits == 64) {
5236 if (NumNonZero == 1) {
5237 // One half is zero or undef.
5238 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005239 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005240 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005241 return getShuffleVectorZeroOrUndef(V2, Idx, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005242 Subtarget->hasXMMInt(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005243 }
Dan Gohman475871a2008-07-27 21:46:04 +00005244 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005245 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005246
5247 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005248 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005249 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005250 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005251 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005252 }
5253
Bill Wendling826f36f2007-03-28 00:57:11 +00005254 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005255 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005256 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005257 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005258 }
5259
5260 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005261 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005262 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005263 if (NumElems == 4 && NumZero > 0) {
5264 for (unsigned i = 0; i < 4; ++i) {
5265 bool isZero = !(NonZeros & (1 << i));
5266 if (isZero)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005267 V[i] = getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005268 else
Dale Johannesenace16102009-02-03 19:33:06 +00005269 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005270 }
5271
5272 for (unsigned i = 0; i < 2; ++i) {
5273 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5274 default: break;
5275 case 0:
5276 V[i] = V[i*2]; // Must be a zero vector.
5277 break;
5278 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005279 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005280 break;
5281 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005282 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005283 break;
5284 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005285 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005286 break;
5287 }
5288 }
5289
Nate Begeman9008ca62009-04-27 18:41:29 +00005290 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005291 bool Reverse = (NonZeros & 0x3) == 2;
5292 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005293 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005294 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5295 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005296 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5297 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005298 }
5299
Nate Begemanfdea31a2010-03-24 20:49:50 +00005300 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5301 // Check for a build vector of consecutive loads.
5302 for (unsigned i = 0; i < NumElems; ++i)
5303 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005304
Nate Begemanfdea31a2010-03-24 20:49:50 +00005305 // Check for elements which are consecutive loads.
5306 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5307 if (LD.getNode())
5308 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005309
5310 // For SSE 4.1, use insertps to put the high elements into the low element.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005311 if (getSubtarget()->hasSSE41() || getSubtarget()->hasAVX()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005312 SDValue Result;
5313 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5314 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5315 else
5316 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005317
Chris Lattner24faf612010-08-28 17:59:08 +00005318 for (unsigned i = 1; i < NumElems; ++i) {
5319 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5320 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005321 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005322 }
5323 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005324 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005325
Chris Lattner6e80e442010-08-28 17:15:43 +00005326 // Otherwise, expand into a number of unpckl*, start by extending each of
5327 // our (non-undef) elements to the full vector width with the element in the
5328 // bottom slot of the vector (which generates no code for SSE).
5329 for (unsigned i = 0; i < NumElems; ++i) {
5330 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5331 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5332 else
5333 V[i] = DAG.getUNDEF(VT);
5334 }
5335
5336 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005337 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5338 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5339 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005340 unsigned EltStride = NumElems >> 1;
5341 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005342 for (unsigned i = 0; i < EltStride; ++i) {
5343 // If V[i+EltStride] is undef and this is the first round of mixing,
5344 // then it is safe to just drop this shuffle: V[i] is already in the
5345 // right place, the one element (since it's the first round) being
5346 // inserted as undef can be dropped. This isn't safe for successive
5347 // rounds because they will permute elements within both vectors.
5348 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5349 EltStride == NumElems/2)
5350 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005351
Chris Lattner6e80e442010-08-28 17:15:43 +00005352 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005353 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005354 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005355 }
5356 return V[0];
5357 }
Dan Gohman475871a2008-07-27 21:46:04 +00005358 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005359}
5360
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005361// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5362// them in a MMX register. This is better than doing a stack convert.
5363static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005364 DebugLoc dl = Op.getDebugLoc();
5365 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005366
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005367 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5368 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5369 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005370 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005371 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5372 InVec = Op.getOperand(1);
5373 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5374 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005375 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005376 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5377 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5378 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005379 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005380 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5381 Mask[0] = 0; Mask[1] = 2;
5382 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5383 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005384 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005385}
5386
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005387// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5388// to create 256-bit vectors from two other 128-bit ones.
5389static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5390 DebugLoc dl = Op.getDebugLoc();
5391 EVT ResVT = Op.getValueType();
5392
5393 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5394
5395 SDValue V1 = Op.getOperand(0);
5396 SDValue V2 = Op.getOperand(1);
5397 unsigned NumElems = ResVT.getVectorNumElements();
5398
5399 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5400 DAG.getConstant(0, MVT::i32), DAG, dl);
5401 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5402 DAG, dl);
5403}
5404
5405SDValue
5406X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005407 EVT ResVT = Op.getValueType();
5408
5409 assert(Op.getNumOperands() == 2);
5410 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5411 "Unsupported CONCAT_VECTORS for value type");
5412
5413 // We support concatenate two MMX registers and place them in a MMX register.
5414 // This is better than doing a stack convert.
5415 if (ResVT.is128BitVector())
5416 return LowerMMXCONCAT_VECTORS(Op, DAG);
5417
5418 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5419 // from two other 128-bit ones.
5420 return LowerAVXCONCAT_VECTORS(Op, DAG);
5421}
5422
Nate Begemanb9a47b82009-02-23 08:49:38 +00005423// v8i16 shuffles - Prefer shuffles in the following order:
5424// 1. [all] pshuflw, pshufhw, optional move
5425// 2. [ssse3] 1 x pshufb
5426// 3. [ssse3] 2 x pshufb + 1 x por
5427// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005428SDValue
5429X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5430 SelectionDAG &DAG) const {
5431 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005432 SDValue V1 = SVOp->getOperand(0);
5433 SDValue V2 = SVOp->getOperand(1);
5434 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005435 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005436
Nate Begemanb9a47b82009-02-23 08:49:38 +00005437 // Determine if more than 1 of the words in each of the low and high quadwords
5438 // of the result come from the same quadword of one of the two inputs. Undef
5439 // mask values count as coming from any quadword, for better codegen.
5440 SmallVector<unsigned, 4> LoQuad(4);
5441 SmallVector<unsigned, 4> HiQuad(4);
5442 BitVector InputQuads(4);
5443 for (unsigned i = 0; i < 8; ++i) {
5444 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005445 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005446 MaskVals.push_back(EltIdx);
5447 if (EltIdx < 0) {
5448 ++Quad[0];
5449 ++Quad[1];
5450 ++Quad[2];
5451 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005452 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005453 }
5454 ++Quad[EltIdx / 4];
5455 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005456 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005457
Nate Begemanb9a47b82009-02-23 08:49:38 +00005458 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005459 unsigned MaxQuad = 1;
5460 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005461 if (LoQuad[i] > MaxQuad) {
5462 BestLoQuad = i;
5463 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005464 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005465 }
5466
Nate Begemanb9a47b82009-02-23 08:49:38 +00005467 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005468 MaxQuad = 1;
5469 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005470 if (HiQuad[i] > MaxQuad) {
5471 BestHiQuad = i;
5472 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005473 }
5474 }
5475
Nate Begemanb9a47b82009-02-23 08:49:38 +00005476 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005477 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005478 // single pshufb instruction is necessary. If There are more than 2 input
5479 // quads, disable the next transformation since it does not help SSSE3.
5480 bool V1Used = InputQuads[0] || InputQuads[1];
5481 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005482 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005483 if (InputQuads.count() == 2 && V1Used && V2Used) {
5484 BestLoQuad = InputQuads.find_first();
5485 BestHiQuad = InputQuads.find_next(BestLoQuad);
5486 }
5487 if (InputQuads.count() > 2) {
5488 BestLoQuad = -1;
5489 BestHiQuad = -1;
5490 }
5491 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005492
Nate Begemanb9a47b82009-02-23 08:49:38 +00005493 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5494 // the shuffle mask. If a quad is scored as -1, that means that it contains
5495 // words from all 4 input quadwords.
5496 SDValue NewV;
5497 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005498 SmallVector<int, 8> MaskV;
5499 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5500 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005501 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005502 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5503 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5504 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005505
Nate Begemanb9a47b82009-02-23 08:49:38 +00005506 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5507 // source words for the shuffle, to aid later transformations.
5508 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005509 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005510 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005511 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005512 if (idx != (int)i)
5513 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005514 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005515 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005516 AllWordsInNewV = false;
5517 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005518 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005519
Nate Begemanb9a47b82009-02-23 08:49:38 +00005520 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5521 if (AllWordsInNewV) {
5522 for (int i = 0; i != 8; ++i) {
5523 int idx = MaskVals[i];
5524 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005525 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005526 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005527 if ((idx != i) && idx < 4)
5528 pshufhw = false;
5529 if ((idx != i) && idx > 3)
5530 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005531 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005532 V1 = NewV;
5533 V2Used = false;
5534 BestLoQuad = 0;
5535 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005536 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005537
Nate Begemanb9a47b82009-02-23 08:49:38 +00005538 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5539 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005540 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005541 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5542 unsigned TargetMask = 0;
5543 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005544 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005545 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5546 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5547 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005548 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005549 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005550 }
Eric Christopherfd179292009-08-27 18:07:15 +00005551
Nate Begemanb9a47b82009-02-23 08:49:38 +00005552 // If we have SSSE3, and all words of the result are from 1 input vector,
5553 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5554 // is present, fall back to case 4.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005555 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005556 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005557
Nate Begemanb9a47b82009-02-23 08:49:38 +00005558 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005559 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005560 // mask, and elements that come from V1 in the V2 mask, so that the two
5561 // results can be OR'd together.
5562 bool TwoInputs = V1Used && V2Used;
5563 for (unsigned i = 0; i != 8; ++i) {
5564 int EltIdx = MaskVals[i] * 2;
5565 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005566 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5567 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005568 continue;
5569 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005570 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5571 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005572 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005573 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005574 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005575 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005576 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005577 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005578 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005579
Nate Begemanb9a47b82009-02-23 08:49:38 +00005580 // Calculate the shuffle mask for the second input, shuffle it, and
5581 // OR it with the first shuffled input.
5582 pshufbMask.clear();
5583 for (unsigned i = 0; i != 8; ++i) {
5584 int EltIdx = MaskVals[i] * 2;
5585 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005586 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5587 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005588 continue;
5589 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005590 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5591 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005592 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005593 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005594 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005595 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005596 MVT::v16i8, &pshufbMask[0], 16));
5597 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005598 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005599 }
5600
5601 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5602 // and update MaskVals with new element order.
5603 BitVector InOrder(8);
5604 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005605 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005606 for (int i = 0; i != 4; ++i) {
5607 int idx = MaskVals[i];
5608 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005609 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005610 InOrder.set(i);
5611 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005612 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005613 InOrder.set(i);
5614 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005615 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005616 }
5617 }
5618 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005619 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005620 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005621 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005622
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005623 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5624 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005625 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5626 NewV.getOperand(0),
5627 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5628 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005629 }
Eric Christopherfd179292009-08-27 18:07:15 +00005630
Nate Begemanb9a47b82009-02-23 08:49:38 +00005631 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5632 // and update MaskVals with the new element order.
5633 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005634 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005635 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005636 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005637 for (unsigned i = 4; i != 8; ++i) {
5638 int idx = MaskVals[i];
5639 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005640 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005641 InOrder.set(i);
5642 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005643 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005644 InOrder.set(i);
5645 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005646 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005647 }
5648 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005649 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005650 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005651
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005652 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5653 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005654 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5655 NewV.getOperand(0),
5656 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5657 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005658 }
Eric Christopherfd179292009-08-27 18:07:15 +00005659
Nate Begemanb9a47b82009-02-23 08:49:38 +00005660 // In case BestHi & BestLo were both -1, which means each quadword has a word
5661 // from each of the four input quadwords, calculate the InOrder bitvector now
5662 // before falling through to the insert/extract cleanup.
5663 if (BestLoQuad == -1 && BestHiQuad == -1) {
5664 NewV = V1;
5665 for (int i = 0; i != 8; ++i)
5666 if (MaskVals[i] < 0 || MaskVals[i] == i)
5667 InOrder.set(i);
5668 }
Eric Christopherfd179292009-08-27 18:07:15 +00005669
Nate Begemanb9a47b82009-02-23 08:49:38 +00005670 // The other elements are put in the right place using pextrw and pinsrw.
5671 for (unsigned i = 0; i != 8; ++i) {
5672 if (InOrder[i])
5673 continue;
5674 int EltIdx = MaskVals[i];
5675 if (EltIdx < 0)
5676 continue;
5677 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005678 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005679 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005680 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005681 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005682 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005683 DAG.getIntPtrConstant(i));
5684 }
5685 return NewV;
5686}
5687
5688// v16i8 shuffles - Prefer shuffles in the following order:
5689// 1. [ssse3] 1 x pshufb
5690// 2. [ssse3] 2 x pshufb + 1 x por
5691// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5692static
Nate Begeman9008ca62009-04-27 18:41:29 +00005693SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005694 SelectionDAG &DAG,
5695 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005696 SDValue V1 = SVOp->getOperand(0);
5697 SDValue V2 = SVOp->getOperand(1);
5698 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005699 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005700 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005701
Nate Begemanb9a47b82009-02-23 08:49:38 +00005702 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005703 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005704 // present, fall back to case 3.
5705 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5706 bool V1Only = true;
5707 bool V2Only = true;
5708 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005709 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005710 if (EltIdx < 0)
5711 continue;
5712 if (EltIdx < 16)
5713 V2Only = false;
5714 else
5715 V1Only = false;
5716 }
Eric Christopherfd179292009-08-27 18:07:15 +00005717
Nate Begemanb9a47b82009-02-23 08:49:38 +00005718 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005719 if (TLI.getSubtarget()->hasSSSE3() || TLI.getSubtarget()->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005720 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005721
Nate Begemanb9a47b82009-02-23 08:49:38 +00005722 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005723 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005724 //
5725 // Otherwise, we have elements from both input vectors, and must zero out
5726 // elements that come from V2 in the first mask, and V1 in the second mask
5727 // so that we can OR them together.
5728 bool TwoInputs = !(V1Only || V2Only);
5729 for (unsigned i = 0; i != 16; ++i) {
5730 int EltIdx = MaskVals[i];
5731 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005732 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005733 continue;
5734 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005735 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005736 }
5737 // If all the elements are from V2, assign it to V1 and return after
5738 // building the first pshufb.
5739 if (V2Only)
5740 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005741 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005742 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005743 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005744 if (!TwoInputs)
5745 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005746
Nate Begemanb9a47b82009-02-23 08:49:38 +00005747 // Calculate the shuffle mask for the second input, shuffle it, and
5748 // OR it with the first shuffled input.
5749 pshufbMask.clear();
5750 for (unsigned i = 0; i != 16; ++i) {
5751 int EltIdx = MaskVals[i];
5752 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005753 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005754 continue;
5755 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005756 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005757 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005758 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005759 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005760 MVT::v16i8, &pshufbMask[0], 16));
5761 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005762 }
Eric Christopherfd179292009-08-27 18:07:15 +00005763
Nate Begemanb9a47b82009-02-23 08:49:38 +00005764 // No SSSE3 - Calculate in place words and then fix all out of place words
5765 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5766 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005767 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5768 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005769 SDValue NewV = V2Only ? V2 : V1;
5770 for (int i = 0; i != 8; ++i) {
5771 int Elt0 = MaskVals[i*2];
5772 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005773
Nate Begemanb9a47b82009-02-23 08:49:38 +00005774 // This word of the result is all undef, skip it.
5775 if (Elt0 < 0 && Elt1 < 0)
5776 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005777
Nate Begemanb9a47b82009-02-23 08:49:38 +00005778 // This word of the result is already in the correct place, skip it.
5779 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5780 continue;
5781 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5782 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005783
Nate Begemanb9a47b82009-02-23 08:49:38 +00005784 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5785 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5786 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005787
5788 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5789 // using a single extract together, load it and store it.
5790 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005791 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005792 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005793 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005794 DAG.getIntPtrConstant(i));
5795 continue;
5796 }
5797
Nate Begemanb9a47b82009-02-23 08:49:38 +00005798 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005799 // source byte is not also odd, shift the extracted word left 8 bits
5800 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005801 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005802 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005803 DAG.getIntPtrConstant(Elt1 / 2));
5804 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005805 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005806 DAG.getConstant(8,
5807 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005808 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005809 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5810 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005811 }
5812 // If Elt0 is defined, extract it from the appropriate source. If the
5813 // source byte is not also even, shift the extracted word right 8 bits. If
5814 // Elt1 was also defined, OR the extracted values together before
5815 // inserting them in the result.
5816 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005817 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005818 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5819 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005820 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005821 DAG.getConstant(8,
5822 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005823 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005824 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5825 DAG.getConstant(0x00FF, MVT::i16));
5826 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005827 : InsElt0;
5828 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005829 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005830 DAG.getIntPtrConstant(i));
5831 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005832 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005833}
5834
Evan Cheng7a831ce2007-12-15 03:00:47 +00005835/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005836/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005837/// done when every pair / quad of shuffle mask elements point to elements in
5838/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005839/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005840static
Nate Begeman9008ca62009-04-27 18:41:29 +00005841SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005842 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005843 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005844 SDValue V1 = SVOp->getOperand(0);
5845 SDValue V2 = SVOp->getOperand(1);
5846 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005847 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005848 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005849 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005850 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005851 case MVT::v4f32: NewVT = MVT::v2f64; break;
5852 case MVT::v4i32: NewVT = MVT::v2i64; break;
5853 case MVT::v8i16: NewVT = MVT::v4i32; break;
5854 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005855 }
5856
Nate Begeman9008ca62009-04-27 18:41:29 +00005857 int Scale = NumElems / NewWidth;
5858 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005859 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005860 int StartIdx = -1;
5861 for (int j = 0; j < Scale; ++j) {
5862 int EltIdx = SVOp->getMaskElt(i+j);
5863 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005864 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005865 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005866 StartIdx = EltIdx - (EltIdx % Scale);
5867 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005868 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005869 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005870 if (StartIdx == -1)
5871 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005872 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005873 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005874 }
5875
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005876 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5877 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005878 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005879}
5880
Evan Chengd880b972008-05-09 21:53:03 +00005881/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005882///
Owen Andersone50ed302009-08-10 22:56:29 +00005883static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005884 SDValue SrcOp, SelectionDAG &DAG,
5885 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005886 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005887 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005888 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005889 LD = dyn_cast<LoadSDNode>(SrcOp);
5890 if (!LD) {
5891 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5892 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005893 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005894 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005895 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005896 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005897 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005898 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005899 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005900 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005901 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5902 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5903 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005904 SrcOp.getOperand(0)
5905 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005906 }
5907 }
5908 }
5909
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005910 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005911 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005912 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005913 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005914}
5915
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005916/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5917/// shuffle node referes to only one lane in the sources.
5918static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5919 EVT VT = SVOp->getValueType(0);
5920 int NumElems = VT.getVectorNumElements();
5921 int HalfSize = NumElems/2;
5922 SmallVector<int, 16> M;
5923 SVOp->getMask(M);
5924 bool MatchA = false, MatchB = false;
5925
5926 for (int l = 0; l < NumElems*2; l += HalfSize) {
5927 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5928 MatchA = true;
5929 break;
5930 }
5931 }
5932
5933 for (int l = 0; l < NumElems*2; l += HalfSize) {
5934 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
5935 MatchB = true;
5936 break;
5937 }
5938 }
5939
5940 return MatchA && MatchB;
5941}
5942
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005943/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5944/// which could not be matched by any known target speficic shuffle
5945static SDValue
5946LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005947 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
5948 // If each half of a vector shuffle node referes to only one lane in the
5949 // source vectors, extract each used 128-bit lane and shuffle them using
5950 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
5951 // the work to the legalizer.
5952 DebugLoc dl = SVOp->getDebugLoc();
5953 EVT VT = SVOp->getValueType(0);
5954 int NumElems = VT.getVectorNumElements();
5955 int HalfSize = NumElems/2;
5956
5957 // Extract the reference for each half
5958 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
5959 int FstVecOpNum = 0, SndVecOpNum = 0;
5960 for (int i = 0; i < HalfSize; ++i) {
5961 int Elt = SVOp->getMaskElt(i);
5962 if (SVOp->getMaskElt(i) < 0)
5963 continue;
5964 FstVecOpNum = Elt/NumElems;
5965 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5966 break;
5967 }
5968 for (int i = HalfSize; i < NumElems; ++i) {
5969 int Elt = SVOp->getMaskElt(i);
5970 if (SVOp->getMaskElt(i) < 0)
5971 continue;
5972 SndVecOpNum = Elt/NumElems;
5973 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5974 break;
5975 }
5976
5977 // Extract the subvectors
5978 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
5979 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
5980 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
5981 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
5982
5983 // Generate 128-bit shuffles
5984 SmallVector<int, 16> MaskV1, MaskV2;
5985 for (int i = 0; i < HalfSize; ++i) {
5986 int Elt = SVOp->getMaskElt(i);
5987 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5988 }
5989 for (int i = HalfSize; i < NumElems; ++i) {
5990 int Elt = SVOp->getMaskElt(i);
5991 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5992 }
5993
5994 EVT NVT = V1.getValueType();
5995 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
5996 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
5997
5998 // Concatenate the result back
5999 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
6000 DAG.getConstant(0, MVT::i32), DAG, dl);
6001 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
6002 DAG, dl);
6003 }
6004
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006005 return SDValue();
6006}
6007
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006008/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6009/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006010static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006011LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006012 SDValue V1 = SVOp->getOperand(0);
6013 SDValue V2 = SVOp->getOperand(1);
6014 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006015 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006016
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006017 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
6018
Evan Chengace3c172008-07-22 21:13:36 +00006019 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00006020 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006021 SmallVector<int, 8> Mask1(4U, -1);
6022 SmallVector<int, 8> PermMask;
6023 SVOp->getMask(PermMask);
6024
Evan Chengace3c172008-07-22 21:13:36 +00006025 unsigned NumHi = 0;
6026 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006027 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006028 int Idx = PermMask[i];
6029 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006030 Locs[i] = std::make_pair(-1, -1);
6031 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006032 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6033 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006034 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006035 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006036 NumLo++;
6037 } else {
6038 Locs[i] = std::make_pair(1, NumHi);
6039 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006040 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006041 NumHi++;
6042 }
6043 }
6044 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006045
Evan Chengace3c172008-07-22 21:13:36 +00006046 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006047 // If no more than two elements come from either vector. This can be
6048 // implemented with two shuffles. First shuffle gather the elements.
6049 // The second shuffle, which takes the first shuffle as both of its
6050 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006051 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006052
Nate Begeman9008ca62009-04-27 18:41:29 +00006053 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006054
Evan Chengace3c172008-07-22 21:13:36 +00006055 for (unsigned i = 0; i != 4; ++i) {
6056 if (Locs[i].first == -1)
6057 continue;
6058 else {
6059 unsigned Idx = (i < 2) ? 0 : 4;
6060 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006061 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006062 }
6063 }
6064
Nate Begeman9008ca62009-04-27 18:41:29 +00006065 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006066 } else if (NumLo == 3 || NumHi == 3) {
6067 // Otherwise, we must have three elements from one vector, call it X, and
6068 // one element from the other, call it Y. First, use a shufps to build an
6069 // intermediate vector with the one element from Y and the element from X
6070 // that will be in the same half in the final destination (the indexes don't
6071 // matter). Then, use a shufps to build the final vector, taking the half
6072 // containing the element from Y from the intermediate, and the other half
6073 // from X.
6074 if (NumHi == 3) {
6075 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00006076 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006077 std::swap(V1, V2);
6078 }
6079
6080 // Find the element from V2.
6081 unsigned HiIndex;
6082 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006083 int Val = PermMask[HiIndex];
6084 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006085 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006086 if (Val >= 4)
6087 break;
6088 }
6089
Nate Begeman9008ca62009-04-27 18:41:29 +00006090 Mask1[0] = PermMask[HiIndex];
6091 Mask1[1] = -1;
6092 Mask1[2] = PermMask[HiIndex^1];
6093 Mask1[3] = -1;
6094 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006095
6096 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006097 Mask1[0] = PermMask[0];
6098 Mask1[1] = PermMask[1];
6099 Mask1[2] = HiIndex & 1 ? 6 : 4;
6100 Mask1[3] = HiIndex & 1 ? 4 : 6;
6101 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006102 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006103 Mask1[0] = HiIndex & 1 ? 2 : 0;
6104 Mask1[1] = HiIndex & 1 ? 0 : 2;
6105 Mask1[2] = PermMask[2];
6106 Mask1[3] = PermMask[3];
6107 if (Mask1[2] >= 0)
6108 Mask1[2] += 4;
6109 if (Mask1[3] >= 0)
6110 Mask1[3] += 4;
6111 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006112 }
Evan Chengace3c172008-07-22 21:13:36 +00006113 }
6114
6115 // Break it into (shuffle shuffle_hi, shuffle_lo).
6116 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006117 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006118 SmallVector<int,8> LoMask(4U, -1);
6119 SmallVector<int,8> HiMask(4U, -1);
6120
6121 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006122 unsigned MaskIdx = 0;
6123 unsigned LoIdx = 0;
6124 unsigned HiIdx = 2;
6125 for (unsigned i = 0; i != 4; ++i) {
6126 if (i == 2) {
6127 MaskPtr = &HiMask;
6128 MaskIdx = 1;
6129 LoIdx = 0;
6130 HiIdx = 2;
6131 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006132 int Idx = PermMask[i];
6133 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006134 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006135 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006136 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006137 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006138 LoIdx++;
6139 } else {
6140 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006141 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006142 HiIdx++;
6143 }
6144 }
6145
Nate Begeman9008ca62009-04-27 18:41:29 +00006146 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6147 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6148 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006149 for (unsigned i = 0; i != 4; ++i) {
6150 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006151 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006152 } else {
6153 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006154 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006155 }
6156 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006157 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006158}
6159
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006160static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006161 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006162 V = V.getOperand(0);
6163 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6164 V = V.getOperand(0);
6165 if (MayFoldLoad(V))
6166 return true;
6167 return false;
6168}
6169
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006170// FIXME: the version above should always be used. Since there's
6171// a bug where several vector shuffles can't be folded because the
6172// DAG is not updated during lowering and a node claims to have two
6173// uses while it only has one, use this version, and let isel match
6174// another instruction if the load really happens to have more than
6175// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006176// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006177static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006178 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006179 V = V.getOperand(0);
6180 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6181 V = V.getOperand(0);
6182 if (ISD::isNormalLoad(V.getNode()))
6183 return true;
6184 return false;
6185}
6186
6187/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6188/// a vector extract, and if both can be later optimized into a single load.
6189/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6190/// here because otherwise a target specific shuffle node is going to be
6191/// emitted for this shuffle, and the optimization not done.
6192/// FIXME: This is probably not the best approach, but fix the problem
6193/// until the right path is decided.
6194static
6195bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6196 const TargetLowering &TLI) {
6197 EVT VT = V.getValueType();
6198 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6199
6200 // Be sure that the vector shuffle is present in a pattern like this:
6201 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6202 if (!V.hasOneUse())
6203 return false;
6204
6205 SDNode *N = *V.getNode()->use_begin();
6206 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6207 return false;
6208
6209 SDValue EltNo = N->getOperand(1);
6210 if (!isa<ConstantSDNode>(EltNo))
6211 return false;
6212
6213 // If the bit convert changed the number of elements, it is unsafe
6214 // to examine the mask.
6215 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006216 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006217 EVT SrcVT = V.getOperand(0).getValueType();
6218 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6219 return false;
6220 V = V.getOperand(0);
6221 HasShuffleIntoBitcast = true;
6222 }
6223
6224 // Select the input vector, guarding against out of range extract vector.
6225 unsigned NumElems = VT.getVectorNumElements();
6226 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6227 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6228 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6229
6230 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006231 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006232 V = V.getOperand(0);
6233
6234 if (ISD::isNormalLoad(V.getNode())) {
6235 // Is the original load suitable?
6236 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6237
6238 // FIXME: avoid the multi-use bug that is preventing lots of
6239 // of foldings to be detected, this is still wrong of course, but
6240 // give the temporary desired behavior, and if it happens that
6241 // the load has real more uses, during isel it will not fold, and
6242 // will generate poor code.
6243 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6244 return false;
6245
6246 if (!HasShuffleIntoBitcast)
6247 return true;
6248
6249 // If there's a bitcast before the shuffle, check if the load type and
6250 // alignment is valid.
6251 unsigned Align = LN0->getAlignment();
6252 unsigned NewAlign =
6253 TLI.getTargetData()->getABITypeAlignment(
6254 VT.getTypeForEVT(*DAG.getContext()));
6255
6256 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6257 return false;
6258 }
6259
6260 return true;
6261}
6262
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006263static
Evan Cheng835580f2010-10-07 20:50:20 +00006264SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6265 EVT VT = Op.getValueType();
6266
6267 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006268 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6269 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006270 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6271 V1, DAG));
6272}
6273
6274static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006275SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006276 bool HasXMMInt) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006277 SDValue V1 = Op.getOperand(0);
6278 SDValue V2 = Op.getOperand(1);
6279 EVT VT = Op.getValueType();
6280
6281 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6282
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006283 if (HasXMMInt && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006284 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6285
Evan Cheng0899f5c2011-08-31 02:05:24 +00006286 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6287 return DAG.getNode(ISD::BITCAST, dl, VT,
6288 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6289 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6290 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006291}
6292
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006293static
6294SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6295 SDValue V1 = Op.getOperand(0);
6296 SDValue V2 = Op.getOperand(1);
6297 EVT VT = Op.getValueType();
6298
6299 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6300 "unsupported shuffle type");
6301
6302 if (V2.getOpcode() == ISD::UNDEF)
6303 V2 = V1;
6304
6305 // v4i32 or v4f32
6306 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6307}
6308
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006309static inline unsigned getSHUFPOpcode(EVT VT) {
6310 switch(VT.getSimpleVT().SimpleTy) {
6311 case MVT::v8i32: // Use fp unit for int unpack.
6312 case MVT::v8f32:
6313 case MVT::v4i32: // Use fp unit for int unpack.
6314 case MVT::v4f32: return X86ISD::SHUFPS;
6315 case MVT::v4i64: // Use fp unit for int unpack.
6316 case MVT::v4f64:
6317 case MVT::v2i64: // Use fp unit for int unpack.
6318 case MVT::v2f64: return X86ISD::SHUFPD;
6319 default:
6320 llvm_unreachable("Unknown type for shufp*");
6321 }
6322 return 0;
6323}
6324
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006325static
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006326SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasXMMInt) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006327 SDValue V1 = Op.getOperand(0);
6328 SDValue V2 = Op.getOperand(1);
6329 EVT VT = Op.getValueType();
6330 unsigned NumElems = VT.getVectorNumElements();
6331
6332 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6333 // operand of these instructions is only memory, so check if there's a
6334 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6335 // same masks.
6336 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006337
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006338 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006339 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006340 CanFoldLoad = true;
6341
6342 // When V1 is a load, it can be folded later into a store in isel, example:
6343 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6344 // turns into:
6345 // (MOVLPSmr addr:$src1, VR128:$src2)
6346 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006347 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006348 CanFoldLoad = true;
6349
Eric Christopher893a8822011-02-20 05:04:42 +00006350 // Both of them can't be memory operations though.
6351 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
6352 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00006353
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006354 if (CanFoldLoad) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006355 if (HasXMMInt && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006356 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6357
6358 if (NumElems == 4)
6359 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
6360 }
6361
6362 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6363 // movl and movlp will both match v2i64, but v2i64 is never matched by
6364 // movl earlier because we make it strict to avoid messing with the movlp load
6365 // folding logic (see the code above getMOVLP call). Match it here then,
6366 // this is horrible, but will stay like this until we move all shuffle
6367 // matching to x86 specific nodes. Note that for the 1st condition all
6368 // types are matched with movsd.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006369 if (HasXMMInt) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006370 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6371 // as to remove this logic from here, as much as possible
6372 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006373 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006374 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006375 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006376
6377 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6378
6379 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006380 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006381 X86::getShuffleSHUFImmediate(SVOp), DAG);
6382}
6383
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006384static inline unsigned getUNPCKLOpcode(EVT VT) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006385 switch(VT.getSimpleVT().SimpleTy) {
6386 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
6387 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006388 case MVT::v4f32: return X86ISD::UNPCKLPS;
6389 case MVT::v2f64: return X86ISD::UNPCKLPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006390 case MVT::v8i32: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006391 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006392 case MVT::v4i64: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006393 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006394 case MVT::v16i8: return X86ISD::PUNPCKLBW;
6395 case MVT::v8i16: return X86ISD::PUNPCKLWD;
6396 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006397 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006398 }
6399 return 0;
6400}
6401
6402static inline unsigned getUNPCKHOpcode(EVT VT) {
6403 switch(VT.getSimpleVT().SimpleTy) {
6404 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
6405 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
6406 case MVT::v4f32: return X86ISD::UNPCKHPS;
6407 case MVT::v2f64: return X86ISD::UNPCKHPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006408 case MVT::v8i32: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006409 case MVT::v8f32: return X86ISD::VUNPCKHPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006410 case MVT::v4i64: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006411 case MVT::v4f64: return X86ISD::VUNPCKHPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006412 case MVT::v16i8: return X86ISD::PUNPCKHBW;
6413 case MVT::v8i16: return X86ISD::PUNPCKHWD;
6414 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006415 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006416 }
6417 return 0;
6418}
6419
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006420static inline unsigned getVPERMILOpcode(EVT VT) {
6421 switch(VT.getSimpleVT().SimpleTy) {
6422 case MVT::v4i32:
6423 case MVT::v4f32: return X86ISD::VPERMILPS;
6424 case MVT::v2i64:
6425 case MVT::v2f64: return X86ISD::VPERMILPD;
6426 case MVT::v8i32:
6427 case MVT::v8f32: return X86ISD::VPERMILPSY;
6428 case MVT::v4i64:
6429 case MVT::v4f64: return X86ISD::VPERMILPDY;
6430 default:
6431 llvm_unreachable("Unknown type for vpermil");
6432 }
6433 return 0;
6434}
6435
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006436/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
6437/// a vbroadcast node. The nodes are suitable whenever we can fold a load coming
6438/// from a 32 or 64 bit scalar. Update Op to the desired load to be folded.
6439static bool isVectorBroadcast(SDValue &Op) {
6440 EVT VT = Op.getValueType();
6441 bool Is256 = VT.getSizeInBits() == 256;
6442
6443 assert((VT.getSizeInBits() == 128 || Is256) &&
6444 "Unsupported type for vbroadcast node");
6445
6446 SDValue V = Op;
6447 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
6448 V = V.getOperand(0);
6449
6450 if (Is256 && !(V.hasOneUse() &&
6451 V.getOpcode() == ISD::INSERT_SUBVECTOR &&
6452 V.getOperand(0).getOpcode() == ISD::UNDEF))
6453 return false;
6454
6455 if (Is256)
6456 V = V.getOperand(1);
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006457
6458 if (!V.hasOneUse())
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006459 return false;
6460
6461 // Check the source scalar_to_vector type. 256-bit broadcasts are
6462 // supported for 32/64-bit sizes, while 128-bit ones are only supported
6463 // for 32-bit scalars.
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006464 if (V.getOpcode() != ISD::SCALAR_TO_VECTOR)
6465 return false;
6466
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006467 unsigned ScalarSize = V.getOperand(0).getValueType().getSizeInBits();
6468 if (ScalarSize != 32 && ScalarSize != 64)
6469 return false;
6470 if (!Is256 && ScalarSize == 64)
6471 return false;
6472
6473 V = V.getOperand(0);
6474 if (!MayFoldLoad(V))
6475 return false;
6476
6477 // Return the load node
6478 Op = V;
6479 return true;
6480}
6481
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006482static
6483SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006484 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006485 const X86Subtarget *Subtarget) {
6486 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6487 EVT VT = Op.getValueType();
6488 DebugLoc dl = Op.getDebugLoc();
6489 SDValue V1 = Op.getOperand(0);
6490 SDValue V2 = Op.getOperand(1);
6491
6492 if (isZeroShuffle(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006493 return getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006494
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006495 // Handle splat operations
6496 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006497 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006498 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006499 // Special case, this is the only place now where it's allowed to return
6500 // a vector_shuffle operation without using a target specific node, because
6501 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6502 // this be moved to DAGCombine instead?
6503 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006504 return Op;
6505
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006506 // Use vbroadcast whenever the splat comes from a foldable load
6507 if (Subtarget->hasAVX() && isVectorBroadcast(V1))
6508 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, V1);
6509
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006510 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006511 if ((Size == 128 && NumElem <= 4) ||
6512 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006513 return SDValue();
6514
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006515 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006516 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006517 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006518
6519 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6520 // do it!
6521 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6522 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6523 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006524 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006525 } else if ((VT == MVT::v4i32 ||
6526 (VT == MVT::v4f32 && Subtarget->hasXMMInt()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006527 // FIXME: Figure out a cleaner way to do this.
6528 // Try to make use of movq to zero out the top part.
6529 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6530 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6531 if (NewOp.getNode()) {
6532 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6533 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6534 DAG, Subtarget, dl);
6535 }
6536 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6537 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6538 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6539 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6540 DAG, Subtarget, dl);
6541 }
6542 }
6543 return SDValue();
6544}
6545
Dan Gohman475871a2008-07-27 21:46:04 +00006546SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006547X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006548 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006549 SDValue V1 = Op.getOperand(0);
6550 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006551 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006552 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006553 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006554 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006555 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
6556 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006557 bool V1IsSplat = false;
6558 bool V2IsSplat = false;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006559 bool HasXMMInt = Subtarget->hasXMMInt();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006560 MachineFunction &MF = DAG.getMachineFunction();
6561 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006562
Dale Johannesen0488fb62010-09-30 23:57:10 +00006563 // Shuffle operations on MMX not supported.
6564 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006565 return Op;
6566
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006567 // Vector shuffle lowering takes 3 steps:
6568 //
6569 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6570 // narrowing and commutation of operands should be handled.
6571 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6572 // shuffle nodes.
6573 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6574 // so the shuffle can be broken into other shuffles and the legalizer can
6575 // try the lowering again.
6576 //
6577 // The general ideia is that no vector_shuffle operation should be left to
6578 // be matched during isel, all of them must be converted to a target specific
6579 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006580
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006581 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6582 // narrowing and commutation of operands should be handled. The actual code
6583 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006584 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006585 if (NewOp.getNode())
6586 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006587
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006588 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6589 // unpckh_undef). Only use pshufd if speed is more important than size.
6590 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006591 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006592 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006593 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006594
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006595 if (X86::isMOVDDUPMask(SVOp) &&
6596 (Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
6597 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006598 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006599
Dale Johannesen0488fb62010-09-30 23:57:10 +00006600 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006601 return getMOVHighToLow(Op, dl, DAG);
6602
6603 // Use to match splats
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006604 if (HasXMMInt && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006605 (VT == MVT::v2f64 || VT == MVT::v2i64))
6606 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
6607
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006608 if (X86::isPSHUFDMask(SVOp)) {
6609 // The actual implementation will match the mask in the if above and then
6610 // during isel it can match several different instructions, not only pshufd
6611 // as its name says, sad but true, emulate the behavior for now...
6612 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6613 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6614
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006615 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6616
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006617 if (HasXMMInt && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006618 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6619
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006620 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6621 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006622 }
Eric Christopherfd179292009-08-27 18:07:15 +00006623
Evan Chengf26ffe92008-05-29 08:22:04 +00006624 // Check if this can be converted into a logical shift.
6625 bool isLeft = false;
6626 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006627 SDValue ShVal;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006628 bool isShift = getSubtarget()->hasXMMInt() &&
6629 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006630 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006631 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006632 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006633 EVT EltVT = VT.getVectorElementType();
6634 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006635 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006636 }
Eric Christopherfd179292009-08-27 18:07:15 +00006637
Nate Begeman9008ca62009-04-27 18:41:29 +00006638 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006639 if (V1IsUndef)
6640 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00006641 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006642 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006643 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006644 if (HasXMMInt && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006645 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6646
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006647 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006648 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6649 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006650 }
Eric Christopherfd179292009-08-27 18:07:15 +00006651
Nate Begeman9008ca62009-04-27 18:41:29 +00006652 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00006653 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006654 return getMOVLowToHigh(Op, dl, DAG, HasXMMInt);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006655
Dale Johannesen0488fb62010-09-30 23:57:10 +00006656 if (X86::isMOVHLPSMask(SVOp))
6657 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006658
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006659 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006660 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006661
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006662 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006663 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006664
Dale Johannesen0488fb62010-09-30 23:57:10 +00006665 if (X86::isMOVLPMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006666 return getMOVLP(Op, dl, DAG, HasXMMInt);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006667
Nate Begeman9008ca62009-04-27 18:41:29 +00006668 if (ShouldXformToMOVHLPS(SVOp) ||
6669 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6670 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006671
Evan Chengf26ffe92008-05-29 08:22:04 +00006672 if (isShift) {
6673 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006674 EVT EltVT = VT.getVectorElementType();
6675 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006676 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006677 }
Eric Christopherfd179292009-08-27 18:07:15 +00006678
Evan Cheng9eca5e82006-10-25 21:49:50 +00006679 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006680 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6681 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006682 V1IsSplat = isSplatVector(V1.getNode());
6683 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006684
Chris Lattner8a594482007-11-25 00:24:49 +00006685 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00006686 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006687 Op = CommuteVectorShuffle(SVOp, DAG);
6688 SVOp = cast<ShuffleVectorSDNode>(Op);
6689 V1 = SVOp->getOperand(0);
6690 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006691 std::swap(V1IsSplat, V2IsSplat);
6692 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006693 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006694 }
6695
Nate Begeman9008ca62009-04-27 18:41:29 +00006696 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6697 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006698 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006699 return V1;
6700 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6701 // the instruction selector will not match, so get a canonical MOVL with
6702 // swapped operands to undo the commute.
6703 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006704 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006705
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006706 if (X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006707 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006708
6709 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006710 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006711
Evan Cheng9bbbb982006-10-25 20:48:19 +00006712 if (V2IsSplat) {
6713 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006714 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006715 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006716 SDValue NewMask = NormalizeMask(SVOp, DAG);
6717 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6718 if (NSVOp != SVOp) {
6719 if (X86::isUNPCKLMask(NSVOp, true)) {
6720 return NewMask;
6721 } else if (X86::isUNPCKHMask(NSVOp, true)) {
6722 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006723 }
6724 }
6725 }
6726
Evan Cheng9eca5e82006-10-25 21:49:50 +00006727 if (Commuted) {
6728 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006729 // FIXME: this seems wrong.
6730 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6731 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006732
6733 if (X86::isUNPCKLMask(NewSVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006734 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006735
6736 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006737 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006738 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006739
Nate Begeman9008ca62009-04-27 18:41:29 +00006740 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006741 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006742 return CommuteVectorShuffle(SVOp, DAG);
6743
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006744 // The checks below are all present in isShuffleMaskLegal, but they are
6745 // inlined here right now to enable us to directly emit target specific
6746 // nodes, and remove one by one until they don't return Op anymore.
6747 SmallVector<int, 16> M;
6748 SVOp->getMask(M);
6749
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006750 if (isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006751 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6752 X86::getShufflePALIGNRImmediate(SVOp),
6753 DAG);
6754
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006755 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6756 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006757 if (VT == MVT::v2f64)
6758 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006759 if (VT == MVT::v2i64)
6760 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6761 }
6762
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006763 if (isPSHUFHWMask(M, VT))
6764 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6765 X86::getShufflePSHUFHWImmediate(SVOp),
6766 DAG);
6767
6768 if (isPSHUFLWMask(M, VT))
6769 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6770 X86::getShufflePSHUFLWImmediate(SVOp),
6771 DAG);
6772
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006773 if (isSHUFPMask(M, VT))
6774 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6775 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006776
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006777 if (X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006778 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006779 if (X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006780 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006781
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006782 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006783 // Generate target specific nodes for 128 or 256-bit shuffles only
6784 // supported in the AVX instruction set.
6785 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006786
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006787 // Handle VMOVDDUPY permutations
6788 if (isMOVDDUPYMask(SVOp, Subtarget))
6789 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6790
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006791 // Handle VPERMILPS* permutations
6792 if (isVPERMILPSMask(M, VT, Subtarget))
6793 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6794 getShuffleVPERMILPSImmediate(SVOp), DAG);
6795
6796 // Handle VPERMILPD* permutations
6797 if (isVPERMILPDMask(M, VT, Subtarget))
6798 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6799 getShuffleVPERMILPDImmediate(SVOp), DAG);
6800
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00006801 // Handle VPERM2F128 permutations
6802 if (isVPERM2F128Mask(M, VT, Subtarget))
6803 return getTargetShuffleNode(X86ISD::VPERM2F128, dl, VT, V1, V2,
6804 getShuffleVPERM2F128Immediate(SVOp), DAG);
6805
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006806 // Handle VSHUFPSY permutations
6807 if (isVSHUFPSYMask(M, VT, Subtarget))
6808 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6809 getShuffleVSHUFPSYImmediate(SVOp), DAG);
6810
6811 // Handle VSHUFPDY permutations
6812 if (isVSHUFPDYMask(M, VT, Subtarget))
6813 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6814 getShuffleVSHUFPDYImmediate(SVOp), DAG);
6815
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006816 //===--------------------------------------------------------------------===//
6817 // Since no target specific shuffle was selected for this generic one,
6818 // lower it into other known shuffles. FIXME: this isn't true yet, but
6819 // this is the plan.
6820 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006821
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006822 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6823 if (VT == MVT::v8i16) {
6824 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6825 if (NewOp.getNode())
6826 return NewOp;
6827 }
6828
6829 if (VT == MVT::v16i8) {
6830 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6831 if (NewOp.getNode())
6832 return NewOp;
6833 }
6834
6835 // Handle all 128-bit wide vectors with 4 elements, and match them with
6836 // several different shuffle types.
6837 if (NumElems == 4 && VT.getSizeInBits() == 128)
6838 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6839
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006840 // Handle general 256-bit shuffles
6841 if (VT.is256BitVector())
6842 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6843
Dan Gohman475871a2008-07-27 21:46:04 +00006844 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006845}
6846
Dan Gohman475871a2008-07-27 21:46:04 +00006847SDValue
6848X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006849 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006850 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006851 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006852
6853 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6854 return SDValue();
6855
Duncan Sands83ec4b62008-06-06 12:08:01 +00006856 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006857 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006858 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006859 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006860 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006861 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006862 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006863 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6864 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6865 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006866 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6867 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006868 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006869 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006870 Op.getOperand(0)),
6871 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006872 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006873 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006874 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006875 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006876 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006877 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006878 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6879 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006880 // result has a single use which is a store or a bitcast to i32. And in
6881 // the case of a store, it's not worth it if the index is a constant 0,
6882 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006883 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006884 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006885 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006886 if ((User->getOpcode() != ISD::STORE ||
6887 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6888 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006889 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006890 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006891 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006892 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006893 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006894 Op.getOperand(0)),
6895 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006896 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006897 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006898 // ExtractPS works with constant index.
6899 if (isa<ConstantSDNode>(Op.getOperand(1)))
6900 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006901 }
Dan Gohman475871a2008-07-27 21:46:04 +00006902 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006903}
6904
6905
Dan Gohman475871a2008-07-27 21:46:04 +00006906SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006907X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6908 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006909 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006910 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006911
David Greene74a579d2011-02-10 16:57:36 +00006912 SDValue Vec = Op.getOperand(0);
6913 EVT VecVT = Vec.getValueType();
6914
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006915 // If this is a 256-bit vector result, first extract the 128-bit vector and
6916 // then extract the element from the 128-bit vector.
6917 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006918 DebugLoc dl = Op.getNode()->getDebugLoc();
6919 unsigned NumElems = VecVT.getVectorNumElements();
6920 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006921 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6922
6923 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006924 bool Upper = IdxVal >= NumElems/2;
6925 Vec = Extract128BitVector(Vec,
6926 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006927
David Greene74a579d2011-02-10 16:57:36 +00006928 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006929 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006930 }
6931
6932 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6933
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006934 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006935 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006936 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006937 return Res;
6938 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006939
Owen Andersone50ed302009-08-10 22:56:29 +00006940 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006941 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006942 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006943 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006944 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006945 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006946 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006947 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6948 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006949 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006950 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006951 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006952 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006953 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006954 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006955 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006956 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006957 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006958 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006959 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006960 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006961 if (Idx == 0)
6962 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006963
Evan Cheng0db9fe62006-04-25 20:13:52 +00006964 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006965 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006966 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006967 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006968 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006969 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006970 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006971 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006972 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6973 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6974 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006975 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006976 if (Idx == 0)
6977 return Op;
6978
6979 // UNPCKHPD the element to the lowest double word, then movsd.
6980 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6981 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006982 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006983 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006984 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006985 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006986 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006987 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006988 }
6989
Dan Gohman475871a2008-07-27 21:46:04 +00006990 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006991}
6992
Dan Gohman475871a2008-07-27 21:46:04 +00006993SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006994X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6995 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006996 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006997 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006998 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006999
Dan Gohman475871a2008-07-27 21:46:04 +00007000 SDValue N0 = Op.getOperand(0);
7001 SDValue N1 = Op.getOperand(1);
7002 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007003
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007004 if (VT.getSizeInBits() == 256)
7005 return SDValue();
7006
Dan Gohman8a55ce42009-09-23 21:02:20 +00007007 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007008 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007009 unsigned Opc;
7010 if (VT == MVT::v8i16)
7011 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007012 else if (VT == MVT::v16i8)
7013 Opc = X86ISD::PINSRB;
7014 else
7015 Opc = X86ISD::PINSRB;
7016
Nate Begeman14d12ca2008-02-11 04:19:36 +00007017 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7018 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007019 if (N1.getValueType() != MVT::i32)
7020 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7021 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007022 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007023 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007024 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007025 // Bits [7:6] of the constant are the source select. This will always be
7026 // zero here. The DAG Combiner may combine an extract_elt index into these
7027 // bits. For example (insert (extract, 3), 2) could be matched by putting
7028 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007029 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007030 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007031 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007032 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007033 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007034 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007035 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007036 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007037 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007038 // PINSR* works with constant index.
7039 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007040 }
Dan Gohman475871a2008-07-27 21:46:04 +00007041 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007042}
7043
Dan Gohman475871a2008-07-27 21:46:04 +00007044SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007045X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007046 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007047 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007048
David Greene6b381262011-02-09 15:32:06 +00007049 DebugLoc dl = Op.getDebugLoc();
7050 SDValue N0 = Op.getOperand(0);
7051 SDValue N1 = Op.getOperand(1);
7052 SDValue N2 = Op.getOperand(2);
7053
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007054 // If this is a 256-bit vector result, first extract the 128-bit vector,
7055 // insert the element into the extracted half and then place it back.
7056 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007057 if (!isa<ConstantSDNode>(N2))
7058 return SDValue();
7059
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007060 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007061 unsigned NumElems = VT.getVectorNumElements();
7062 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007063 bool Upper = IdxVal >= NumElems/2;
7064 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
7065 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007066
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007067 // Insert the element into the desired half.
7068 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
7069 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00007070
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007071 // Insert the changed part back to the 256-bit vector
7072 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007073 }
7074
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007075 if (Subtarget->hasSSE41() || Subtarget->hasAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007076 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7077
Dan Gohman8a55ce42009-09-23 21:02:20 +00007078 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007079 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007080
Dan Gohman8a55ce42009-09-23 21:02:20 +00007081 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007082 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7083 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007084 if (N1.getValueType() != MVT::i32)
7085 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7086 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007087 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007088 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007089 }
Dan Gohman475871a2008-07-27 21:46:04 +00007090 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007091}
7092
Dan Gohman475871a2008-07-27 21:46:04 +00007093SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007094X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007095 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007096 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007097 EVT OpVT = Op.getValueType();
7098
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007099 // If this is a 256-bit vector result, first insert into a 128-bit
7100 // vector and then insert into the 256-bit vector.
7101 if (OpVT.getSizeInBits() > 128) {
7102 // Insert into a 128-bit vector.
7103 EVT VT128 = EVT::getVectorVT(*Context,
7104 OpVT.getVectorElementType(),
7105 OpVT.getVectorNumElements() / 2);
7106
7107 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7108
7109 // Insert the 128-bit vector.
7110 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7111 DAG.getConstant(0, MVT::i32),
7112 DAG, dl);
7113 }
7114
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007115 if (Op.getValueType() == MVT::v1i64 &&
7116 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007117 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007118
Owen Anderson825b72b2009-08-11 20:47:22 +00007119 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007120 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7121 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007122 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007123 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007124}
7125
David Greene91585092011-01-26 15:38:49 +00007126// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7127// a simple subregister reference or explicit instructions to grab
7128// upper bits of a vector.
7129SDValue
7130X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7131 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007132 DebugLoc dl = Op.getNode()->getDebugLoc();
7133 SDValue Vec = Op.getNode()->getOperand(0);
7134 SDValue Idx = Op.getNode()->getOperand(1);
7135
7136 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7137 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7138 return Extract128BitVector(Vec, Idx, DAG, dl);
7139 }
David Greene91585092011-01-26 15:38:49 +00007140 }
7141 return SDValue();
7142}
7143
David Greenecfe33c42011-01-26 19:13:22 +00007144// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7145// simple superregister reference or explicit instructions to insert
7146// the upper bits of a vector.
7147SDValue
7148X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7149 if (Subtarget->hasAVX()) {
7150 DebugLoc dl = Op.getNode()->getDebugLoc();
7151 SDValue Vec = Op.getNode()->getOperand(0);
7152 SDValue SubVec = Op.getNode()->getOperand(1);
7153 SDValue Idx = Op.getNode()->getOperand(2);
7154
7155 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7156 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007157 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007158 }
7159 }
7160 return SDValue();
7161}
7162
Bill Wendling056292f2008-09-16 21:48:12 +00007163// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7164// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7165// one of the above mentioned nodes. It has to be wrapped because otherwise
7166// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7167// be used to form addressing mode. These wrapped nodes will be selected
7168// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007169SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007170X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007171 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007172
Chris Lattner41621a22009-06-26 19:22:52 +00007173 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7174 // global base reg.
7175 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007176 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007177 CodeModel::Model M = getTargetMachine().getCodeModel();
7178
Chris Lattner4f066492009-07-11 20:29:19 +00007179 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007180 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007181 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007182 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007183 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007184 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007185 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007186
Evan Cheng1606e8e2009-03-13 07:51:59 +00007187 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007188 CP->getAlignment(),
7189 CP->getOffset(), OpFlag);
7190 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007191 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007192 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007193 if (OpFlag) {
7194 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007195 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007196 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007197 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007198 }
7199
7200 return Result;
7201}
7202
Dan Gohmand858e902010-04-17 15:26:15 +00007203SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007204 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007205
Chris Lattner18c59872009-06-27 04:16:01 +00007206 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7207 // global base reg.
7208 unsigned char OpFlag = 0;
7209 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007210 CodeModel::Model M = getTargetMachine().getCodeModel();
7211
Chris Lattner4f066492009-07-11 20:29:19 +00007212 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007213 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007214 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007215 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007216 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007217 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007218 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007219
Chris Lattner18c59872009-06-27 04:16:01 +00007220 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7221 OpFlag);
7222 DebugLoc DL = JT->getDebugLoc();
7223 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007224
Chris Lattner18c59872009-06-27 04:16:01 +00007225 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007226 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007227 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7228 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007229 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007230 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007231
Chris Lattner18c59872009-06-27 04:16:01 +00007232 return Result;
7233}
7234
7235SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007236X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007237 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007238
Chris Lattner18c59872009-06-27 04:16:01 +00007239 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7240 // global base reg.
7241 unsigned char OpFlag = 0;
7242 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007243 CodeModel::Model M = getTargetMachine().getCodeModel();
7244
Chris Lattner4f066492009-07-11 20:29:19 +00007245 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007246 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7247 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7248 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007249 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007250 } else if (Subtarget->isPICStyleGOT()) {
7251 OpFlag = X86II::MO_GOT;
7252 } else if (Subtarget->isPICStyleStubPIC()) {
7253 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7254 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7255 OpFlag = X86II::MO_DARWIN_NONLAZY;
7256 }
Eric Christopherfd179292009-08-27 18:07:15 +00007257
Chris Lattner18c59872009-06-27 04:16:01 +00007258 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007259
Chris Lattner18c59872009-06-27 04:16:01 +00007260 DebugLoc DL = Op.getDebugLoc();
7261 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007262
7263
Chris Lattner18c59872009-06-27 04:16:01 +00007264 // With PIC, the address is actually $g + Offset.
7265 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007266 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007267 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7268 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007269 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007270 Result);
7271 }
Eric Christopherfd179292009-08-27 18:07:15 +00007272
Eli Friedman586272d2011-08-11 01:48:05 +00007273 // For symbols that require a load from a stub to get the address, emit the
7274 // load.
7275 if (isGlobalStubReference(OpFlag))
7276 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
7277 MachinePointerInfo::getGOT(), false, false, 0);
7278
Chris Lattner18c59872009-06-27 04:16:01 +00007279 return Result;
7280}
7281
Dan Gohman475871a2008-07-27 21:46:04 +00007282SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007283X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007284 // Create the TargetBlockAddressAddress node.
7285 unsigned char OpFlags =
7286 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007287 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007288 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007289 DebugLoc dl = Op.getDebugLoc();
7290 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7291 /*isTarget=*/true, OpFlags);
7292
Dan Gohmanf705adb2009-10-30 01:28:02 +00007293 if (Subtarget->isPICStyleRIPRel() &&
7294 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007295 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7296 else
7297 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007298
Dan Gohman29cbade2009-11-20 23:18:13 +00007299 // With PIC, the address is actually $g + Offset.
7300 if (isGlobalRelativeToPICBase(OpFlags)) {
7301 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7302 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7303 Result);
7304 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007305
7306 return Result;
7307}
7308
7309SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007310X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007311 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007312 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007313 // Create the TargetGlobalAddress node, folding in the constant
7314 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007315 unsigned char OpFlags =
7316 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007317 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007318 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007319 if (OpFlags == X86II::MO_NO_FLAG &&
7320 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007321 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007322 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007323 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007324 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007325 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007326 }
Eric Christopherfd179292009-08-27 18:07:15 +00007327
Chris Lattner4f066492009-07-11 20:29:19 +00007328 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007329 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007330 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7331 else
7332 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007333
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007334 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007335 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007336 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7337 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007338 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007339 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007340
Chris Lattner36c25012009-07-10 07:34:39 +00007341 // For globals that require a load from a stub to get the address, emit the
7342 // load.
7343 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007344 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007345 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007346
Dan Gohman6520e202008-10-18 02:06:02 +00007347 // If there was a non-zero offset that we didn't fold, create an explicit
7348 // addition for it.
7349 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007350 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007351 DAG.getConstant(Offset, getPointerTy()));
7352
Evan Cheng0db9fe62006-04-25 20:13:52 +00007353 return Result;
7354}
7355
Evan Chengda43bcf2008-09-24 00:05:32 +00007356SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007357X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007358 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007359 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007360 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007361}
7362
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007363static SDValue
7364GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007365 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007366 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007367 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007368 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007369 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007370 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007371 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007372 GA->getOffset(),
7373 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007374 if (InFlag) {
7375 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007376 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007377 } else {
7378 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007379 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007380 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007381
7382 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007383 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007384
Rafael Espindola15f1b662009-04-24 12:59:40 +00007385 SDValue Flag = Chain.getValue(1);
7386 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007387}
7388
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007389// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007390static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007391LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007392 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007393 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007394 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7395 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007396 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007397 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007398 InFlag = Chain.getValue(1);
7399
Chris Lattnerb903bed2009-06-26 21:20:29 +00007400 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007401}
7402
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007403// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007404static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007405LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007406 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007407 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7408 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007409}
7410
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007411// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7412// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007413static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007414 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007415 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007416 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007417
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007418 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7419 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7420 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007421
Michael J. Spencerec38de22010-10-10 22:04:20 +00007422 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007423 DAG.getIntPtrConstant(0),
7424 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007425
Chris Lattnerb903bed2009-06-26 21:20:29 +00007426 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007427 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7428 // initialexec.
7429 unsigned WrapperKind = X86ISD::Wrapper;
7430 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007431 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007432 } else if (is64Bit) {
7433 assert(model == TLSModel::InitialExec);
7434 OperandFlags = X86II::MO_GOTTPOFF;
7435 WrapperKind = X86ISD::WrapperRIP;
7436 } else {
7437 assert(model == TLSModel::InitialExec);
7438 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007439 }
Eric Christopherfd179292009-08-27 18:07:15 +00007440
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007441 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7442 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007443 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007444 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007445 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007446 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007447
Rafael Espindola9a580232009-02-27 13:37:18 +00007448 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007449 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007450 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007451
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007452 // The address of the thread local variable is the add of the thread
7453 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007454 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007455}
7456
Dan Gohman475871a2008-07-27 21:46:04 +00007457SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007458X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007459
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007460 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007461 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007462
Eric Christopher30ef0e52010-06-03 04:07:48 +00007463 if (Subtarget->isTargetELF()) {
7464 // TODO: implement the "local dynamic" model
7465 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007466
Eric Christopher30ef0e52010-06-03 04:07:48 +00007467 // If GV is an alias then use the aliasee for determining
7468 // thread-localness.
7469 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7470 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007471
7472 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007473 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007474
Eric Christopher30ef0e52010-06-03 04:07:48 +00007475 switch (model) {
7476 case TLSModel::GeneralDynamic:
7477 case TLSModel::LocalDynamic: // not implemented
7478 if (Subtarget->is64Bit())
7479 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7480 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007481
Eric Christopher30ef0e52010-06-03 04:07:48 +00007482 case TLSModel::InitialExec:
7483 case TLSModel::LocalExec:
7484 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7485 Subtarget->is64Bit());
7486 }
7487 } else if (Subtarget->isTargetDarwin()) {
7488 // Darwin only has one model of TLS. Lower to that.
7489 unsigned char OpFlag = 0;
7490 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7491 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007492
Eric Christopher30ef0e52010-06-03 04:07:48 +00007493 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7494 // global base reg.
7495 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7496 !Subtarget->is64Bit();
7497 if (PIC32)
7498 OpFlag = X86II::MO_TLVP_PIC_BASE;
7499 else
7500 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007501 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007502 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007503 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007504 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007505 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007506
Eric Christopher30ef0e52010-06-03 04:07:48 +00007507 // With PIC32, the address is actually $g + Offset.
7508 if (PIC32)
7509 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7510 DAG.getNode(X86ISD::GlobalBaseReg,
7511 DebugLoc(), getPointerTy()),
7512 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007513
Eric Christopher30ef0e52010-06-03 04:07:48 +00007514 // Lowering the machine isd will make sure everything is in the right
7515 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007516 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007517 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007518 SDValue Args[] = { Chain, Offset };
7519 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007520
Eric Christopher30ef0e52010-06-03 04:07:48 +00007521 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7522 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7523 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007524
Eric Christopher30ef0e52010-06-03 04:07:48 +00007525 // And our return value (tls address) is in the standard call return value
7526 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007527 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
7528 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007529 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007530
Eric Christopher30ef0e52010-06-03 04:07:48 +00007531 assert(false &&
7532 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007533
Torok Edwinc23197a2009-07-14 16:55:14 +00007534 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007535 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007536}
7537
Evan Cheng0db9fe62006-04-25 20:13:52 +00007538
Nadav Rotem43012222011-05-11 08:12:09 +00007539/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007540/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007541SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007542 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007543 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007544 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007545 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007546 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007547 SDValue ShOpLo = Op.getOperand(0);
7548 SDValue ShOpHi = Op.getOperand(1);
7549 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007550 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007551 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007552 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007553
Dan Gohman475871a2008-07-27 21:46:04 +00007554 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007555 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007556 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7557 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007558 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007559 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7560 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007561 }
Evan Chenge3413162006-01-09 18:33:28 +00007562
Owen Anderson825b72b2009-08-11 20:47:22 +00007563 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7564 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007565 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007566 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007567
Dan Gohman475871a2008-07-27 21:46:04 +00007568 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007569 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007570 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7571 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007572
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007573 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007574 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7575 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007576 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007577 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7578 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007579 }
7580
Dan Gohman475871a2008-07-27 21:46:04 +00007581 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007582 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007583}
Evan Chenga3195e82006-01-12 22:54:21 +00007584
Dan Gohmand858e902010-04-17 15:26:15 +00007585SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7586 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007587 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007588
Dale Johannesen0488fb62010-09-30 23:57:10 +00007589 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007590 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007591
Owen Anderson825b72b2009-08-11 20:47:22 +00007592 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007593 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007594
Eli Friedman36df4992009-05-27 00:47:34 +00007595 // These are really Legal; return the operand so the caller accepts it as
7596 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007597 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007598 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007599 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007600 Subtarget->is64Bit()) {
7601 return Op;
7602 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007603
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007604 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007605 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007606 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007607 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007608 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007609 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007610 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007611 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007612 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007613 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7614}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007615
Owen Andersone50ed302009-08-10 22:56:29 +00007616SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007617 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007618 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007619 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007620 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007621 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007622 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007623 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007624 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007625 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007626 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007627
Chris Lattner492a43e2010-09-22 01:28:21 +00007628 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007629
Stuart Hastings84be9582011-06-02 15:57:11 +00007630 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7631 MachineMemOperand *MMO;
7632 if (FI) {
7633 int SSFI = FI->getIndex();
7634 MMO =
7635 DAG.getMachineFunction()
7636 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7637 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7638 } else {
7639 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7640 StackSlot = StackSlot.getOperand(1);
7641 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007642 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007643 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7644 X86ISD::FILD, DL,
7645 Tys, Ops, array_lengthof(Ops),
7646 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007647
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007648 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007649 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007650 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007651
7652 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7653 // shouldn't be necessary except that RFP cannot be live across
7654 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007655 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007656 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7657 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007658 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007659 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007660 SDValue Ops[] = {
7661 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7662 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007663 MachineMemOperand *MMO =
7664 DAG.getMachineFunction()
7665 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007666 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007667
Chris Lattner492a43e2010-09-22 01:28:21 +00007668 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7669 Ops, array_lengthof(Ops),
7670 Op.getValueType(), MMO);
7671 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007672 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007673 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007674 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007675
Evan Cheng0db9fe62006-04-25 20:13:52 +00007676 return Result;
7677}
7678
Bill Wendling8b8a6362009-01-17 03:56:04 +00007679// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007680SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7681 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007682 // This algorithm is not obvious. Here it is in C code, more or less:
7683 /*
7684 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7685 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7686 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007687
Bill Wendling8b8a6362009-01-17 03:56:04 +00007688 // Copy ints to xmm registers.
7689 __m128i xh = _mm_cvtsi32_si128( hi );
7690 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007691
Bill Wendling8b8a6362009-01-17 03:56:04 +00007692 // Combine into low half of a single xmm register.
7693 __m128i x = _mm_unpacklo_epi32( xh, xl );
7694 __m128d d;
7695 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007696
Bill Wendling8b8a6362009-01-17 03:56:04 +00007697 // Merge in appropriate exponents to give the integer bits the right
7698 // magnitude.
7699 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007700
Bill Wendling8b8a6362009-01-17 03:56:04 +00007701 // Subtract away the biases to deal with the IEEE-754 double precision
7702 // implicit 1.
7703 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007704
Bill Wendling8b8a6362009-01-17 03:56:04 +00007705 // All conversions up to here are exact. The correctly rounded result is
7706 // calculated using the current rounding mode using the following
7707 // horizontal add.
7708 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7709 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7710 // store doesn't really need to be here (except
7711 // maybe to zero the other double)
7712 return sd;
7713 }
7714 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007715
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007716 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007717 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007718
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007719 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007720 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007721 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7722 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7723 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7724 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007725 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007726 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007727
Bill Wendling8b8a6362009-01-17 03:56:04 +00007728 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007729 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007730 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007731 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007732 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007733 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007734 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007735
Owen Anderson825b72b2009-08-11 20:47:22 +00007736 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7737 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007738 Op.getOperand(0),
7739 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007740 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7741 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007742 Op.getOperand(0),
7743 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007744 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7745 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007746 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007747 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007748 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007749 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007750 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007751 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007752 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007753 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007754
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007755 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007756 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007757 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7758 DAG.getUNDEF(MVT::v2f64), ShufMask);
7759 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7760 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007761 DAG.getIntPtrConstant(0));
7762}
7763
Bill Wendling8b8a6362009-01-17 03:56:04 +00007764// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007765SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7766 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007767 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007768 // FP constant to bias correct the final result.
7769 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007770 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007771
7772 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007773 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007774 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007775
Eli Friedmanf3704762011-08-29 21:15:46 +00007776 // Zero out the upper parts of the register.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00007777 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasXMMInt(),
7778 DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007779
Owen Anderson825b72b2009-08-11 20:47:22 +00007780 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007781 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007782 DAG.getIntPtrConstant(0));
7783
7784 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007785 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007786 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007787 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007788 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007789 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007790 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007791 MVT::v2f64, Bias)));
7792 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007793 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007794 DAG.getIntPtrConstant(0));
7795
7796 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007797 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007798
7799 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007800 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007801
Owen Anderson825b72b2009-08-11 20:47:22 +00007802 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007803 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007804 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007805 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007806 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007807 }
7808
7809 // Handle final rounding.
7810 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007811}
7812
Dan Gohmand858e902010-04-17 15:26:15 +00007813SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7814 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007815 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007816 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007817
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007818 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007819 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7820 // the optimization here.
7821 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007822 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007823
Owen Andersone50ed302009-08-10 22:56:29 +00007824 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007825 EVT DstVT = Op.getValueType();
7826 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007827 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007828 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007829 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007830
7831 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007832 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007833 if (SrcVT == MVT::i32) {
7834 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7835 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7836 getPointerTy(), StackSlot, WordOff);
7837 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007838 StackSlot, MachinePointerInfo(),
7839 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007840 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007841 OffsetSlot, MachinePointerInfo(),
7842 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007843 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7844 return Fild;
7845 }
7846
7847 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7848 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007849 StackSlot, MachinePointerInfo(),
7850 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007851 // For i64 source, we need to add the appropriate power of 2 if the input
7852 // was negative. This is the same as the optimization in
7853 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7854 // we must be careful to do the computation in x87 extended precision, not
7855 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007856 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7857 MachineMemOperand *MMO =
7858 DAG.getMachineFunction()
7859 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7860 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007861
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007862 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7863 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007864 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7865 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007866
7867 APInt FF(32, 0x5F800000ULL);
7868
7869 // Check whether the sign bit is set.
7870 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7871 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7872 ISD::SETLT);
7873
7874 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7875 SDValue FudgePtr = DAG.getConstantPool(
7876 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7877 getPointerTy());
7878
7879 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7880 SDValue Zero = DAG.getIntPtrConstant(0);
7881 SDValue Four = DAG.getIntPtrConstant(4);
7882 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7883 Zero, Four);
7884 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7885
7886 // Load the value out, extending it from f32 to f80.
7887 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007888 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007889 FudgePtr, MachinePointerInfo::getConstantPool(),
7890 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007891 // Extend everything to 80 bits to force it to be done on x87.
7892 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7893 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007894}
7895
Dan Gohman475871a2008-07-27 21:46:04 +00007896std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007897FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007898 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007899
Owen Andersone50ed302009-08-10 22:56:29 +00007900 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007901
7902 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007903 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7904 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007905 }
7906
Owen Anderson825b72b2009-08-11 20:47:22 +00007907 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7908 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007909 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007910
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007911 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007912 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007913 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007914 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007915 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007916 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007917 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007918 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007919
Evan Cheng87c89352007-10-15 20:11:21 +00007920 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7921 // stack slot.
7922 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007923 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007924 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007925 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007926
Michael J. Spencerec38de22010-10-10 22:04:20 +00007927
7928
Evan Cheng0db9fe62006-04-25 20:13:52 +00007929 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007930 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007931 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007932 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7933 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7934 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007935 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007936
Dan Gohman475871a2008-07-27 21:46:04 +00007937 SDValue Chain = DAG.getEntryNode();
7938 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007939 EVT TheVT = Op.getOperand(0).getValueType();
7940 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007941 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007942 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007943 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007944 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007945 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007946 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007947 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007948 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007949
Chris Lattner492a43e2010-09-22 01:28:21 +00007950 MachineMemOperand *MMO =
7951 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7952 MachineMemOperand::MOLoad, MemSize, MemSize);
7953 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7954 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007955 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007956 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007957 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7958 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007959
Chris Lattner07290932010-09-22 01:05:16 +00007960 MachineMemOperand *MMO =
7961 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7962 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007963
Evan Cheng0db9fe62006-04-25 20:13:52 +00007964 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007965 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007966 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7967 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007968
Chris Lattner27a6c732007-11-24 07:07:01 +00007969 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007970}
7971
Dan Gohmand858e902010-04-17 15:26:15 +00007972SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7973 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007974 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007975 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007976
Eli Friedman948e95a2009-05-23 09:59:16 +00007977 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007978 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007979 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7980 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007981
Chris Lattner27a6c732007-11-24 07:07:01 +00007982 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007983 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007984 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007985}
7986
Dan Gohmand858e902010-04-17 15:26:15 +00007987SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7988 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007989 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7990 SDValue FIST = Vals.first, StackSlot = Vals.second;
7991 assert(FIST.getNode() && "Unexpected failure");
7992
7993 // Load the result.
7994 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007995 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007996}
7997
Dan Gohmand858e902010-04-17 15:26:15 +00007998SDValue X86TargetLowering::LowerFABS(SDValue Op,
7999 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008000 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008001 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008002 EVT VT = Op.getValueType();
8003 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008004 if (VT.isVector())
8005 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008006 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008007 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008008 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00008009 CV.push_back(C);
8010 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008011 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008012 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00008013 CV.push_back(C);
8014 CV.push_back(C);
8015 CV.push_back(C);
8016 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008017 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008018 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008019 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008020 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008021 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008022 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008023 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008024}
8025
Dan Gohmand858e902010-04-17 15:26:15 +00008026SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008027 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008028 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008029 EVT VT = Op.getValueType();
8030 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00008031 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008032 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008033 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008034 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008035 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00008036 CV.push_back(C);
8037 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008038 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008039 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00008040 CV.push_back(C);
8041 CV.push_back(C);
8042 CV.push_back(C);
8043 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008044 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008045 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008046 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008047 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008048 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008049 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008050 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008051 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008052 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008053 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008054 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008055 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008056 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008057 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00008058 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008059}
8060
Dan Gohmand858e902010-04-17 15:26:15 +00008061SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008062 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008063 SDValue Op0 = Op.getOperand(0);
8064 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008065 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008066 EVT VT = Op.getValueType();
8067 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008068
8069 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008070 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008071 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008072 SrcVT = VT;
8073 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008074 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008075 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008076 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008077 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008078 }
8079
8080 // At this point the operands and the result should have the same
8081 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008082
Evan Cheng68c47cb2007-01-05 07:55:56 +00008083 // First get the sign bit of second operand.
8084 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008085 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008086 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8087 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008088 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008089 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8090 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8091 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8092 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008093 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008094 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008095 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008096 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008097 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008098 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008099 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008100
8101 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008102 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008103 // Op0 is MVT::f32, Op1 is MVT::f64.
8104 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8105 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8106 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008107 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008108 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008109 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008110 }
8111
Evan Cheng73d6cf12007-01-05 21:37:56 +00008112 // Clear first operand sign bit.
8113 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008114 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008115 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8116 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008117 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008118 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8119 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8120 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8121 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008122 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008123 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008124 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008125 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008126 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008127 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008128 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008129
8130 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008131 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008132}
8133
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008134SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8135 SDValue N0 = Op.getOperand(0);
8136 DebugLoc dl = Op.getDebugLoc();
8137 EVT VT = Op.getValueType();
8138
8139 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8140 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8141 DAG.getConstant(1, VT));
8142 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8143}
8144
Dan Gohman076aee32009-03-04 19:44:21 +00008145/// Emit nodes that will be selected as "test Op0,Op0", or something
8146/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008147SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008148 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008149 DebugLoc dl = Op.getDebugLoc();
8150
Dan Gohman31125812009-03-07 01:58:32 +00008151 // CF and OF aren't always set the way we want. Determine which
8152 // of these we need.
8153 bool NeedCF = false;
8154 bool NeedOF = false;
8155 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008156 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008157 case X86::COND_A: case X86::COND_AE:
8158 case X86::COND_B: case X86::COND_BE:
8159 NeedCF = true;
8160 break;
8161 case X86::COND_G: case X86::COND_GE:
8162 case X86::COND_L: case X86::COND_LE:
8163 case X86::COND_O: case X86::COND_NO:
8164 NeedOF = true;
8165 break;
Dan Gohman31125812009-03-07 01:58:32 +00008166 }
8167
Dan Gohman076aee32009-03-04 19:44:21 +00008168 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008169 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8170 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008171 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8172 // Emit a CMP with 0, which is the TEST pattern.
8173 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8174 DAG.getConstant(0, Op.getValueType()));
8175
8176 unsigned Opcode = 0;
8177 unsigned NumOperands = 0;
8178 switch (Op.getNode()->getOpcode()) {
8179 case ISD::ADD:
8180 // Due to an isel shortcoming, be conservative if this add is likely to be
8181 // selected as part of a load-modify-store instruction. When the root node
8182 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8183 // uses of other nodes in the match, such as the ADD in this case. This
8184 // leads to the ADD being left around and reselected, with the result being
8185 // two adds in the output. Alas, even if none our users are stores, that
8186 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8187 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8188 // climbing the DAG back to the root, and it doesn't seem to be worth the
8189 // effort.
8190 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00008191 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008192 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
8193 goto default_case;
8194
8195 if (ConstantSDNode *C =
8196 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8197 // An add of one will be selected as an INC.
8198 if (C->getAPIntValue() == 1) {
8199 Opcode = X86ISD::INC;
8200 NumOperands = 1;
8201 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008202 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008203
8204 // An add of negative one (subtract of one) will be selected as a DEC.
8205 if (C->getAPIntValue().isAllOnesValue()) {
8206 Opcode = X86ISD::DEC;
8207 NumOperands = 1;
8208 break;
8209 }
Dan Gohman076aee32009-03-04 19:44:21 +00008210 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008211
8212 // Otherwise use a regular EFLAGS-setting add.
8213 Opcode = X86ISD::ADD;
8214 NumOperands = 2;
8215 break;
8216 case ISD::AND: {
8217 // If the primary and result isn't used, don't bother using X86ISD::AND,
8218 // because a TEST instruction will be better.
8219 bool NonFlagUse = false;
8220 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8221 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8222 SDNode *User = *UI;
8223 unsigned UOpNo = UI.getOperandNo();
8224 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8225 // Look pass truncate.
8226 UOpNo = User->use_begin().getOperandNo();
8227 User = *User->use_begin();
8228 }
8229
8230 if (User->getOpcode() != ISD::BRCOND &&
8231 User->getOpcode() != ISD::SETCC &&
8232 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8233 NonFlagUse = true;
8234 break;
8235 }
Dan Gohman076aee32009-03-04 19:44:21 +00008236 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008237
8238 if (!NonFlagUse)
8239 break;
8240 }
8241 // FALL THROUGH
8242 case ISD::SUB:
8243 case ISD::OR:
8244 case ISD::XOR:
8245 // Due to the ISEL shortcoming noted above, be conservative if this op is
8246 // likely to be selected as part of a load-modify-store instruction.
8247 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8248 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8249 if (UI->getOpcode() == ISD::STORE)
8250 goto default_case;
8251
8252 // Otherwise use a regular EFLAGS-setting instruction.
8253 switch (Op.getNode()->getOpcode()) {
8254 default: llvm_unreachable("unexpected operator!");
8255 case ISD::SUB: Opcode = X86ISD::SUB; break;
8256 case ISD::OR: Opcode = X86ISD::OR; break;
8257 case ISD::XOR: Opcode = X86ISD::XOR; break;
8258 case ISD::AND: Opcode = X86ISD::AND; break;
8259 }
8260
8261 NumOperands = 2;
8262 break;
8263 case X86ISD::ADD:
8264 case X86ISD::SUB:
8265 case X86ISD::INC:
8266 case X86ISD::DEC:
8267 case X86ISD::OR:
8268 case X86ISD::XOR:
8269 case X86ISD::AND:
8270 return SDValue(Op.getNode(), 1);
8271 default:
8272 default_case:
8273 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008274 }
8275
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008276 if (Opcode == 0)
8277 // Emit a CMP with 0, which is the TEST pattern.
8278 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8279 DAG.getConstant(0, Op.getValueType()));
8280
8281 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8282 SmallVector<SDValue, 4> Ops;
8283 for (unsigned i = 0; i != NumOperands; ++i)
8284 Ops.push_back(Op.getOperand(i));
8285
8286 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8287 DAG.ReplaceAllUsesWith(Op, New);
8288 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008289}
8290
8291/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8292/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008293SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008294 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008295 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8296 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008297 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008298
8299 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008300 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008301}
8302
Evan Chengd40d03e2010-01-06 19:38:29 +00008303/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8304/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008305SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8306 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008307 SDValue Op0 = And.getOperand(0);
8308 SDValue Op1 = And.getOperand(1);
8309 if (Op0.getOpcode() == ISD::TRUNCATE)
8310 Op0 = Op0.getOperand(0);
8311 if (Op1.getOpcode() == ISD::TRUNCATE)
8312 Op1 = Op1.getOperand(0);
8313
Evan Chengd40d03e2010-01-06 19:38:29 +00008314 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008315 if (Op1.getOpcode() == ISD::SHL)
8316 std::swap(Op0, Op1);
8317 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008318 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8319 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008320 // If we looked past a truncate, check that it's only truncating away
8321 // known zeros.
8322 unsigned BitWidth = Op0.getValueSizeInBits();
8323 unsigned AndBitWidth = And.getValueSizeInBits();
8324 if (BitWidth > AndBitWidth) {
8325 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8326 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8327 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8328 return SDValue();
8329 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008330 LHS = Op1;
8331 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008332 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008333 } else if (Op1.getOpcode() == ISD::Constant) {
8334 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
8335 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00008336 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
8337 LHS = AndLHS.getOperand(0);
8338 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008339 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008340 }
Evan Cheng0488db92007-09-25 01:57:46 +00008341
Evan Chengd40d03e2010-01-06 19:38:29 +00008342 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008343 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008344 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008345 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008346 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008347 // Also promote i16 to i32 for performance / code size reason.
8348 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008349 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008350 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008351
Evan Chengd40d03e2010-01-06 19:38:29 +00008352 // If the operand types disagree, extend the shift amount to match. Since
8353 // BT ignores high bits (like shifts) we can use anyextend.
8354 if (LHS.getValueType() != RHS.getValueType())
8355 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008356
Evan Chengd40d03e2010-01-06 19:38:29 +00008357 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8358 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8359 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8360 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008361 }
8362
Evan Cheng54de3ea2010-01-05 06:52:31 +00008363 return SDValue();
8364}
8365
Dan Gohmand858e902010-04-17 15:26:15 +00008366SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008367
8368 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8369
Evan Cheng54de3ea2010-01-05 06:52:31 +00008370 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8371 SDValue Op0 = Op.getOperand(0);
8372 SDValue Op1 = Op.getOperand(1);
8373 DebugLoc dl = Op.getDebugLoc();
8374 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8375
8376 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008377 // Lower (X & (1 << N)) == 0 to BT(X, N).
8378 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8379 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008380 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008381 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008382 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008383 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8384 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8385 if (NewSetCC.getNode())
8386 return NewSetCC;
8387 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008388
Chris Lattner481eebc2010-12-19 21:23:48 +00008389 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8390 // these.
8391 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008392 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008393 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8394 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008395
Chris Lattner481eebc2010-12-19 21:23:48 +00008396 // If the input is a setcc, then reuse the input setcc or use a new one with
8397 // the inverted condition.
8398 if (Op0.getOpcode() == X86ISD::SETCC) {
8399 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8400 bool Invert = (CC == ISD::SETNE) ^
8401 cast<ConstantSDNode>(Op1)->isNullValue();
8402 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008403
Evan Cheng2c755ba2010-02-27 07:36:59 +00008404 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008405 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8406 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8407 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008408 }
8409
Evan Chenge5b51ac2010-04-17 06:13:15 +00008410 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008411 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008412 if (X86CC == X86::COND_INVALID)
8413 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008414
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008415 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008416 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008417 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008418}
8419
Craig Topper89af15e2011-09-18 08:03:58 +00008420// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008421// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008422static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008423 EVT VT = Op.getValueType();
8424
Duncan Sands28b77e92011-09-06 19:07:46 +00008425 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008426 "Unsupported value type for operation");
8427
8428 int NumElems = VT.getVectorNumElements();
8429 DebugLoc dl = Op.getDebugLoc();
8430 SDValue CC = Op.getOperand(2);
8431 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8432 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8433
8434 // Extract the LHS vectors
8435 SDValue LHS = Op.getOperand(0);
8436 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8437 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8438
8439 // Extract the RHS vectors
8440 SDValue RHS = Op.getOperand(1);
8441 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8442 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8443
8444 // Issue the operation on the smaller types and concatenate the result back
8445 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8446 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8447 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8448 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8449 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8450}
8451
8452
Dan Gohmand858e902010-04-17 15:26:15 +00008453SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008454 SDValue Cond;
8455 SDValue Op0 = Op.getOperand(0);
8456 SDValue Op1 = Op.getOperand(1);
8457 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008458 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008459 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8460 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008461 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008462
8463 if (isFP) {
8464 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008465 EVT EltVT = Op0.getValueType().getVectorElementType();
8466 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8467
8468 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008469 bool Swap = false;
8470
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008471 // SSE Condition code mapping:
8472 // 0 - EQ
8473 // 1 - LT
8474 // 2 - LE
8475 // 3 - UNORD
8476 // 4 - NEQ
8477 // 5 - NLT
8478 // 6 - NLE
8479 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008480 switch (SetCCOpcode) {
8481 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008482 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008483 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008484 case ISD::SETOGT:
8485 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008486 case ISD::SETLT:
8487 case ISD::SETOLT: SSECC = 1; break;
8488 case ISD::SETOGE:
8489 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008490 case ISD::SETLE:
8491 case ISD::SETOLE: SSECC = 2; break;
8492 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008493 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008494 case ISD::SETNE: SSECC = 4; break;
8495 case ISD::SETULE: Swap = true;
8496 case ISD::SETUGE: SSECC = 5; break;
8497 case ISD::SETULT: Swap = true;
8498 case ISD::SETUGT: SSECC = 6; break;
8499 case ISD::SETO: SSECC = 7; break;
8500 }
8501 if (Swap)
8502 std::swap(Op0, Op1);
8503
Nate Begemanfb8ead02008-07-25 19:05:58 +00008504 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008505 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008506 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008507 SDValue UNORD, EQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008508 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8509 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008510 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008511 }
8512 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008513 SDValue ORD, NEQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008514 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8515 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008516 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008517 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008518 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008519 }
8520 // Handle all other FP comparisons here.
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008521 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008522 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008523
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008524 // Break 256-bit integer vector compare into smaller ones.
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008525 if (!isFP && VT.getSizeInBits() == 256)
Craig Topper89af15e2011-09-18 08:03:58 +00008526 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008527
Nate Begeman30a0de92008-07-17 16:51:19 +00008528 // We are handling one of the integer comparisons here. Since SSE only has
8529 // GT and EQ comparisons for integer, swapping operands and multiple
8530 // operations may be required for some comparisons.
8531 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8532 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008533
Owen Anderson825b72b2009-08-11 20:47:22 +00008534 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008535 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008536 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008537 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008538 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8539 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008540 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008541
Nate Begeman30a0de92008-07-17 16:51:19 +00008542 switch (SetCCOpcode) {
8543 default: break;
8544 case ISD::SETNE: Invert = true;
8545 case ISD::SETEQ: Opc = EQOpc; break;
8546 case ISD::SETLT: Swap = true;
8547 case ISD::SETGT: Opc = GTOpc; break;
8548 case ISD::SETGE: Swap = true;
8549 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8550 case ISD::SETULT: Swap = true;
8551 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8552 case ISD::SETUGE: Swap = true;
8553 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8554 }
8555 if (Swap)
8556 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008557
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008558 // Check that the operation in question is available (most are plain SSE2,
8559 // but PCMPGTQ and PCMPEQQ have different requirements).
8560 if (Opc == X86ISD::PCMPGTQ && !Subtarget->hasSSE42() && !Subtarget->hasAVX())
8561 return SDValue();
8562 if (Opc == X86ISD::PCMPEQQ && !Subtarget->hasSSE41() && !Subtarget->hasAVX())
8563 return SDValue();
8564
Nate Begeman30a0de92008-07-17 16:51:19 +00008565 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8566 // bits of the inputs before performing those operations.
8567 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008568 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008569 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8570 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008571 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008572 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8573 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008574 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8575 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008576 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008577
Dale Johannesenace16102009-02-03 19:33:06 +00008578 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008579
8580 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008581 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008582 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008583
Nate Begeman30a0de92008-07-17 16:51:19 +00008584 return Result;
8585}
Evan Cheng0488db92007-09-25 01:57:46 +00008586
Evan Cheng370e5342008-12-03 08:38:43 +00008587// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008588static bool isX86LogicalCmp(SDValue Op) {
8589 unsigned Opc = Op.getNode()->getOpcode();
8590 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8591 return true;
8592 if (Op.getResNo() == 1 &&
8593 (Opc == X86ISD::ADD ||
8594 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008595 Opc == X86ISD::ADC ||
8596 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008597 Opc == X86ISD::SMUL ||
8598 Opc == X86ISD::UMUL ||
8599 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008600 Opc == X86ISD::DEC ||
8601 Opc == X86ISD::OR ||
8602 Opc == X86ISD::XOR ||
8603 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008604 return true;
8605
Chris Lattner9637d5b2010-12-05 07:49:54 +00008606 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8607 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008608
Dan Gohman076aee32009-03-04 19:44:21 +00008609 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008610}
8611
Chris Lattnera2b56002010-12-05 01:23:24 +00008612static bool isZero(SDValue V) {
8613 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8614 return C && C->isNullValue();
8615}
8616
Chris Lattner96908b12010-12-05 02:00:51 +00008617static bool isAllOnes(SDValue V) {
8618 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8619 return C && C->isAllOnesValue();
8620}
8621
Dan Gohmand858e902010-04-17 15:26:15 +00008622SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008623 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008624 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008625 SDValue Op1 = Op.getOperand(1);
8626 SDValue Op2 = Op.getOperand(2);
8627 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008628 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008629
Dan Gohman1a492952009-10-20 16:22:37 +00008630 if (Cond.getOpcode() == ISD::SETCC) {
8631 SDValue NewCond = LowerSETCC(Cond, DAG);
8632 if (NewCond.getNode())
8633 Cond = NewCond;
8634 }
Evan Cheng734503b2006-09-11 02:19:56 +00008635
Chris Lattnera2b56002010-12-05 01:23:24 +00008636 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008637 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008638 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008639 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008640 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008641 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8642 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008643 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008644
Chris Lattnera2b56002010-12-05 01:23:24 +00008645 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008646
8647 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008648 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8649 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008650
8651 SDValue CmpOp0 = Cmp.getOperand(0);
8652 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8653 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008654
Chris Lattner96908b12010-12-05 02:00:51 +00008655 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008656 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8657 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008658
Chris Lattner96908b12010-12-05 02:00:51 +00008659 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8660 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008661
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008662 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008663 if (N2C == 0 || !N2C->isNullValue())
8664 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8665 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008666 }
8667 }
8668
Chris Lattnera2b56002010-12-05 01:23:24 +00008669 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008670 if (Cond.getOpcode() == ISD::AND &&
8671 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8672 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008673 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008674 Cond = Cond.getOperand(0);
8675 }
8676
Evan Cheng3f41d662007-10-08 22:16:29 +00008677 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8678 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008679 if (Cond.getOpcode() == X86ISD::SETCC ||
8680 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008681 CC = Cond.getOperand(0);
8682
Dan Gohman475871a2008-07-27 21:46:04 +00008683 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008684 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008685 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008686
Evan Cheng3f41d662007-10-08 22:16:29 +00008687 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008688 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008689 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008690 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008691
Chris Lattnerd1980a52009-03-12 06:52:53 +00008692 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8693 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008694 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008695 addTest = false;
8696 }
8697 }
8698
8699 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008700 // Look pass the truncate.
8701 if (Cond.getOpcode() == ISD::TRUNCATE)
8702 Cond = Cond.getOperand(0);
8703
8704 // We know the result of AND is compared against zero. Try to match
8705 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008706 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008707 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008708 if (NewSetCC.getNode()) {
8709 CC = NewSetCC.getOperand(0);
8710 Cond = NewSetCC.getOperand(1);
8711 addTest = false;
8712 }
8713 }
8714 }
8715
8716 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008717 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008718 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008719 }
8720
Benjamin Kramere915ff32010-12-22 23:09:28 +00008721 // a < b ? -1 : 0 -> RES = ~setcc_carry
8722 // a < b ? 0 : -1 -> RES = setcc_carry
8723 // a >= b ? -1 : 0 -> RES = setcc_carry
8724 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8725 if (Cond.getOpcode() == X86ISD::CMP) {
8726 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8727
8728 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8729 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8730 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8731 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8732 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8733 return DAG.getNOT(DL, Res, Res.getValueType());
8734 return Res;
8735 }
8736 }
8737
Evan Cheng0488db92007-09-25 01:57:46 +00008738 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8739 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008740 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008741 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008742 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008743}
8744
Evan Cheng370e5342008-12-03 08:38:43 +00008745// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8746// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8747// from the AND / OR.
8748static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8749 Opc = Op.getOpcode();
8750 if (Opc != ISD::OR && Opc != ISD::AND)
8751 return false;
8752 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8753 Op.getOperand(0).hasOneUse() &&
8754 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8755 Op.getOperand(1).hasOneUse());
8756}
8757
Evan Cheng961d6d42009-02-02 08:19:07 +00008758// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8759// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008760static bool isXor1OfSetCC(SDValue Op) {
8761 if (Op.getOpcode() != ISD::XOR)
8762 return false;
8763 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8764 if (N1C && N1C->getAPIntValue() == 1) {
8765 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8766 Op.getOperand(0).hasOneUse();
8767 }
8768 return false;
8769}
8770
Dan Gohmand858e902010-04-17 15:26:15 +00008771SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008772 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008773 SDValue Chain = Op.getOperand(0);
8774 SDValue Cond = Op.getOperand(1);
8775 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008776 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008777 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00008778
Dan Gohman1a492952009-10-20 16:22:37 +00008779 if (Cond.getOpcode() == ISD::SETCC) {
8780 SDValue NewCond = LowerSETCC(Cond, DAG);
8781 if (NewCond.getNode())
8782 Cond = NewCond;
8783 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008784#if 0
8785 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008786 else if (Cond.getOpcode() == X86ISD::ADD ||
8787 Cond.getOpcode() == X86ISD::SUB ||
8788 Cond.getOpcode() == X86ISD::SMUL ||
8789 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008790 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008791#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008792
Evan Chengad9c0a32009-12-15 00:53:42 +00008793 // Look pass (and (setcc_carry (cmp ...)), 1).
8794 if (Cond.getOpcode() == ISD::AND &&
8795 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8796 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008797 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008798 Cond = Cond.getOperand(0);
8799 }
8800
Evan Cheng3f41d662007-10-08 22:16:29 +00008801 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8802 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008803 if (Cond.getOpcode() == X86ISD::SETCC ||
8804 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008805 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008806
Dan Gohman475871a2008-07-27 21:46:04 +00008807 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008808 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008809 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008810 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008811 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008812 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008813 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008814 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008815 default: break;
8816 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008817 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008818 // These can only come from an arithmetic instruction with overflow,
8819 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008820 Cond = Cond.getNode()->getOperand(1);
8821 addTest = false;
8822 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008823 }
Evan Cheng0488db92007-09-25 01:57:46 +00008824 }
Evan Cheng370e5342008-12-03 08:38:43 +00008825 } else {
8826 unsigned CondOpc;
8827 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8828 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008829 if (CondOpc == ISD::OR) {
8830 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8831 // two branches instead of an explicit OR instruction with a
8832 // separate test.
8833 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008834 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008835 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008836 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008837 Chain, Dest, CC, Cmp);
8838 CC = Cond.getOperand(1).getOperand(0);
8839 Cond = Cmp;
8840 addTest = false;
8841 }
8842 } else { // ISD::AND
8843 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8844 // two branches instead of an explicit AND instruction with a
8845 // separate test. However, we only do this if this block doesn't
8846 // have a fall-through edge, because this requires an explicit
8847 // jmp when the condition is false.
8848 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008849 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008850 Op.getNode()->hasOneUse()) {
8851 X86::CondCode CCode =
8852 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8853 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008854 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008855 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008856 // Look for an unconditional branch following this conditional branch.
8857 // We need this because we need to reverse the successors in order
8858 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008859 if (User->getOpcode() == ISD::BR) {
8860 SDValue FalseBB = User->getOperand(1);
8861 SDNode *NewBR =
8862 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008863 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008864 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008865 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008866
Dale Johannesene4d209d2009-02-03 20:21:25 +00008867 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008868 Chain, Dest, CC, Cmp);
8869 X86::CondCode CCode =
8870 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8871 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008872 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008873 Cond = Cmp;
8874 addTest = false;
8875 }
8876 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008877 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008878 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8879 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8880 // It should be transformed during dag combiner except when the condition
8881 // is set by a arithmetics with overflow node.
8882 X86::CondCode CCode =
8883 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8884 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008885 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008886 Cond = Cond.getOperand(0).getOperand(1);
8887 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00008888 }
Evan Cheng0488db92007-09-25 01:57:46 +00008889 }
8890
8891 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008892 // Look pass the truncate.
8893 if (Cond.getOpcode() == ISD::TRUNCATE)
8894 Cond = Cond.getOperand(0);
8895
8896 // We know the result of AND is compared against zero. Try to match
8897 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008898 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008899 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8900 if (NewSetCC.getNode()) {
8901 CC = NewSetCC.getOperand(0);
8902 Cond = NewSetCC.getOperand(1);
8903 addTest = false;
8904 }
8905 }
8906 }
8907
8908 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008909 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008910 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008911 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008912 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008913 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008914}
8915
Anton Korobeynikove060b532007-04-17 19:34:00 +00008916
8917// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8918// Calls to _alloca is needed to probe the stack when allocating more than 4k
8919// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8920// that the guard pages used by the OS virtual memory manager are allocated in
8921// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008922SDValue
8923X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008924 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008925 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
8926 EnableSegmentedStacks) &&
8927 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00008928 "are being used");
8929 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008930 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008931
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008932 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008933 SDValue Chain = Op.getOperand(0);
8934 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008935 // FIXME: Ensure alignment here
8936
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008937 bool Is64Bit = Subtarget->is64Bit();
8938 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008939
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008940 if (EnableSegmentedStacks) {
8941 MachineFunction &MF = DAG.getMachineFunction();
8942 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008943
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008944 if (Is64Bit) {
8945 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00008946 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008947 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008948
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008949 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
8950 I != E; I++)
8951 if (I->hasNestAttr())
8952 report_fatal_error("Cannot use segmented stacks with functions that "
8953 "have nested arguments.");
8954 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008955
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008956 const TargetRegisterClass *AddrRegClass =
8957 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
8958 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
8959 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
8960 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
8961 DAG.getRegister(Vreg, SPTy));
8962 SDValue Ops1[2] = { Value, Chain };
8963 return DAG.getMergeValues(Ops1, 2, dl);
8964 } else {
8965 SDValue Flag;
8966 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008967
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008968 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
8969 Flag = Chain.getValue(1);
8970 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008971
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008972 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
8973 Flag = Chain.getValue(1);
8974
8975 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
8976
8977 SDValue Ops1[2] = { Chain.getValue(0), Chain };
8978 return DAG.getMergeValues(Ops1, 2, dl);
8979 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008980}
8981
Dan Gohmand858e902010-04-17 15:26:15 +00008982SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008983 MachineFunction &MF = DAG.getMachineFunction();
8984 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8985
Dan Gohman69de1932008-02-06 22:27:42 +00008986 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008987 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008988
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008989 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008990 // vastart just stores the address of the VarArgsFrameIndex slot into the
8991 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008992 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8993 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008994 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8995 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008996 }
8997
8998 // __va_list_tag:
8999 // gp_offset (0 - 6 * 8)
9000 // fp_offset (48 - 48 + 8 * 16)
9001 // overflow_arg_area (point to parameters coming in memory).
9002 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009003 SmallVector<SDValue, 8> MemOps;
9004 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009005 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009006 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009007 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9008 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009009 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009010 MemOps.push_back(Store);
9011
9012 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009013 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009014 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009015 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009016 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9017 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009018 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009019 MemOps.push_back(Store);
9020
9021 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009022 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009023 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009024 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9025 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009026 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9027 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009028 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009029 MemOps.push_back(Store);
9030
9031 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009032 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009033 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009034 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9035 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009036 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9037 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009038 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009039 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009040 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009041}
9042
Dan Gohmand858e902010-04-17 15:26:15 +00009043SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009044 assert(Subtarget->is64Bit() &&
9045 "LowerVAARG only handles 64-bit va_arg!");
9046 assert((Subtarget->isTargetLinux() ||
9047 Subtarget->isTargetDarwin()) &&
9048 "Unhandled target in LowerVAARG");
9049 assert(Op.getNode()->getNumOperands() == 4);
9050 SDValue Chain = Op.getOperand(0);
9051 SDValue SrcPtr = Op.getOperand(1);
9052 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9053 unsigned Align = Op.getConstantOperandVal(3);
9054 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009055
Dan Gohman320afb82010-10-12 18:00:49 +00009056 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009057 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009058 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9059 uint8_t ArgMode;
9060
9061 // Decide which area this value should be read from.
9062 // TODO: Implement the AMD64 ABI in its entirety. This simple
9063 // selection mechanism works only for the basic types.
9064 if (ArgVT == MVT::f80) {
9065 llvm_unreachable("va_arg for f80 not yet implemented");
9066 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9067 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9068 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9069 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9070 } else {
9071 llvm_unreachable("Unhandled argument type in LowerVAARG");
9072 }
9073
9074 if (ArgMode == 2) {
9075 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00009076 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009077 !(DAG.getMachineFunction()
9078 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009079 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009080 }
9081
9082 // Insert VAARG_64 node into the DAG
9083 // VAARG_64 returns two values: Variable Argument Address, Chain
9084 SmallVector<SDValue, 11> InstOps;
9085 InstOps.push_back(Chain);
9086 InstOps.push_back(SrcPtr);
9087 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9088 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9089 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9090 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9091 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9092 VTs, &InstOps[0], InstOps.size(),
9093 MVT::i64,
9094 MachinePointerInfo(SV),
9095 /*Align=*/0,
9096 /*Volatile=*/false,
9097 /*ReadMem=*/true,
9098 /*WriteMem=*/true);
9099 Chain = VAARG.getValue(1);
9100
9101 // Load the next argument and return it
9102 return DAG.getLoad(ArgVT, dl,
9103 Chain,
9104 VAARG,
9105 MachinePointerInfo(),
9106 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009107}
9108
Dan Gohmand858e902010-04-17 15:26:15 +00009109SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009110 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009111 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009112 SDValue Chain = Op.getOperand(0);
9113 SDValue DstPtr = Op.getOperand(1);
9114 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009115 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9116 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009117 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009118
Chris Lattnere72f2022010-09-21 05:40:29 +00009119 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009120 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009121 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009122 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009123}
9124
Dan Gohman475871a2008-07-27 21:46:04 +00009125SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009126X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009127 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009128 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009129 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009130 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009131 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009132 case Intrinsic::x86_sse_comieq_ss:
9133 case Intrinsic::x86_sse_comilt_ss:
9134 case Intrinsic::x86_sse_comile_ss:
9135 case Intrinsic::x86_sse_comigt_ss:
9136 case Intrinsic::x86_sse_comige_ss:
9137 case Intrinsic::x86_sse_comineq_ss:
9138 case Intrinsic::x86_sse_ucomieq_ss:
9139 case Intrinsic::x86_sse_ucomilt_ss:
9140 case Intrinsic::x86_sse_ucomile_ss:
9141 case Intrinsic::x86_sse_ucomigt_ss:
9142 case Intrinsic::x86_sse_ucomige_ss:
9143 case Intrinsic::x86_sse_ucomineq_ss:
9144 case Intrinsic::x86_sse2_comieq_sd:
9145 case Intrinsic::x86_sse2_comilt_sd:
9146 case Intrinsic::x86_sse2_comile_sd:
9147 case Intrinsic::x86_sse2_comigt_sd:
9148 case Intrinsic::x86_sse2_comige_sd:
9149 case Intrinsic::x86_sse2_comineq_sd:
9150 case Intrinsic::x86_sse2_ucomieq_sd:
9151 case Intrinsic::x86_sse2_ucomilt_sd:
9152 case Intrinsic::x86_sse2_ucomile_sd:
9153 case Intrinsic::x86_sse2_ucomigt_sd:
9154 case Intrinsic::x86_sse2_ucomige_sd:
9155 case Intrinsic::x86_sse2_ucomineq_sd: {
9156 unsigned Opc = 0;
9157 ISD::CondCode CC = ISD::SETCC_INVALID;
9158 switch (IntNo) {
9159 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009160 case Intrinsic::x86_sse_comieq_ss:
9161 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009162 Opc = X86ISD::COMI;
9163 CC = ISD::SETEQ;
9164 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009165 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009166 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009167 Opc = X86ISD::COMI;
9168 CC = ISD::SETLT;
9169 break;
9170 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009171 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009172 Opc = X86ISD::COMI;
9173 CC = ISD::SETLE;
9174 break;
9175 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009176 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009177 Opc = X86ISD::COMI;
9178 CC = ISD::SETGT;
9179 break;
9180 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009181 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009182 Opc = X86ISD::COMI;
9183 CC = ISD::SETGE;
9184 break;
9185 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009186 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009187 Opc = X86ISD::COMI;
9188 CC = ISD::SETNE;
9189 break;
9190 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009191 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009192 Opc = X86ISD::UCOMI;
9193 CC = ISD::SETEQ;
9194 break;
9195 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009196 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009197 Opc = X86ISD::UCOMI;
9198 CC = ISD::SETLT;
9199 break;
9200 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009201 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009202 Opc = X86ISD::UCOMI;
9203 CC = ISD::SETLE;
9204 break;
9205 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009206 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009207 Opc = X86ISD::UCOMI;
9208 CC = ISD::SETGT;
9209 break;
9210 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009211 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009212 Opc = X86ISD::UCOMI;
9213 CC = ISD::SETGE;
9214 break;
9215 case Intrinsic::x86_sse_ucomineq_ss:
9216 case Intrinsic::x86_sse2_ucomineq_sd:
9217 Opc = X86ISD::UCOMI;
9218 CC = ISD::SETNE;
9219 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009220 }
Evan Cheng734503b2006-09-11 02:19:56 +00009221
Dan Gohman475871a2008-07-27 21:46:04 +00009222 SDValue LHS = Op.getOperand(1);
9223 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009224 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009225 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009226 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9227 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9228 DAG.getConstant(X86CC, MVT::i8), Cond);
9229 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009230 }
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009231 // Arithmetic intrinsics.
9232 case Intrinsic::x86_sse3_hadd_ps:
9233 case Intrinsic::x86_sse3_hadd_pd:
9234 case Intrinsic::x86_avx_hadd_ps_256:
9235 case Intrinsic::x86_avx_hadd_pd_256:
9236 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9237 Op.getOperand(1), Op.getOperand(2));
9238 case Intrinsic::x86_sse3_hsub_ps:
9239 case Intrinsic::x86_sse3_hsub_pd:
9240 case Intrinsic::x86_avx_hsub_ps_256:
9241 case Intrinsic::x86_avx_hsub_pd_256:
9242 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9243 Op.getOperand(1), Op.getOperand(2));
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009244 // ptest and testp intrinsics. The intrinsic these come from are designed to
9245 // return an integer value, not just an instruction so lower it to the ptest
9246 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009247 case Intrinsic::x86_sse41_ptestz:
9248 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009249 case Intrinsic::x86_sse41_ptestnzc:
9250 case Intrinsic::x86_avx_ptestz_256:
9251 case Intrinsic::x86_avx_ptestc_256:
9252 case Intrinsic::x86_avx_ptestnzc_256:
9253 case Intrinsic::x86_avx_vtestz_ps:
9254 case Intrinsic::x86_avx_vtestc_ps:
9255 case Intrinsic::x86_avx_vtestnzc_ps:
9256 case Intrinsic::x86_avx_vtestz_pd:
9257 case Intrinsic::x86_avx_vtestc_pd:
9258 case Intrinsic::x86_avx_vtestnzc_pd:
9259 case Intrinsic::x86_avx_vtestz_ps_256:
9260 case Intrinsic::x86_avx_vtestc_ps_256:
9261 case Intrinsic::x86_avx_vtestnzc_ps_256:
9262 case Intrinsic::x86_avx_vtestz_pd_256:
9263 case Intrinsic::x86_avx_vtestc_pd_256:
9264 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9265 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009266 unsigned X86CC = 0;
9267 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009268 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009269 case Intrinsic::x86_avx_vtestz_ps:
9270 case Intrinsic::x86_avx_vtestz_pd:
9271 case Intrinsic::x86_avx_vtestz_ps_256:
9272 case Intrinsic::x86_avx_vtestz_pd_256:
9273 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009274 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009275 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009276 // ZF = 1
9277 X86CC = X86::COND_E;
9278 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009279 case Intrinsic::x86_avx_vtestc_ps:
9280 case Intrinsic::x86_avx_vtestc_pd:
9281 case Intrinsic::x86_avx_vtestc_ps_256:
9282 case Intrinsic::x86_avx_vtestc_pd_256:
9283 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009284 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009285 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009286 // CF = 1
9287 X86CC = X86::COND_B;
9288 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009289 case Intrinsic::x86_avx_vtestnzc_ps:
9290 case Intrinsic::x86_avx_vtestnzc_pd:
9291 case Intrinsic::x86_avx_vtestnzc_ps_256:
9292 case Intrinsic::x86_avx_vtestnzc_pd_256:
9293 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009294 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009295 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009296 // ZF and CF = 0
9297 X86CC = X86::COND_A;
9298 break;
9299 }
Eric Christopherfd179292009-08-27 18:07:15 +00009300
Eric Christopher71c67532009-07-29 00:28:05 +00009301 SDValue LHS = Op.getOperand(1);
9302 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009303 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9304 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009305 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9306 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9307 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009308 }
Evan Cheng5759f972008-05-04 09:15:50 +00009309
9310 // Fix vector shift instructions where the last operand is a non-immediate
9311 // i32 value.
9312 case Intrinsic::x86_sse2_pslli_w:
9313 case Intrinsic::x86_sse2_pslli_d:
9314 case Intrinsic::x86_sse2_pslli_q:
9315 case Intrinsic::x86_sse2_psrli_w:
9316 case Intrinsic::x86_sse2_psrli_d:
9317 case Intrinsic::x86_sse2_psrli_q:
9318 case Intrinsic::x86_sse2_psrai_w:
9319 case Intrinsic::x86_sse2_psrai_d:
9320 case Intrinsic::x86_mmx_pslli_w:
9321 case Intrinsic::x86_mmx_pslli_d:
9322 case Intrinsic::x86_mmx_pslli_q:
9323 case Intrinsic::x86_mmx_psrli_w:
9324 case Intrinsic::x86_mmx_psrli_d:
9325 case Intrinsic::x86_mmx_psrli_q:
9326 case Intrinsic::x86_mmx_psrai_w:
9327 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009328 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009329 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009330 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009331
9332 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009333 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009334 switch (IntNo) {
9335 case Intrinsic::x86_sse2_pslli_w:
9336 NewIntNo = Intrinsic::x86_sse2_psll_w;
9337 break;
9338 case Intrinsic::x86_sse2_pslli_d:
9339 NewIntNo = Intrinsic::x86_sse2_psll_d;
9340 break;
9341 case Intrinsic::x86_sse2_pslli_q:
9342 NewIntNo = Intrinsic::x86_sse2_psll_q;
9343 break;
9344 case Intrinsic::x86_sse2_psrli_w:
9345 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9346 break;
9347 case Intrinsic::x86_sse2_psrli_d:
9348 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9349 break;
9350 case Intrinsic::x86_sse2_psrli_q:
9351 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9352 break;
9353 case Intrinsic::x86_sse2_psrai_w:
9354 NewIntNo = Intrinsic::x86_sse2_psra_w;
9355 break;
9356 case Intrinsic::x86_sse2_psrai_d:
9357 NewIntNo = Intrinsic::x86_sse2_psra_d;
9358 break;
9359 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009360 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009361 switch (IntNo) {
9362 case Intrinsic::x86_mmx_pslli_w:
9363 NewIntNo = Intrinsic::x86_mmx_psll_w;
9364 break;
9365 case Intrinsic::x86_mmx_pslli_d:
9366 NewIntNo = Intrinsic::x86_mmx_psll_d;
9367 break;
9368 case Intrinsic::x86_mmx_pslli_q:
9369 NewIntNo = Intrinsic::x86_mmx_psll_q;
9370 break;
9371 case Intrinsic::x86_mmx_psrli_w:
9372 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9373 break;
9374 case Intrinsic::x86_mmx_psrli_d:
9375 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9376 break;
9377 case Intrinsic::x86_mmx_psrli_q:
9378 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9379 break;
9380 case Intrinsic::x86_mmx_psrai_w:
9381 NewIntNo = Intrinsic::x86_mmx_psra_w;
9382 break;
9383 case Intrinsic::x86_mmx_psrai_d:
9384 NewIntNo = Intrinsic::x86_mmx_psra_d;
9385 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009386 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009387 }
9388 break;
9389 }
9390 }
Mon P Wangefa42202009-09-03 19:56:25 +00009391
9392 // The vector shift intrinsics with scalars uses 32b shift amounts but
9393 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9394 // to be zero.
9395 SDValue ShOps[4];
9396 ShOps[0] = ShAmt;
9397 ShOps[1] = DAG.getConstant(0, MVT::i32);
9398 if (ShAmtVT == MVT::v4i32) {
9399 ShOps[2] = DAG.getUNDEF(MVT::i32);
9400 ShOps[3] = DAG.getUNDEF(MVT::i32);
9401 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9402 } else {
9403 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009404// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009405 }
9406
Owen Andersone50ed302009-08-10 22:56:29 +00009407 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009408 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009409 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009410 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009411 Op.getOperand(1), ShAmt);
9412 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009413 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009414}
Evan Cheng72261582005-12-20 06:22:03 +00009415
Dan Gohmand858e902010-04-17 15:26:15 +00009416SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9417 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009418 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9419 MFI->setReturnAddressIsTaken(true);
9420
Bill Wendling64e87322009-01-16 19:25:27 +00009421 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009422 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009423
9424 if (Depth > 0) {
9425 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9426 SDValue Offset =
9427 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009428 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009429 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009430 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009431 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00009432 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009433 }
9434
9435 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009436 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009437 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009438 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009439}
9440
Dan Gohmand858e902010-04-17 15:26:15 +00009441SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009442 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9443 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009444
Owen Andersone50ed302009-08-10 22:56:29 +00009445 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009446 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009447 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9448 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009449 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009450 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009451 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9452 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00009453 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009454 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009455}
9456
Dan Gohman475871a2008-07-27 21:46:04 +00009457SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009458 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009459 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009460}
9461
Dan Gohmand858e902010-04-17 15:26:15 +00009462SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009463 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009464 SDValue Chain = Op.getOperand(0);
9465 SDValue Offset = Op.getOperand(1);
9466 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009467 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009468
Dan Gohmand8816272010-08-11 18:14:00 +00009469 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9470 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9471 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009472 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009473
Dan Gohmand8816272010-08-11 18:14:00 +00009474 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9475 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009476 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009477 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9478 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009479 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009480 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009481
Dale Johannesene4d209d2009-02-03 20:21:25 +00009482 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009483 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009484 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009485}
9486
Duncan Sands4a544a72011-09-06 13:37:06 +00009487SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9488 SelectionDAG &DAG) const {
9489 return Op.getOperand(0);
9490}
9491
9492SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9493 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009494 SDValue Root = Op.getOperand(0);
9495 SDValue Trmp = Op.getOperand(1); // trampoline
9496 SDValue FPtr = Op.getOperand(2); // nested function
9497 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009498 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009499
Dan Gohman69de1932008-02-06 22:27:42 +00009500 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009501
9502 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009503 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009504
9505 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009506 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9507 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009508
Evan Cheng0e6a0522011-07-18 20:57:22 +00009509 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9510 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009511
9512 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9513
9514 // Load the pointer to the nested function into R11.
9515 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009516 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009517 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009518 Addr, MachinePointerInfo(TrmpAddr),
9519 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009520
Owen Anderson825b72b2009-08-11 20:47:22 +00009521 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9522 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009523 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9524 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009525 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009526
9527 // Load the 'nest' parameter value into R10.
9528 // R10 is specified in X86CallingConv.td
9529 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009530 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9531 DAG.getConstant(10, MVT::i64));
9532 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009533 Addr, MachinePointerInfo(TrmpAddr, 10),
9534 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009535
Owen Anderson825b72b2009-08-11 20:47:22 +00009536 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9537 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009538 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9539 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009540 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009541
9542 // Jump to the nested function.
9543 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009544 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9545 DAG.getConstant(20, MVT::i64));
9546 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009547 Addr, MachinePointerInfo(TrmpAddr, 20),
9548 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009549
9550 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009551 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9552 DAG.getConstant(22, MVT::i64));
9553 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009554 MachinePointerInfo(TrmpAddr, 22),
9555 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009556
Duncan Sands4a544a72011-09-06 13:37:06 +00009557 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009558 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009559 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009560 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009561 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009562 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009563
9564 switch (CC) {
9565 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009566 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009567 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009568 case CallingConv::X86_StdCall: {
9569 // Pass 'nest' parameter in ECX.
9570 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009571 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009572
9573 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009574 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009575 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009576
Chris Lattner58d74912008-03-12 17:45:29 +00009577 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009578 unsigned InRegCount = 0;
9579 unsigned Idx = 1;
9580
9581 for (FunctionType::param_iterator I = FTy->param_begin(),
9582 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009583 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009584 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009585 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009586
9587 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009588 report_fatal_error("Nest register in use - reduce number of inreg"
9589 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009590 }
9591 }
9592 break;
9593 }
9594 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009595 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009596 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009597 // Pass 'nest' parameter in EAX.
9598 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009599 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009600 break;
9601 }
9602
Dan Gohman475871a2008-07-27 21:46:04 +00009603 SDValue OutChains[4];
9604 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009605
Owen Anderson825b72b2009-08-11 20:47:22 +00009606 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9607 DAG.getConstant(10, MVT::i32));
9608 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009609
Chris Lattnera62fe662010-02-05 19:20:30 +00009610 // This is storing the opcode for MOV32ri.
9611 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009612 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009613 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009614 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009615 Trmp, MachinePointerInfo(TrmpAddr),
9616 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009617
Owen Anderson825b72b2009-08-11 20:47:22 +00009618 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9619 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009620 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9621 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009622 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009623
Chris Lattnera62fe662010-02-05 19:20:30 +00009624 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009625 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9626 DAG.getConstant(5, MVT::i32));
9627 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009628 MachinePointerInfo(TrmpAddr, 5),
9629 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009630
Owen Anderson825b72b2009-08-11 20:47:22 +00009631 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9632 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009633 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9634 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009635 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009636
Duncan Sands4a544a72011-09-06 13:37:06 +00009637 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009638 }
9639}
9640
Dan Gohmand858e902010-04-17 15:26:15 +00009641SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9642 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009643 /*
9644 The rounding mode is in bits 11:10 of FPSR, and has the following
9645 settings:
9646 00 Round to nearest
9647 01 Round to -inf
9648 10 Round to +inf
9649 11 Round to 0
9650
9651 FLT_ROUNDS, on the other hand, expects the following:
9652 -1 Undefined
9653 0 Round to 0
9654 1 Round to nearest
9655 2 Round to +inf
9656 3 Round to -inf
9657
9658 To perform the conversion, we do:
9659 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9660 */
9661
9662 MachineFunction &MF = DAG.getMachineFunction();
9663 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009664 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009665 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009666 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009667 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009668
9669 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009670 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009671 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009672
Michael J. Spencerec38de22010-10-10 22:04:20 +00009673
Chris Lattner2156b792010-09-22 01:11:26 +00009674 MachineMemOperand *MMO =
9675 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9676 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009677
Chris Lattner2156b792010-09-22 01:11:26 +00009678 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9679 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9680 DAG.getVTList(MVT::Other),
9681 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009682
9683 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009684 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00009685 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009686
9687 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009688 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009689 DAG.getNode(ISD::SRL, DL, MVT::i16,
9690 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009691 CWD, DAG.getConstant(0x800, MVT::i16)),
9692 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009693 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009694 DAG.getNode(ISD::SRL, DL, MVT::i16,
9695 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009696 CWD, DAG.getConstant(0x400, MVT::i16)),
9697 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009698
Dan Gohman475871a2008-07-27 21:46:04 +00009699 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009700 DAG.getNode(ISD::AND, DL, MVT::i16,
9701 DAG.getNode(ISD::ADD, DL, MVT::i16,
9702 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009703 DAG.getConstant(1, MVT::i16)),
9704 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009705
9706
Duncan Sands83ec4b62008-06-06 12:08:01 +00009707 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009708 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009709}
9710
Dan Gohmand858e902010-04-17 15:26:15 +00009711SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009712 EVT VT = Op.getValueType();
9713 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009714 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009715 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009716
9717 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009718 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009719 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009720 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009721 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009722 }
Evan Cheng18efe262007-12-14 02:13:44 +00009723
Evan Cheng152804e2007-12-14 08:30:15 +00009724 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009725 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009726 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009727
9728 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009729 SDValue Ops[] = {
9730 Op,
9731 DAG.getConstant(NumBits+NumBits-1, OpVT),
9732 DAG.getConstant(X86::COND_E, MVT::i8),
9733 Op.getValue(1)
9734 };
9735 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009736
9737 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009738 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009739
Owen Anderson825b72b2009-08-11 20:47:22 +00009740 if (VT == MVT::i8)
9741 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009742 return Op;
9743}
9744
Dan Gohmand858e902010-04-17 15:26:15 +00009745SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009746 EVT VT = Op.getValueType();
9747 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009748 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009749 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009750
9751 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009752 if (VT == MVT::i8) {
9753 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009754 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009755 }
Evan Cheng152804e2007-12-14 08:30:15 +00009756
9757 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009758 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009759 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009760
9761 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009762 SDValue Ops[] = {
9763 Op,
9764 DAG.getConstant(NumBits, OpVT),
9765 DAG.getConstant(X86::COND_E, MVT::i8),
9766 Op.getValue(1)
9767 };
9768 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009769
Owen Anderson825b72b2009-08-11 20:47:22 +00009770 if (VT == MVT::i8)
9771 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009772 return Op;
9773}
9774
Craig Topper13894fa2011-08-24 06:14:18 +00009775// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9776// ones, and then concatenate the result back.
9777static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009778 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009779
9780 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9781 "Unsupported value type for operation");
9782
9783 int NumElems = VT.getVectorNumElements();
9784 DebugLoc dl = Op.getDebugLoc();
9785 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9786 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9787
9788 // Extract the LHS vectors
9789 SDValue LHS = Op.getOperand(0);
9790 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9791 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9792
9793 // Extract the RHS vectors
9794 SDValue RHS = Op.getOperand(1);
9795 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9796 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9797
9798 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9799 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9800
9801 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9802 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
9803 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
9804}
9805
9806SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
9807 assert(Op.getValueType().getSizeInBits() == 256 &&
9808 Op.getValueType().isInteger() &&
9809 "Only handle AVX 256-bit vector integer operation");
9810 return Lower256IntArith(Op, DAG);
9811}
9812
9813SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
9814 assert(Op.getValueType().getSizeInBits() == 256 &&
9815 Op.getValueType().isInteger() &&
9816 "Only handle AVX 256-bit vector integer operation");
9817 return Lower256IntArith(Op, DAG);
9818}
9819
9820SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
9821 EVT VT = Op.getValueType();
9822
9823 // Decompose 256-bit ops into smaller 128-bit ops.
9824 if (VT.getSizeInBits() == 256)
9825 return Lower256IntArith(Op, DAG);
9826
Owen Anderson825b72b2009-08-11 20:47:22 +00009827 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009828 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00009829
Mon P Wangaf9b9522008-12-18 21:42:19 +00009830 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
9831 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
9832 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
9833 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
9834 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
9835 //
9836 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
9837 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
9838 // return AloBlo + AloBhi + AhiBlo;
9839
9840 SDValue A = Op.getOperand(0);
9841 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009842
Dale Johannesene4d209d2009-02-03 20:21:25 +00009843 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009844 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9845 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009846 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009847 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9848 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009849 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009850 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009851 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009852 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009853 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009854 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009855 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009856 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009857 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009858 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009859 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9860 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009861 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009862 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9863 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009864 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9865 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009866 return Res;
9867}
9868
Nadav Rotem43012222011-05-11 08:12:09 +00009869SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
9870
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009871 EVT VT = Op.getValueType();
9872 DebugLoc dl = Op.getDebugLoc();
9873 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00009874 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009875 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009876
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00009877 if (!Subtarget->hasXMMInt())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009878 return SDValue();
9879
9880 // Decompose 256-bit shifts into smaller 128-bit shifts.
9881 if (VT.getSizeInBits() == 256) {
9882 int NumElems = VT.getVectorNumElements();
9883 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9884 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9885
9886 // Extract the two vectors
9887 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
9888 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
9889 DAG, dl);
9890
9891 // Recreate the shift amount vectors
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009892 SDValue Amt1, Amt2;
9893 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
9894 // Constant shift amount
9895 SmallVector<SDValue, 4> Amt1Csts;
9896 SmallVector<SDValue, 4> Amt2Csts;
9897 for (int i = 0; i < NumElems/2; ++i)
9898 Amt1Csts.push_back(Amt->getOperand(i));
9899 for (int i = NumElems/2; i < NumElems; ++i)
9900 Amt2Csts.push_back(Amt->getOperand(i));
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009901
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009902 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9903 &Amt1Csts[0], NumElems/2);
9904 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9905 &Amt2Csts[0], NumElems/2);
9906 } else {
9907 // Variable shift amount
9908 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
9909 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
9910 DAG, dl);
9911 }
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009912
9913 // Issue new vector shifts for the smaller types
9914 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
9915 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
9916
9917 // Concatenate the result back
9918 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
9919 }
Nate Begeman51409212010-07-28 00:21:48 +00009920
Nadav Rotem43012222011-05-11 08:12:09 +00009921 // Optimize shl/srl/sra with constant shift amount.
9922 if (isSplatVector(Amt.getNode())) {
9923 SDValue SclrAmt = Amt->getOperand(0);
9924 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
9925 uint64_t ShiftAmt = C->getZExtValue();
9926
9927 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
9928 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9929 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9930 R, DAG.getConstant(ShiftAmt, MVT::i32));
9931
9932 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
9933 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9934 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9935 R, DAG.getConstant(ShiftAmt, MVT::i32));
9936
9937 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
9938 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9939 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9940 R, DAG.getConstant(ShiftAmt, MVT::i32));
9941
9942 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
9943 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9944 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9945 R, DAG.getConstant(ShiftAmt, MVT::i32));
9946
9947 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
9948 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9949 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
9950 R, DAG.getConstant(ShiftAmt, MVT::i32));
9951
9952 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
9953 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9954 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
9955 R, DAG.getConstant(ShiftAmt, MVT::i32));
9956
9957 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
9958 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9959 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
9960 R, DAG.getConstant(ShiftAmt, MVT::i32));
9961
9962 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
9963 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9964 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
9965 R, DAG.getConstant(ShiftAmt, MVT::i32));
9966 }
9967 }
9968
9969 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00009970 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009971 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9972 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9973 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
9974
9975 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009976
Nate Begeman51409212010-07-28 00:21:48 +00009977 std::vector<Constant*> CV(4, CI);
9978 Constant *C = ConstantVector::get(CV);
9979 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9980 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009981 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009982 false, false, 16);
9983
9984 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009985 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009986 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
9987 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
9988 }
Nadav Rotem43012222011-05-11 08:12:09 +00009989 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009990 // a = a << 5;
9991 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9992 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9993 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
9994
9995 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
9996 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
9997
9998 std::vector<Constant*> CVM1(16, CM1);
9999 std::vector<Constant*> CVM2(16, CM2);
10000 Constant *C = ConstantVector::get(CVM1);
10001 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10002 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010003 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +000010004 false, false, 16);
10005
10006 // r = pblendv(r, psllw(r & (char16)15, 4), a);
10007 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10008 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10009 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10010 DAG.getConstant(4, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010011 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010012 // a += a
10013 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010014
Nate Begeman51409212010-07-28 00:21:48 +000010015 C = ConstantVector::get(CVM2);
10016 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10017 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010018 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010019 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010020
Nate Begeman51409212010-07-28 00:21:48 +000010021 // r = pblendv(r, psllw(r & (char16)63, 2), a);
10022 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10023 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10024 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10025 DAG.getConstant(2, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010026 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010027 // a += a
10028 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010029
Nate Begeman51409212010-07-28 00:21:48 +000010030 // return pblendv(r, r+r, a);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010031 R = DAG.getNode(ISD::VSELECT, dl, VT, Op,
10032 R, DAG.getNode(ISD::ADD, dl, VT, R, R));
Nate Begeman51409212010-07-28 00:21:48 +000010033 return R;
10034 }
10035 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010036}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010037
Dan Gohmand858e902010-04-17 15:26:15 +000010038SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010039 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10040 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010041 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10042 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010043 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010044 SDValue LHS = N->getOperand(0);
10045 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010046 unsigned BaseOp = 0;
10047 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010048 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010049 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010050 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010051 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010052 // A subtract of one will be selected as a INC. Note that INC doesn't
10053 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010054 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10055 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010056 BaseOp = X86ISD::INC;
10057 Cond = X86::COND_O;
10058 break;
10059 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010060 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010061 Cond = X86::COND_O;
10062 break;
10063 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010064 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010065 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010066 break;
10067 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010068 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10069 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010070 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10071 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010072 BaseOp = X86ISD::DEC;
10073 Cond = X86::COND_O;
10074 break;
10075 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010076 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010077 Cond = X86::COND_O;
10078 break;
10079 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010080 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010081 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010082 break;
10083 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010084 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010085 Cond = X86::COND_O;
10086 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010087 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10088 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10089 MVT::i32);
10090 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010091
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010092 SDValue SetCC =
10093 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10094 DAG.getConstant(X86::COND_O, MVT::i32),
10095 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010096
Dan Gohman6e5fda22011-07-22 18:45:15 +000010097 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010098 }
Bill Wendling74c37652008-12-09 22:08:41 +000010099 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010100
Bill Wendling61edeb52008-12-02 01:06:39 +000010101 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010102 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010103 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010104
Bill Wendling61edeb52008-12-02 01:06:39 +000010105 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010106 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10107 DAG.getConstant(Cond, MVT::i32),
10108 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010109
Dan Gohman6e5fda22011-07-22 18:45:15 +000010110 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010111}
10112
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010113SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10114 DebugLoc dl = Op.getDebugLoc();
10115 SDNode* Node = Op.getNode();
10116 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
10117 EVT VT = Node->getValueType(0);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010118 if (Subtarget->hasXMMInt() && VT.isVector()) {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010119 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10120 ExtraVT.getScalarType().getSizeInBits();
10121 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10122
10123 unsigned SHLIntrinsicsID = 0;
10124 unsigned SRAIntrinsicsID = 0;
10125 switch (VT.getSimpleVT().SimpleTy) {
10126 default:
10127 return SDValue();
10128 case MVT::v2i64: {
10129 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_q;
10130 SRAIntrinsicsID = 0;
10131 break;
10132 }
10133 case MVT::v4i32: {
10134 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10135 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10136 break;
10137 }
10138 case MVT::v8i16: {
10139 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10140 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10141 break;
10142 }
10143 }
10144
10145 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10146 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
10147 Node->getOperand(0), ShAmt);
10148
10149 // In case of 1 bit sext, no need to shr
10150 if (ExtraVT.getScalarType().getSizeInBits() == 1) return Tmp1;
10151
10152 if (SRAIntrinsicsID) {
10153 Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10154 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10155 Tmp1, ShAmt);
10156 }
10157 return Tmp1;
10158 }
10159
10160 return SDValue();
10161}
10162
10163
Eric Christopher9a9d2752010-07-22 02:48:34 +000010164SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10165 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010166
Eric Christopher77ed1352011-07-08 00:04:56 +000010167 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10168 // There isn't any reason to disable it if the target processor supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010169 if (!Subtarget->hasXMMInt() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010170 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010171 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010172 SDValue Ops[] = {
10173 DAG.getRegister(X86::ESP, MVT::i32), // Base
10174 DAG.getTargetConstant(1, MVT::i8), // Scale
10175 DAG.getRegister(0, MVT::i32), // Index
10176 DAG.getTargetConstant(0, MVT::i32), // Disp
10177 DAG.getRegister(0, MVT::i32), // Segment.
10178 Zero,
10179 Chain
10180 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010181 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010182 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10183 array_lengthof(Ops));
10184 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010185 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010186
Eric Christopher9a9d2752010-07-22 02:48:34 +000010187 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010188 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010189 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010190
Chris Lattner132929a2010-08-14 17:26:09 +000010191 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10192 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10193 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10194 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010195
Chris Lattner132929a2010-08-14 17:26:09 +000010196 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10197 if (!Op1 && !Op2 && !Op3 && Op4)
10198 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010199
Chris Lattner132929a2010-08-14 17:26:09 +000010200 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10201 if (Op1 && !Op2 && !Op3 && !Op4)
10202 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010203
10204 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010205 // (MFENCE)>;
10206 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010207}
10208
Eli Friedman14648462011-07-27 22:21:52 +000010209SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10210 SelectionDAG &DAG) const {
10211 DebugLoc dl = Op.getDebugLoc();
10212 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10213 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10214 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10215 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10216
10217 // The only fence that needs an instruction is a sequentially-consistent
10218 // cross-thread fence.
10219 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10220 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10221 // no-sse2). There isn't any reason to disable it if the target processor
10222 // supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010223 if (Subtarget->hasXMMInt() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010224 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10225
10226 SDValue Chain = Op.getOperand(0);
10227 SDValue Zero = DAG.getConstant(0, MVT::i32);
10228 SDValue Ops[] = {
10229 DAG.getRegister(X86::ESP, MVT::i32), // Base
10230 DAG.getTargetConstant(1, MVT::i8), // Scale
10231 DAG.getRegister(0, MVT::i32), // Index
10232 DAG.getTargetConstant(0, MVT::i32), // Disp
10233 DAG.getRegister(0, MVT::i32), // Segment.
10234 Zero,
10235 Chain
10236 };
10237 SDNode *Res =
10238 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10239 array_lengthof(Ops));
10240 return SDValue(Res, 0);
10241 }
10242
10243 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10244 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10245}
10246
10247
Dan Gohmand858e902010-04-17 15:26:15 +000010248SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010249 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010250 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010251 unsigned Reg = 0;
10252 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010253 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010254 default:
10255 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010256 case MVT::i8: Reg = X86::AL; size = 1; break;
10257 case MVT::i16: Reg = X86::AX; size = 2; break;
10258 case MVT::i32: Reg = X86::EAX; size = 4; break;
10259 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010260 assert(Subtarget->is64Bit() && "Node not type legal!");
10261 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010262 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010263 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010264 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010265 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010266 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010267 Op.getOperand(1),
10268 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010269 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010270 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010271 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010272 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10273 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10274 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010275 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010276 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010277 return cpOut;
10278}
10279
Duncan Sands1607f052008-12-01 11:39:25 +000010280SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010281 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010282 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010283 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010284 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010285 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010286 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010287 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10288 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010289 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010290 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10291 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010292 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010293 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010294 rdx.getValue(1)
10295 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010296 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010297}
10298
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010299SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010300 SelectionDAG &DAG) const {
10301 EVT SrcVT = Op.getOperand(0).getValueType();
10302 EVT DstVT = Op.getValueType();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010303 assert(Subtarget->is64Bit() && !Subtarget->hasXMMInt() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010304 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010305 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010306 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010307 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010308 // i64 <=> MMX conversions are Legal.
10309 if (SrcVT==MVT::i64 && DstVT.isVector())
10310 return Op;
10311 if (DstVT==MVT::i64 && SrcVT.isVector())
10312 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010313 // MMX <=> MMX conversions are Legal.
10314 if (SrcVT.isVector() && DstVT.isVector())
10315 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010316 // All other conversions need to be expanded.
10317 return SDValue();
10318}
Chris Lattner5b856542010-12-20 00:59:46 +000010319
Dan Gohmand858e902010-04-17 15:26:15 +000010320SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010321 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010322 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010323 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010324 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010325 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010326 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010327 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010328 Node->getOperand(0),
10329 Node->getOperand(1), negOp,
10330 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010331 cast<AtomicSDNode>(Node)->getAlignment(),
10332 cast<AtomicSDNode>(Node)->getOrdering(),
10333 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010334}
10335
Eli Friedman327236c2011-08-24 20:50:09 +000010336static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10337 SDNode *Node = Op.getNode();
10338 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010339 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010340
10341 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010342 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10343 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10344 // (The only way to get a 16-byte store is cmpxchg16b)
10345 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10346 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10347 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010348 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10349 cast<AtomicSDNode>(Node)->getMemoryVT(),
10350 Node->getOperand(0),
10351 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010352 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010353 cast<AtomicSDNode>(Node)->getOrdering(),
10354 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010355 return Swap.getValue(1);
10356 }
10357 // Other atomic stores have a simple pattern.
10358 return Op;
10359}
10360
Chris Lattner5b856542010-12-20 00:59:46 +000010361static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10362 EVT VT = Op.getNode()->getValueType(0);
10363
10364 // Let legalize expand this if it isn't a legal type yet.
10365 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10366 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010367
Chris Lattner5b856542010-12-20 00:59:46 +000010368 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010369
Chris Lattner5b856542010-12-20 00:59:46 +000010370 unsigned Opc;
10371 bool ExtraOp = false;
10372 switch (Op.getOpcode()) {
10373 default: assert(0 && "Invalid code");
10374 case ISD::ADDC: Opc = X86ISD::ADD; break;
10375 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10376 case ISD::SUBC: Opc = X86ISD::SUB; break;
10377 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10378 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010379
Chris Lattner5b856542010-12-20 00:59:46 +000010380 if (!ExtraOp)
10381 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10382 Op.getOperand(1));
10383 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10384 Op.getOperand(1), Op.getOperand(2));
10385}
10386
Evan Cheng0db9fe62006-04-25 20:13:52 +000010387/// LowerOperation - Provide custom lowering hooks for some operations.
10388///
Dan Gohmand858e902010-04-17 15:26:15 +000010389SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010390 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010391 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010392 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010393 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010394 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010395 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10396 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010397 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010398 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010399 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010400 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10401 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10402 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010403 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010404 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010405 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10406 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10407 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010408 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010409 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010410 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010411 case ISD::SHL_PARTS:
10412 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010413 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010414 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010415 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010416 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010417 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010418 case ISD::FABS: return LowerFABS(Op, DAG);
10419 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010420 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010421 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010422 case ISD::SETCC: return LowerSETCC(Op, DAG);
10423 case ISD::SELECT: return LowerSELECT(Op, DAG);
10424 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010425 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010426 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010427 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010428 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010429 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010430 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10431 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010432 case ISD::FRAME_TO_ARGS_OFFSET:
10433 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010434 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010435 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010436 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10437 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010438 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010439 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10440 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010441 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010442 case ISD::SRA:
10443 case ISD::SRL:
10444 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010445 case ISD::SADDO:
10446 case ISD::UADDO:
10447 case ISD::SSUBO:
10448 case ISD::USUBO:
10449 case ISD::SMULO:
10450 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010451 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010452 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010453 case ISD::ADDC:
10454 case ISD::ADDE:
10455 case ISD::SUBC:
10456 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010457 case ISD::ADD: return LowerADD(Op, DAG);
10458 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010459 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010460}
10461
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010462static void ReplaceATOMIC_LOAD(SDNode *Node,
10463 SmallVectorImpl<SDValue> &Results,
10464 SelectionDAG &DAG) {
10465 DebugLoc dl = Node->getDebugLoc();
10466 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10467
10468 // Convert wide load -> cmpxchg8b/cmpxchg16b
10469 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10470 // (The only way to get a 16-byte load is cmpxchg16b)
10471 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010472 SDValue Zero = DAG.getConstant(0, VT);
10473 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010474 Node->getOperand(0),
10475 Node->getOperand(1), Zero, Zero,
10476 cast<AtomicSDNode>(Node)->getMemOperand(),
10477 cast<AtomicSDNode>(Node)->getOrdering(),
10478 cast<AtomicSDNode>(Node)->getSynchScope());
10479 Results.push_back(Swap.getValue(0));
10480 Results.push_back(Swap.getValue(1));
10481}
10482
Duncan Sands1607f052008-12-01 11:39:25 +000010483void X86TargetLowering::
10484ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010485 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010486 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010487 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +000010488 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010489
10490 SDValue Chain = Node->getOperand(0);
10491 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010492 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010493 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010494 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010495 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010496 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010497 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010498 SDValue Result =
10499 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10500 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010501 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010502 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010503 Results.push_back(Result.getValue(2));
10504}
10505
Duncan Sands126d9072008-07-04 11:47:58 +000010506/// ReplaceNodeResults - Replace a node with an illegal result type
10507/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010508void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10509 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010510 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010511 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010512 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010513 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010514 assert(false && "Do not know how to custom type legalize this operation!");
10515 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010516 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010517 case ISD::ADDC:
10518 case ISD::ADDE:
10519 case ISD::SUBC:
10520 case ISD::SUBE:
10521 // We don't want to expand or promote these.
10522 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010523 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010524 std::pair<SDValue,SDValue> Vals =
10525 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010526 SDValue FIST = Vals.first, StackSlot = Vals.second;
10527 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010528 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010529 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010530 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
10531 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010532 }
10533 return;
10534 }
10535 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010536 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010537 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010538 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010539 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010540 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010541 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010542 eax.getValue(2));
10543 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10544 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010545 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010546 Results.push_back(edx.getValue(1));
10547 return;
10548 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010549 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010550 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010551 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010552 bool Regs64bit = T == MVT::i128;
10553 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010554 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010555 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10556 DAG.getConstant(0, HalfT));
10557 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10558 DAG.getConstant(1, HalfT));
10559 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10560 Regs64bit ? X86::RAX : X86::EAX,
10561 cpInL, SDValue());
10562 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10563 Regs64bit ? X86::RDX : X86::EDX,
10564 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010565 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010566 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10567 DAG.getConstant(0, HalfT));
10568 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10569 DAG.getConstant(1, HalfT));
10570 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10571 Regs64bit ? X86::RBX : X86::EBX,
10572 swapInL, cpInH.getValue(1));
10573 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10574 Regs64bit ? X86::RCX : X86::ECX,
10575 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010576 SDValue Ops[] = { swapInH.getValue(0),
10577 N->getOperand(1),
10578 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010579 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010580 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010581 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10582 X86ISD::LCMPXCHG8_DAG;
10583 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010584 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010585 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10586 Regs64bit ? X86::RAX : X86::EAX,
10587 HalfT, Result.getValue(1));
10588 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10589 Regs64bit ? X86::RDX : X86::EDX,
10590 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010591 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010592 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010593 Results.push_back(cpOutH.getValue(1));
10594 return;
10595 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010596 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010597 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10598 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010599 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010600 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10601 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010602 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010603 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10604 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010605 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010606 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10607 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010608 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010609 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10610 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010611 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010612 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10613 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010614 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010615 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10616 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010617 case ISD::ATOMIC_LOAD:
10618 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010619 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010620}
10621
Evan Cheng72261582005-12-20 06:22:03 +000010622const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10623 switch (Opcode) {
10624 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010625 case X86ISD::BSF: return "X86ISD::BSF";
10626 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010627 case X86ISD::SHLD: return "X86ISD::SHLD";
10628 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010629 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010630 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010631 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010632 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010633 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010634 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010635 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10636 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10637 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010638 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010639 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010640 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010641 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010642 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010643 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010644 case X86ISD::COMI: return "X86ISD::COMI";
10645 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010646 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010647 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010648 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10649 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010650 case X86ISD::CMOV: return "X86ISD::CMOV";
10651 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010652 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010653 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10654 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010655 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010656 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010657 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010658 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010659 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010660 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10661 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010662 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010663 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010664 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +000010665 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
10666 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
10667 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Evan Cheng8ca29322006-11-10 21:43:37 +000010668 case X86ISD::FMAX: return "X86ISD::FMAX";
10669 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000010670 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
10671 case X86ISD::FRCP: return "X86ISD::FRCP";
Duncan Sands17470be2011-09-22 20:15:48 +000010672 case X86ISD::FHADD: return "X86ISD::FHADD";
10673 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010674 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000010675 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010676 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000010677 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010678 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000010679 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
10680 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010681 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
10682 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
10683 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
10684 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
10685 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
10686 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000010687 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
10688 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000010689 case X86ISD::VSHL: return "X86ISD::VSHL";
10690 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000010691 case X86ISD::CMPPD: return "X86ISD::CMPPD";
10692 case X86ISD::CMPPS: return "X86ISD::CMPPS";
10693 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
10694 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
10695 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
10696 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
10697 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
10698 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
10699 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
10700 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010701 case X86ISD::ADD: return "X86ISD::ADD";
10702 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000010703 case X86ISD::ADC: return "X86ISD::ADC";
10704 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000010705 case X86ISD::SMUL: return "X86ISD::SMUL";
10706 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000010707 case X86ISD::INC: return "X86ISD::INC";
10708 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000010709 case X86ISD::OR: return "X86ISD::OR";
10710 case X86ISD::XOR: return "X86ISD::XOR";
10711 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +000010712 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000010713 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010714 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010715 case X86ISD::PALIGN: return "X86ISD::PALIGN";
10716 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
10717 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
10718 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
10719 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
10720 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
10721 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
10722 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
10723 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010724 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000010725 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010726 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000010727 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
10728 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010729 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
10730 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
10731 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
10732 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
10733 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
10734 case X86ISD::MOVSD: return "X86ISD::MOVSD";
10735 case X86ISD::MOVSS: return "X86ISD::MOVSS";
10736 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
10737 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +000010738 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010739 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
10740 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
10741 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
10742 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
10743 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
10744 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
10745 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
10746 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
10747 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
10748 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000010749 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000010750 case X86ISD::VPERMILPS: return "X86ISD::VPERMILPS";
10751 case X86ISD::VPERMILPSY: return "X86ISD::VPERMILPSY";
10752 case X86ISD::VPERMILPD: return "X86ISD::VPERMILPD";
10753 case X86ISD::VPERMILPDY: return "X86ISD::VPERMILPDY";
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000010754 case X86ISD::VPERM2F128: return "X86ISD::VPERM2F128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000010755 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000010756 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010757 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000010758 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000010759 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000010760 }
10761}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010762
Chris Lattnerc9addb72007-03-30 23:15:24 +000010763// isLegalAddressingMode - Return true if the addressing mode represented
10764// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000010765bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010766 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000010767 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010768 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000010769 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000010770
Chris Lattnerc9addb72007-03-30 23:15:24 +000010771 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010772 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010773 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000010774
Chris Lattnerc9addb72007-03-30 23:15:24 +000010775 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000010776 unsigned GVFlags =
10777 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010778
Chris Lattnerdfed4132009-07-10 07:38:24 +000010779 // If a reference to this global requires an extra load, we can't fold it.
10780 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010781 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010782
Chris Lattnerdfed4132009-07-10 07:38:24 +000010783 // If BaseGV requires a register for the PIC base, we cannot also have a
10784 // BaseReg specified.
10785 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000010786 return false;
Evan Cheng52787842007-08-01 23:46:47 +000010787
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010788 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000010789 if ((M != CodeModel::Small || R != Reloc::Static) &&
10790 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010791 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000010792 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010793
Chris Lattnerc9addb72007-03-30 23:15:24 +000010794 switch (AM.Scale) {
10795 case 0:
10796 case 1:
10797 case 2:
10798 case 4:
10799 case 8:
10800 // These scales always work.
10801 break;
10802 case 3:
10803 case 5:
10804 case 9:
10805 // These scales are formed with basereg+scalereg. Only accept if there is
10806 // no basereg yet.
10807 if (AM.HasBaseReg)
10808 return false;
10809 break;
10810 default: // Other stuff never works.
10811 return false;
10812 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010813
Chris Lattnerc9addb72007-03-30 23:15:24 +000010814 return true;
10815}
10816
10817
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010818bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010819 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000010820 return false;
Evan Chenge127a732007-10-29 07:57:50 +000010821 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
10822 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010823 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000010824 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010825 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000010826}
10827
Owen Andersone50ed302009-08-10 22:56:29 +000010828bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010829 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010830 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010831 unsigned NumBits1 = VT1.getSizeInBits();
10832 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010833 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010834 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010835 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010836}
Evan Cheng2bd122c2007-10-26 01:56:11 +000010837
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010838bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010839 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010840 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010841}
10842
Owen Andersone50ed302009-08-10 22:56:29 +000010843bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010844 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000010845 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010846}
10847
Owen Andersone50ed302009-08-10 22:56:29 +000010848bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000010849 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000010850 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000010851}
10852
Evan Cheng60c07e12006-07-05 22:17:51 +000010853/// isShuffleMaskLegal - Targets can use this to indicate that they only
10854/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
10855/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
10856/// are assumed to be legal.
10857bool
Eric Christopherfd179292009-08-27 18:07:15 +000010858X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000010859 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000010860 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000010861 if (VT.getSizeInBits() == 64)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010862 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX());
Nate Begeman9008ca62009-04-27 18:41:29 +000010863
Nate Begemana09008b2009-10-19 02:17:23 +000010864 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000010865 return (VT.getVectorNumElements() == 2 ||
10866 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
10867 isMOVLMask(M, VT) ||
10868 isSHUFPMask(M, VT) ||
10869 isPSHUFDMask(M, VT) ||
10870 isPSHUFHWMask(M, VT) ||
10871 isPSHUFLWMask(M, VT) ||
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010872 isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000010873 isUNPCKLMask(M, VT) ||
10874 isUNPCKHMask(M, VT) ||
10875 isUNPCKL_v_undef_Mask(M, VT) ||
10876 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010877}
10878
Dan Gohman7d8143f2008-04-09 20:09:42 +000010879bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000010880X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000010881 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000010882 unsigned NumElts = VT.getVectorNumElements();
10883 // FIXME: This collection of masks seems suspect.
10884 if (NumElts == 2)
10885 return true;
10886 if (NumElts == 4 && VT.getSizeInBits() == 128) {
10887 return (isMOVLMask(Mask, VT) ||
10888 isCommutedMOVLMask(Mask, VT, true) ||
10889 isSHUFPMask(Mask, VT) ||
10890 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010891 }
10892 return false;
10893}
10894
10895//===----------------------------------------------------------------------===//
10896// X86 Scheduler Hooks
10897//===----------------------------------------------------------------------===//
10898
Mon P Wang63307c32008-05-05 19:05:59 +000010899// private utility function
10900MachineBasicBlock *
10901X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
10902 MachineBasicBlock *MBB,
10903 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010904 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010905 unsigned LoadOpc,
10906 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010907 unsigned notOpc,
10908 unsigned EAXreg,
10909 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010910 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000010911 // For the atomic bitwise operator, we generate
10912 // thisMBB:
10913 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000010914 // ld t1 = [bitinstr.addr]
10915 // op t2 = t1, [bitinstr.val]
10916 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000010917 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
10918 // bz newMBB
10919 // fallthrough -->nextMBB
10920 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10921 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010922 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000010923 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010924
Mon P Wang63307c32008-05-05 19:05:59 +000010925 /// First build the CFG
10926 MachineFunction *F = MBB->getParent();
10927 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010928 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10929 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10930 F->insert(MBBIter, newMBB);
10931 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010932
Dan Gohman14152b42010-07-06 20:24:04 +000010933 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10934 nextMBB->splice(nextMBB->begin(), thisMBB,
10935 llvm::next(MachineBasicBlock::iterator(bInstr)),
10936 thisMBB->end());
10937 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010938
Mon P Wang63307c32008-05-05 19:05:59 +000010939 // Update thisMBB to fall through to newMBB
10940 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010941
Mon P Wang63307c32008-05-05 19:05:59 +000010942 // newMBB jumps to itself and fall through to nextMBB
10943 newMBB->addSuccessor(nextMBB);
10944 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010945
Mon P Wang63307c32008-05-05 19:05:59 +000010946 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010947 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010948 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000010949 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010950 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010951 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010952 int numArgs = bInstr->getNumOperands() - 1;
10953 for (int i=0; i < numArgs; ++i)
10954 argOpers[i] = &bInstr->getOperand(i+1);
10955
10956 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010957 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010958 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010959
Dale Johannesen140be2d2008-08-19 18:47:28 +000010960 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010961 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010962 for (int i=0; i <= lastAddrIndx; ++i)
10963 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010964
Dale Johannesen140be2d2008-08-19 18:47:28 +000010965 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010966 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010967 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010968 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010969 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010970 tt = t1;
10971
Dale Johannesen140be2d2008-08-19 18:47:28 +000010972 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000010973 assert((argOpers[valArgIndx]->isReg() ||
10974 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010975 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000010976 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000010977 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010978 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010979 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010980 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000010981 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010982
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010983 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000010984 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010985
Dale Johannesene4d209d2009-02-03 20:21:25 +000010986 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000010987 for (int i=0; i <= lastAddrIndx; ++i)
10988 (*MIB).addOperand(*argOpers[i]);
10989 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000010990 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010991 (*MIB).setMemRefs(bInstr->memoperands_begin(),
10992 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000010993
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010994 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000010995 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010996
Mon P Wang63307c32008-05-05 19:05:59 +000010997 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010998 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010999
Dan Gohman14152b42010-07-06 20:24:04 +000011000 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011001 return nextMBB;
11002}
11003
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011004// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011005MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011006X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11007 MachineBasicBlock *MBB,
11008 unsigned regOpcL,
11009 unsigned regOpcH,
11010 unsigned immOpcL,
11011 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011012 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011013 // For the atomic bitwise operator, we generate
11014 // thisMBB (instructions are in pairs, except cmpxchg8b)
11015 // ld t1,t2 = [bitinstr.addr]
11016 // newMBB:
11017 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11018 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011019 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011020 // mov ECX, EBX <- t5, t6
11021 // mov EAX, EDX <- t1, t2
11022 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11023 // mov t3, t4 <- EAX, EDX
11024 // bz newMBB
11025 // result in out1, out2
11026 // fallthrough -->nextMBB
11027
11028 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11029 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011030 const unsigned NotOpc = X86::NOT32r;
11031 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11032 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11033 MachineFunction::iterator MBBIter = MBB;
11034 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011035
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011036 /// First build the CFG
11037 MachineFunction *F = MBB->getParent();
11038 MachineBasicBlock *thisMBB = MBB;
11039 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11040 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11041 F->insert(MBBIter, newMBB);
11042 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011043
Dan Gohman14152b42010-07-06 20:24:04 +000011044 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11045 nextMBB->splice(nextMBB->begin(), thisMBB,
11046 llvm::next(MachineBasicBlock::iterator(bInstr)),
11047 thisMBB->end());
11048 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011049
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011050 // Update thisMBB to fall through to newMBB
11051 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011052
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011053 // newMBB jumps to itself and fall through to nextMBB
11054 newMBB->addSuccessor(nextMBB);
11055 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011056
Dale Johannesene4d209d2009-02-03 20:21:25 +000011057 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011058 // Insert instructions into newMBB based on incoming instruction
11059 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011060 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011061 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011062 MachineOperand& dest1Oper = bInstr->getOperand(0);
11063 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011064 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11065 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011066 argOpers[i] = &bInstr->getOperand(i+2);
11067
Dan Gohman71ea4e52010-05-14 21:01:44 +000011068 // We use some of the operands multiple times, so conservatively just
11069 // clear any kill flags that might be present.
11070 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11071 argOpers[i]->setIsKill(false);
11072 }
11073
Evan Chengad5b52f2010-01-08 19:14:57 +000011074 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011075 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011076
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011077 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011078 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011079 for (int i=0; i <= lastAddrIndx; ++i)
11080 (*MIB).addOperand(*argOpers[i]);
11081 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011082 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011083 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011084 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011085 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011086 MachineOperand newOp3 = *(argOpers[3]);
11087 if (newOp3.isImm())
11088 newOp3.setImm(newOp3.getImm()+4);
11089 else
11090 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011091 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011092 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011093
11094 // t3/4 are defined later, at the bottom of the loop
11095 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11096 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011097 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011098 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011099 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011100 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11101
Evan Cheng306b4ca2010-01-08 23:41:50 +000011102 // The subsequent operations should be using the destination registers of
11103 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011104 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011105 t1 = F->getRegInfo().createVirtualRegister(RC);
11106 t2 = F->getRegInfo().createVirtualRegister(RC);
11107 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11108 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011109 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011110 t1 = dest1Oper.getReg();
11111 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011112 }
11113
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011114 int valArgIndx = lastAddrIndx + 1;
11115 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011116 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011117 "invalid operand");
11118 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11119 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011120 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011121 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011122 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011123 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011124 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011125 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011126 (*MIB).addOperand(*argOpers[valArgIndx]);
11127 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011128 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011129 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011130 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011131 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011132 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011133 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011134 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011135 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011136 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011137 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011138
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011139 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011140 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011141 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011142 MIB.addReg(t2);
11143
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011144 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011145 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011146 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011147 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011148
Dale Johannesene4d209d2009-02-03 20:21:25 +000011149 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011150 for (int i=0; i <= lastAddrIndx; ++i)
11151 (*MIB).addOperand(*argOpers[i]);
11152
11153 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011154 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11155 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011156
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011157 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011158 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011159 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011160 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011161
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011162 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011163 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011164
Dan Gohman14152b42010-07-06 20:24:04 +000011165 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011166 return nextMBB;
11167}
11168
11169// private utility function
11170MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011171X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11172 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011173 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011174 // For the atomic min/max operator, we generate
11175 // thisMBB:
11176 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011177 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011178 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011179 // cmp t1, t2
11180 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011181 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011182 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11183 // bz newMBB
11184 // fallthrough -->nextMBB
11185 //
11186 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11187 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011188 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011189 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011190
Mon P Wang63307c32008-05-05 19:05:59 +000011191 /// First build the CFG
11192 MachineFunction *F = MBB->getParent();
11193 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011194 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11195 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11196 F->insert(MBBIter, newMBB);
11197 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011198
Dan Gohman14152b42010-07-06 20:24:04 +000011199 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11200 nextMBB->splice(nextMBB->begin(), thisMBB,
11201 llvm::next(MachineBasicBlock::iterator(mInstr)),
11202 thisMBB->end());
11203 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011204
Mon P Wang63307c32008-05-05 19:05:59 +000011205 // Update thisMBB to fall through to newMBB
11206 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011207
Mon P Wang63307c32008-05-05 19:05:59 +000011208 // newMBB jumps to newMBB and fall through to nextMBB
11209 newMBB->addSuccessor(nextMBB);
11210 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011211
Dale Johannesene4d209d2009-02-03 20:21:25 +000011212 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011213 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011214 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011215 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011216 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011217 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011218 int numArgs = mInstr->getNumOperands() - 1;
11219 for (int i=0; i < numArgs; ++i)
11220 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011221
Mon P Wang63307c32008-05-05 19:05:59 +000011222 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011223 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011224 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011225
Mon P Wangab3e7472008-05-05 22:56:23 +000011226 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011227 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011228 for (int i=0; i <= lastAddrIndx; ++i)
11229 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011230
Mon P Wang63307c32008-05-05 19:05:59 +000011231 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011232 assert((argOpers[valArgIndx]->isReg() ||
11233 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011234 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011235
11236 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011237 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011238 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011239 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011240 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011241 (*MIB).addOperand(*argOpers[valArgIndx]);
11242
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011243 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011244 MIB.addReg(t1);
11245
Dale Johannesene4d209d2009-02-03 20:21:25 +000011246 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011247 MIB.addReg(t1);
11248 MIB.addReg(t2);
11249
11250 // Generate movc
11251 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011252 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011253 MIB.addReg(t2);
11254 MIB.addReg(t1);
11255
11256 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011257 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011258 for (int i=0; i <= lastAddrIndx; ++i)
11259 (*MIB).addOperand(*argOpers[i]);
11260 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011261 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011262 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11263 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011264
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011265 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011266 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011267
Mon P Wang63307c32008-05-05 19:05:59 +000011268 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011269 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011270
Dan Gohman14152b42010-07-06 20:24:04 +000011271 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011272 return nextMBB;
11273}
11274
Eric Christopherf83a5de2009-08-27 18:08:16 +000011275// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011276// or XMM0_V32I8 in AVX all of this code can be replaced with that
11277// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011278MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011279X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011280 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011281 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
11282 "Target must have SSE4.2 or AVX features enabled");
11283
Eric Christopherb120ab42009-08-18 22:50:32 +000011284 DebugLoc dl = MI->getDebugLoc();
11285 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011286 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011287 if (!Subtarget->hasAVX()) {
11288 if (memArg)
11289 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11290 else
11291 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11292 } else {
11293 if (memArg)
11294 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11295 else
11296 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11297 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011298
Eric Christopher41c902f2010-11-30 08:20:21 +000011299 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011300 for (unsigned i = 0; i < numArgs; ++i) {
11301 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011302 if (!(Op.isReg() && Op.isImplicit()))
11303 MIB.addOperand(Op);
11304 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011305 BuildMI(*BB, MI, dl,
11306 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11307 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011308 .addReg(X86::XMM0);
11309
Dan Gohman14152b42010-07-06 20:24:04 +000011310 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011311 return BB;
11312}
11313
11314MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011315X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011316 DebugLoc dl = MI->getDebugLoc();
11317 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011318
Eric Christopher228232b2010-11-30 07:20:12 +000011319 // Address into RAX/EAX, other two args into ECX, EDX.
11320 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11321 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11322 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11323 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011324 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011325
Eric Christopher228232b2010-11-30 07:20:12 +000011326 unsigned ValOps = X86::AddrNumOperands;
11327 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11328 .addReg(MI->getOperand(ValOps).getReg());
11329 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11330 .addReg(MI->getOperand(ValOps+1).getReg());
11331
11332 // The instruction doesn't actually take any operands though.
11333 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011334
Eric Christopher228232b2010-11-30 07:20:12 +000011335 MI->eraseFromParent(); // The pseudo is gone now.
11336 return BB;
11337}
11338
11339MachineBasicBlock *
11340X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011341 DebugLoc dl = MI->getDebugLoc();
11342 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011343
Eric Christopher228232b2010-11-30 07:20:12 +000011344 // First arg in ECX, the second in EAX.
11345 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11346 .addReg(MI->getOperand(0).getReg());
11347 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11348 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011349
Eric Christopher228232b2010-11-30 07:20:12 +000011350 // The instruction doesn't actually take any operands though.
11351 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011352
Eric Christopher228232b2010-11-30 07:20:12 +000011353 MI->eraseFromParent(); // The pseudo is gone now.
11354 return BB;
11355}
11356
11357MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011358X86TargetLowering::EmitVAARG64WithCustomInserter(
11359 MachineInstr *MI,
11360 MachineBasicBlock *MBB) const {
11361 // Emit va_arg instruction on X86-64.
11362
11363 // Operands to this pseudo-instruction:
11364 // 0 ) Output : destination address (reg)
11365 // 1-5) Input : va_list address (addr, i64mem)
11366 // 6 ) ArgSize : Size (in bytes) of vararg type
11367 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11368 // 8 ) Align : Alignment of type
11369 // 9 ) EFLAGS (implicit-def)
11370
11371 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11372 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11373
11374 unsigned DestReg = MI->getOperand(0).getReg();
11375 MachineOperand &Base = MI->getOperand(1);
11376 MachineOperand &Scale = MI->getOperand(2);
11377 MachineOperand &Index = MI->getOperand(3);
11378 MachineOperand &Disp = MI->getOperand(4);
11379 MachineOperand &Segment = MI->getOperand(5);
11380 unsigned ArgSize = MI->getOperand(6).getImm();
11381 unsigned ArgMode = MI->getOperand(7).getImm();
11382 unsigned Align = MI->getOperand(8).getImm();
11383
11384 // Memory Reference
11385 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11386 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11387 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11388
11389 // Machine Information
11390 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11391 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11392 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11393 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11394 DebugLoc DL = MI->getDebugLoc();
11395
11396 // struct va_list {
11397 // i32 gp_offset
11398 // i32 fp_offset
11399 // i64 overflow_area (address)
11400 // i64 reg_save_area (address)
11401 // }
11402 // sizeof(va_list) = 24
11403 // alignment(va_list) = 8
11404
11405 unsigned TotalNumIntRegs = 6;
11406 unsigned TotalNumXMMRegs = 8;
11407 bool UseGPOffset = (ArgMode == 1);
11408 bool UseFPOffset = (ArgMode == 2);
11409 unsigned MaxOffset = TotalNumIntRegs * 8 +
11410 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11411
11412 /* Align ArgSize to a multiple of 8 */
11413 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11414 bool NeedsAlign = (Align > 8);
11415
11416 MachineBasicBlock *thisMBB = MBB;
11417 MachineBasicBlock *overflowMBB;
11418 MachineBasicBlock *offsetMBB;
11419 MachineBasicBlock *endMBB;
11420
11421 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11422 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11423 unsigned OffsetReg = 0;
11424
11425 if (!UseGPOffset && !UseFPOffset) {
11426 // If we only pull from the overflow region, we don't create a branch.
11427 // We don't need to alter control flow.
11428 OffsetDestReg = 0; // unused
11429 OverflowDestReg = DestReg;
11430
11431 offsetMBB = NULL;
11432 overflowMBB = thisMBB;
11433 endMBB = thisMBB;
11434 } else {
11435 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11436 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11437 // If not, pull from overflow_area. (branch to overflowMBB)
11438 //
11439 // thisMBB
11440 // | .
11441 // | .
11442 // offsetMBB overflowMBB
11443 // | .
11444 // | .
11445 // endMBB
11446
11447 // Registers for the PHI in endMBB
11448 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11449 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11450
11451 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11452 MachineFunction *MF = MBB->getParent();
11453 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11454 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11455 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11456
11457 MachineFunction::iterator MBBIter = MBB;
11458 ++MBBIter;
11459
11460 // Insert the new basic blocks
11461 MF->insert(MBBIter, offsetMBB);
11462 MF->insert(MBBIter, overflowMBB);
11463 MF->insert(MBBIter, endMBB);
11464
11465 // Transfer the remainder of MBB and its successor edges to endMBB.
11466 endMBB->splice(endMBB->begin(), thisMBB,
11467 llvm::next(MachineBasicBlock::iterator(MI)),
11468 thisMBB->end());
11469 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11470
11471 // Make offsetMBB and overflowMBB successors of thisMBB
11472 thisMBB->addSuccessor(offsetMBB);
11473 thisMBB->addSuccessor(overflowMBB);
11474
11475 // endMBB is a successor of both offsetMBB and overflowMBB
11476 offsetMBB->addSuccessor(endMBB);
11477 overflowMBB->addSuccessor(endMBB);
11478
11479 // Load the offset value into a register
11480 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11481 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11482 .addOperand(Base)
11483 .addOperand(Scale)
11484 .addOperand(Index)
11485 .addDisp(Disp, UseFPOffset ? 4 : 0)
11486 .addOperand(Segment)
11487 .setMemRefs(MMOBegin, MMOEnd);
11488
11489 // Check if there is enough room left to pull this argument.
11490 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11491 .addReg(OffsetReg)
11492 .addImm(MaxOffset + 8 - ArgSizeA8);
11493
11494 // Branch to "overflowMBB" if offset >= max
11495 // Fall through to "offsetMBB" otherwise
11496 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11497 .addMBB(overflowMBB);
11498 }
11499
11500 // In offsetMBB, emit code to use the reg_save_area.
11501 if (offsetMBB) {
11502 assert(OffsetReg != 0);
11503
11504 // Read the reg_save_area address.
11505 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11506 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11507 .addOperand(Base)
11508 .addOperand(Scale)
11509 .addOperand(Index)
11510 .addDisp(Disp, 16)
11511 .addOperand(Segment)
11512 .setMemRefs(MMOBegin, MMOEnd);
11513
11514 // Zero-extend the offset
11515 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11516 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11517 .addImm(0)
11518 .addReg(OffsetReg)
11519 .addImm(X86::sub_32bit);
11520
11521 // Add the offset to the reg_save_area to get the final address.
11522 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11523 .addReg(OffsetReg64)
11524 .addReg(RegSaveReg);
11525
11526 // Compute the offset for the next argument
11527 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11528 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11529 .addReg(OffsetReg)
11530 .addImm(UseFPOffset ? 16 : 8);
11531
11532 // Store it back into the va_list.
11533 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11534 .addOperand(Base)
11535 .addOperand(Scale)
11536 .addOperand(Index)
11537 .addDisp(Disp, UseFPOffset ? 4 : 0)
11538 .addOperand(Segment)
11539 .addReg(NextOffsetReg)
11540 .setMemRefs(MMOBegin, MMOEnd);
11541
11542 // Jump to endMBB
11543 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11544 .addMBB(endMBB);
11545 }
11546
11547 //
11548 // Emit code to use overflow area
11549 //
11550
11551 // Load the overflow_area address into a register.
11552 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11553 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11554 .addOperand(Base)
11555 .addOperand(Scale)
11556 .addOperand(Index)
11557 .addDisp(Disp, 8)
11558 .addOperand(Segment)
11559 .setMemRefs(MMOBegin, MMOEnd);
11560
11561 // If we need to align it, do so. Otherwise, just copy the address
11562 // to OverflowDestReg.
11563 if (NeedsAlign) {
11564 // Align the overflow address
11565 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11566 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11567
11568 // aligned_addr = (addr + (align-1)) & ~(align-1)
11569 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11570 .addReg(OverflowAddrReg)
11571 .addImm(Align-1);
11572
11573 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11574 .addReg(TmpReg)
11575 .addImm(~(uint64_t)(Align-1));
11576 } else {
11577 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11578 .addReg(OverflowAddrReg);
11579 }
11580
11581 // Compute the next overflow address after this argument.
11582 // (the overflow address should be kept 8-byte aligned)
11583 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11584 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11585 .addReg(OverflowDestReg)
11586 .addImm(ArgSizeA8);
11587
11588 // Store the new overflow address.
11589 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11590 .addOperand(Base)
11591 .addOperand(Scale)
11592 .addOperand(Index)
11593 .addDisp(Disp, 8)
11594 .addOperand(Segment)
11595 .addReg(NextAddrReg)
11596 .setMemRefs(MMOBegin, MMOEnd);
11597
11598 // If we branched, emit the PHI to the front of endMBB.
11599 if (offsetMBB) {
11600 BuildMI(*endMBB, endMBB->begin(), DL,
11601 TII->get(X86::PHI), DestReg)
11602 .addReg(OffsetDestReg).addMBB(offsetMBB)
11603 .addReg(OverflowDestReg).addMBB(overflowMBB);
11604 }
11605
11606 // Erase the pseudo instruction
11607 MI->eraseFromParent();
11608
11609 return endMBB;
11610}
11611
11612MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011613X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11614 MachineInstr *MI,
11615 MachineBasicBlock *MBB) const {
11616 // Emit code to save XMM registers to the stack. The ABI says that the
11617 // number of registers to save is given in %al, so it's theoretically
11618 // possible to do an indirect jump trick to avoid saving all of them,
11619 // however this code takes a simpler approach and just executes all
11620 // of the stores if %al is non-zero. It's less code, and it's probably
11621 // easier on the hardware branch predictor, and stores aren't all that
11622 // expensive anyway.
11623
11624 // Create the new basic blocks. One block contains all the XMM stores,
11625 // and one block is the final destination regardless of whether any
11626 // stores were performed.
11627 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11628 MachineFunction *F = MBB->getParent();
11629 MachineFunction::iterator MBBIter = MBB;
11630 ++MBBIter;
11631 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11632 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11633 F->insert(MBBIter, XMMSaveMBB);
11634 F->insert(MBBIter, EndMBB);
11635
Dan Gohman14152b42010-07-06 20:24:04 +000011636 // Transfer the remainder of MBB and its successor edges to EndMBB.
11637 EndMBB->splice(EndMBB->begin(), MBB,
11638 llvm::next(MachineBasicBlock::iterator(MI)),
11639 MBB->end());
11640 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11641
Dan Gohmand6708ea2009-08-15 01:38:56 +000011642 // The original block will now fall through to the XMM save block.
11643 MBB->addSuccessor(XMMSaveMBB);
11644 // The XMMSaveMBB will fall through to the end block.
11645 XMMSaveMBB->addSuccessor(EndMBB);
11646
11647 // Now add the instructions.
11648 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11649 DebugLoc DL = MI->getDebugLoc();
11650
11651 unsigned CountReg = MI->getOperand(0).getReg();
11652 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11653 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11654
11655 if (!Subtarget->isTargetWin64()) {
11656 // If %al is 0, branch around the XMM save block.
11657 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011658 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011659 MBB->addSuccessor(EndMBB);
11660 }
11661
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011662 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000011663 // In the XMM save block, save all the XMM argument registers.
11664 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11665 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011666 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011667 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011668 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011669 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011670 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011671 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000011672 .addFrameIndex(RegSaveFrameIndex)
11673 .addImm(/*Scale=*/1)
11674 .addReg(/*IndexReg=*/0)
11675 .addImm(/*Disp=*/Offset)
11676 .addReg(/*Segment=*/0)
11677 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011678 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011679 }
11680
Dan Gohman14152b42010-07-06 20:24:04 +000011681 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011682
11683 return EndMBB;
11684}
Mon P Wang63307c32008-05-05 19:05:59 +000011685
Evan Cheng60c07e12006-07-05 22:17:51 +000011686MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000011687X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011688 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000011689 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11690 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000011691
Chris Lattner52600972009-09-02 05:57:00 +000011692 // To "insert" a SELECT_CC instruction, we actually have to insert the
11693 // diamond control-flow pattern. The incoming instruction knows the
11694 // destination vreg to set, the condition code register to branch on, the
11695 // true/false values to select between, and a branch opcode to use.
11696 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11697 MachineFunction::iterator It = BB;
11698 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000011699
Chris Lattner52600972009-09-02 05:57:00 +000011700 // thisMBB:
11701 // ...
11702 // TrueVal = ...
11703 // cmpTY ccX, r1, r2
11704 // bCC copy1MBB
11705 // fallthrough --> copy0MBB
11706 MachineBasicBlock *thisMBB = BB;
11707 MachineFunction *F = BB->getParent();
11708 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11709 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000011710 F->insert(It, copy0MBB);
11711 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000011712
Bill Wendling730c07e2010-06-25 20:48:10 +000011713 // If the EFLAGS register isn't dead in the terminator, then claim that it's
11714 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000011715 if (!MI->killsRegister(X86::EFLAGS)) {
11716 copy0MBB->addLiveIn(X86::EFLAGS);
11717 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000011718 }
11719
Dan Gohman14152b42010-07-06 20:24:04 +000011720 // Transfer the remainder of BB and its successor edges to sinkMBB.
11721 sinkMBB->splice(sinkMBB->begin(), BB,
11722 llvm::next(MachineBasicBlock::iterator(MI)),
11723 BB->end());
11724 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11725
11726 // Add the true and fallthrough blocks as its successors.
11727 BB->addSuccessor(copy0MBB);
11728 BB->addSuccessor(sinkMBB);
11729
11730 // Create the conditional branch instruction.
11731 unsigned Opc =
11732 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
11733 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
11734
Chris Lattner52600972009-09-02 05:57:00 +000011735 // copy0MBB:
11736 // %FalseValue = ...
11737 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000011738 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000011739
Chris Lattner52600972009-09-02 05:57:00 +000011740 // sinkMBB:
11741 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
11742 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000011743 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
11744 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000011745 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
11746 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
11747
Dan Gohman14152b42010-07-06 20:24:04 +000011748 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000011749 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000011750}
11751
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011752MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011753X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
11754 bool Is64Bit) const {
11755 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11756 DebugLoc DL = MI->getDebugLoc();
11757 MachineFunction *MF = BB->getParent();
11758 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11759
11760 assert(EnableSegmentedStacks);
11761
11762 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
11763 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
11764
11765 // BB:
11766 // ... [Till the alloca]
11767 // If stacklet is not large enough, jump to mallocMBB
11768 //
11769 // bumpMBB:
11770 // Allocate by subtracting from RSP
11771 // Jump to continueMBB
11772 //
11773 // mallocMBB:
11774 // Allocate by call to runtime
11775 //
11776 // continueMBB:
11777 // ...
11778 // [rest of original BB]
11779 //
11780
11781 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11782 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11783 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11784
11785 MachineRegisterInfo &MRI = MF->getRegInfo();
11786 const TargetRegisterClass *AddrRegClass =
11787 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
11788
11789 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11790 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11791 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
11792 sizeVReg = MI->getOperand(1).getReg(),
11793 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
11794
11795 MachineFunction::iterator MBBIter = BB;
11796 ++MBBIter;
11797
11798 MF->insert(MBBIter, bumpMBB);
11799 MF->insert(MBBIter, mallocMBB);
11800 MF->insert(MBBIter, continueMBB);
11801
11802 continueMBB->splice(continueMBB->begin(), BB, llvm::next
11803 (MachineBasicBlock::iterator(MI)), BB->end());
11804 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
11805
11806 // Add code to the main basic block to check if the stack limit has been hit,
11807 // and if so, jump to mallocMBB otherwise to bumpMBB.
11808 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
11809 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), tmpSPVReg)
11810 .addReg(tmpSPVReg).addReg(sizeVReg);
11811 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
11812 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
11813 .addReg(tmpSPVReg);
11814 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
11815
11816 // bumpMBB simply decreases the stack pointer, since we know the current
11817 // stacklet has enough space.
11818 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
11819 .addReg(tmpSPVReg);
11820 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
11821 .addReg(tmpSPVReg);
11822 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11823
11824 // Calls into a routine in libgcc to allocate more space from the heap.
11825 if (Is64Bit) {
11826 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
11827 .addReg(sizeVReg);
11828 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
11829 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
11830 } else {
11831 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
11832 .addImm(12);
11833 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
11834 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
11835 .addExternalSymbol("__morestack_allocate_stack_space");
11836 }
11837
11838 if (!Is64Bit)
11839 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
11840 .addImm(16);
11841
11842 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
11843 .addReg(Is64Bit ? X86::RAX : X86::EAX);
11844 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11845
11846 // Set up the CFG correctly.
11847 BB->addSuccessor(bumpMBB);
11848 BB->addSuccessor(mallocMBB);
11849 mallocMBB->addSuccessor(continueMBB);
11850 bumpMBB->addSuccessor(continueMBB);
11851
11852 // Take care of the PHI nodes.
11853 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
11854 MI->getOperand(0).getReg())
11855 .addReg(mallocPtrVReg).addMBB(mallocMBB)
11856 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
11857
11858 // Delete the original pseudo instruction.
11859 MI->eraseFromParent();
11860
11861 // And we're done.
11862 return continueMBB;
11863}
11864
11865MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011866X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011867 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011868 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11869 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011870
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011871 assert(!Subtarget->isTargetEnvMacho());
11872
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011873 // The lowering is pretty easy: we're just emitting the call to _alloca. The
11874 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011875
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011876 if (Subtarget->isTargetWin64()) {
11877 if (Subtarget->isTargetCygMing()) {
11878 // ___chkstk(Mingw64):
11879 // Clobbers R10, R11, RAX and EFLAGS.
11880 // Updates RSP.
11881 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11882 .addExternalSymbol("___chkstk")
11883 .addReg(X86::RAX, RegState::Implicit)
11884 .addReg(X86::RSP, RegState::Implicit)
11885 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
11886 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
11887 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11888 } else {
11889 // __chkstk(MSVCRT): does not update stack pointer.
11890 // Clobbers R10, R11 and EFLAGS.
11891 // FIXME: RAX(allocated size) might be reused and not killed.
11892 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11893 .addExternalSymbol("__chkstk")
11894 .addReg(X86::RAX, RegState::Implicit)
11895 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11896 // RAX has the offset to subtracted from RSP.
11897 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
11898 .addReg(X86::RSP)
11899 .addReg(X86::RAX);
11900 }
11901 } else {
11902 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011903 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
11904
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011905 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
11906 .addExternalSymbol(StackProbeSymbol)
11907 .addReg(X86::EAX, RegState::Implicit)
11908 .addReg(X86::ESP, RegState::Implicit)
11909 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
11910 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
11911 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11912 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011913
Dan Gohman14152b42010-07-06 20:24:04 +000011914 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011915 return BB;
11916}
Chris Lattner52600972009-09-02 05:57:00 +000011917
11918MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000011919X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
11920 MachineBasicBlock *BB) const {
11921 // This is pretty easy. We're taking the value that we received from
11922 // our load from the relocation, sticking it in either RDI (x86-64)
11923 // or EAX and doing an indirect call. The return value will then
11924 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000011925 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000011926 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000011927 DebugLoc DL = MI->getDebugLoc();
11928 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000011929
11930 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000011931 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011932
Eric Christopher30ef0e52010-06-03 04:07:48 +000011933 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000011934 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11935 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000011936 .addReg(X86::RIP)
11937 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011938 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011939 MI->getOperand(3).getTargetFlags())
11940 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000011941 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000011942 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000011943 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000011944 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11945 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000011946 .addReg(0)
11947 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011948 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000011949 MI->getOperand(3).getTargetFlags())
11950 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011951 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011952 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011953 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000011954 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11955 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000011956 .addReg(TII->getGlobalBaseReg(F))
11957 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011958 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011959 MI->getOperand(3).getTargetFlags())
11960 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011961 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011962 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011963 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011964
Dan Gohman14152b42010-07-06 20:24:04 +000011965 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000011966 return BB;
11967}
11968
11969MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000011970X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011971 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000011972 switch (MI->getOpcode()) {
Richard Trieu23946fc2011-09-21 03:09:09 +000011973 default: assert(0 && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000011974 case X86::TAILJMPd64:
11975 case X86::TAILJMPr64:
11976 case X86::TAILJMPm64:
Richard Trieu23946fc2011-09-21 03:09:09 +000011977 assert(0 && "TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000011978 case X86::TCRETURNdi64:
11979 case X86::TCRETURNri64:
11980 case X86::TCRETURNmi64:
11981 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
11982 // On AMD64, additional defs should be added before register allocation.
11983 if (!Subtarget->isTargetWin64()) {
11984 MI->addRegisterDefined(X86::RSI);
11985 MI->addRegisterDefined(X86::RDI);
11986 MI->addRegisterDefined(X86::XMM6);
11987 MI->addRegisterDefined(X86::XMM7);
11988 MI->addRegisterDefined(X86::XMM8);
11989 MI->addRegisterDefined(X86::XMM9);
11990 MI->addRegisterDefined(X86::XMM10);
11991 MI->addRegisterDefined(X86::XMM11);
11992 MI->addRegisterDefined(X86::XMM12);
11993 MI->addRegisterDefined(X86::XMM13);
11994 MI->addRegisterDefined(X86::XMM14);
11995 MI->addRegisterDefined(X86::XMM15);
11996 }
11997 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011998 case X86::WIN_ALLOCA:
11999 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012000 case X86::SEG_ALLOCA_32:
12001 return EmitLoweredSegAlloca(MI, BB, false);
12002 case X86::SEG_ALLOCA_64:
12003 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012004 case X86::TLSCall_32:
12005 case X86::TLSCall_64:
12006 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012007 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012008 case X86::CMOV_FR32:
12009 case X86::CMOV_FR64:
12010 case X86::CMOV_V4F32:
12011 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012012 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012013 case X86::CMOV_V8F32:
12014 case X86::CMOV_V4F64:
12015 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012016 case X86::CMOV_GR16:
12017 case X86::CMOV_GR32:
12018 case X86::CMOV_RFP32:
12019 case X86::CMOV_RFP64:
12020 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012021 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012022
Dale Johannesen849f2142007-07-03 00:53:03 +000012023 case X86::FP32_TO_INT16_IN_MEM:
12024 case X86::FP32_TO_INT32_IN_MEM:
12025 case X86::FP32_TO_INT64_IN_MEM:
12026 case X86::FP64_TO_INT16_IN_MEM:
12027 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012028 case X86::FP64_TO_INT64_IN_MEM:
12029 case X86::FP80_TO_INT16_IN_MEM:
12030 case X86::FP80_TO_INT32_IN_MEM:
12031 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012032 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12033 DebugLoc DL = MI->getDebugLoc();
12034
Evan Cheng60c07e12006-07-05 22:17:51 +000012035 // Change the floating point control register to use "round towards zero"
12036 // mode when truncating to an integer value.
12037 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012038 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012039 addFrameReference(BuildMI(*BB, MI, DL,
12040 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012041
12042 // Load the old value of the high byte of the control word...
12043 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012044 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012045 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012046 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012047
12048 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012049 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012050 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012051
12052 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012053 addFrameReference(BuildMI(*BB, MI, DL,
12054 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012055
12056 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012057 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012058 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012059
12060 // Get the X86 opcode to use.
12061 unsigned Opc;
12062 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012063 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012064 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12065 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12066 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12067 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12068 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12069 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012070 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12071 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12072 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012073 }
12074
12075 X86AddressMode AM;
12076 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012077 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012078 AM.BaseType = X86AddressMode::RegBase;
12079 AM.Base.Reg = Op.getReg();
12080 } else {
12081 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012082 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012083 }
12084 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012085 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012086 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012087 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012088 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012089 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012090 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012091 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012092 AM.GV = Op.getGlobal();
12093 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012094 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012095 }
Dan Gohman14152b42010-07-06 20:24:04 +000012096 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012097 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012098
12099 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012100 addFrameReference(BuildMI(*BB, MI, DL,
12101 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012102
Dan Gohman14152b42010-07-06 20:24:04 +000012103 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012104 return BB;
12105 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012106 // String/text processing lowering.
12107 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012108 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012109 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12110 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012111 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012112 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12113 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012114 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012115 return EmitPCMP(MI, BB, 5, false /* in mem */);
12116 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012117 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012118 return EmitPCMP(MI, BB, 5, true /* in mem */);
12119
Eric Christopher228232b2010-11-30 07:20:12 +000012120 // Thread synchronization.
12121 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012122 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012123 case X86::MWAIT:
12124 return EmitMwait(MI, BB);
12125
Eric Christopherb120ab42009-08-18 22:50:32 +000012126 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012127 case X86::ATOMAND32:
12128 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012129 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012130 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012131 X86::NOT32r, X86::EAX,
12132 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012133 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012134 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12135 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012136 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012137 X86::NOT32r, X86::EAX,
12138 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012139 case X86::ATOMXOR32:
12140 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012141 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012142 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012143 X86::NOT32r, X86::EAX,
12144 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012145 case X86::ATOMNAND32:
12146 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012147 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012148 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012149 X86::NOT32r, X86::EAX,
12150 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012151 case X86::ATOMMIN32:
12152 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12153 case X86::ATOMMAX32:
12154 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12155 case X86::ATOMUMIN32:
12156 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12157 case X86::ATOMUMAX32:
12158 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012159
12160 case X86::ATOMAND16:
12161 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12162 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012163 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012164 X86::NOT16r, X86::AX,
12165 X86::GR16RegisterClass);
12166 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012167 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012168 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012169 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012170 X86::NOT16r, X86::AX,
12171 X86::GR16RegisterClass);
12172 case X86::ATOMXOR16:
12173 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12174 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012175 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012176 X86::NOT16r, X86::AX,
12177 X86::GR16RegisterClass);
12178 case X86::ATOMNAND16:
12179 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12180 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012181 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012182 X86::NOT16r, X86::AX,
12183 X86::GR16RegisterClass, true);
12184 case X86::ATOMMIN16:
12185 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12186 case X86::ATOMMAX16:
12187 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12188 case X86::ATOMUMIN16:
12189 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12190 case X86::ATOMUMAX16:
12191 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12192
12193 case X86::ATOMAND8:
12194 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12195 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012196 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012197 X86::NOT8r, X86::AL,
12198 X86::GR8RegisterClass);
12199 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012200 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012201 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012202 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012203 X86::NOT8r, X86::AL,
12204 X86::GR8RegisterClass);
12205 case X86::ATOMXOR8:
12206 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12207 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012208 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012209 X86::NOT8r, X86::AL,
12210 X86::GR8RegisterClass);
12211 case X86::ATOMNAND8:
12212 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12213 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012214 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012215 X86::NOT8r, X86::AL,
12216 X86::GR8RegisterClass, true);
12217 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012218 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012219 case X86::ATOMAND64:
12220 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012221 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012222 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012223 X86::NOT64r, X86::RAX,
12224 X86::GR64RegisterClass);
12225 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012226 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12227 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012228 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012229 X86::NOT64r, X86::RAX,
12230 X86::GR64RegisterClass);
12231 case X86::ATOMXOR64:
12232 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012233 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012234 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012235 X86::NOT64r, X86::RAX,
12236 X86::GR64RegisterClass);
12237 case X86::ATOMNAND64:
12238 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12239 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012240 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012241 X86::NOT64r, X86::RAX,
12242 X86::GR64RegisterClass, true);
12243 case X86::ATOMMIN64:
12244 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12245 case X86::ATOMMAX64:
12246 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12247 case X86::ATOMUMIN64:
12248 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12249 case X86::ATOMUMAX64:
12250 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012251
12252 // This group does 64-bit operations on a 32-bit host.
12253 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012254 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012255 X86::AND32rr, X86::AND32rr,
12256 X86::AND32ri, X86::AND32ri,
12257 false);
12258 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012259 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012260 X86::OR32rr, X86::OR32rr,
12261 X86::OR32ri, X86::OR32ri,
12262 false);
12263 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012264 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012265 X86::XOR32rr, X86::XOR32rr,
12266 X86::XOR32ri, X86::XOR32ri,
12267 false);
12268 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012269 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012270 X86::AND32rr, X86::AND32rr,
12271 X86::AND32ri, X86::AND32ri,
12272 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012273 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012274 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012275 X86::ADD32rr, X86::ADC32rr,
12276 X86::ADD32ri, X86::ADC32ri,
12277 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012278 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012279 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012280 X86::SUB32rr, X86::SBB32rr,
12281 X86::SUB32ri, X86::SBB32ri,
12282 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012283 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012284 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012285 X86::MOV32rr, X86::MOV32rr,
12286 X86::MOV32ri, X86::MOV32ri,
12287 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012288 case X86::VASTART_SAVE_XMM_REGS:
12289 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012290
12291 case X86::VAARG_64:
12292 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012293 }
12294}
12295
12296//===----------------------------------------------------------------------===//
12297// X86 Optimization Hooks
12298//===----------------------------------------------------------------------===//
12299
Dan Gohman475871a2008-07-27 21:46:04 +000012300void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012301 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012302 APInt &KnownZero,
12303 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012304 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012305 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012306 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012307 assert((Opc >= ISD::BUILTIN_OP_END ||
12308 Opc == ISD::INTRINSIC_WO_CHAIN ||
12309 Opc == ISD::INTRINSIC_W_CHAIN ||
12310 Opc == ISD::INTRINSIC_VOID) &&
12311 "Should use MaskedValueIsZero if you don't know whether Op"
12312 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012313
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012314 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012315 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012316 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012317 case X86ISD::ADD:
12318 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012319 case X86ISD::ADC:
12320 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012321 case X86ISD::SMUL:
12322 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012323 case X86ISD::INC:
12324 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012325 case X86ISD::OR:
12326 case X86ISD::XOR:
12327 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012328 // These nodes' second result is a boolean.
12329 if (Op.getResNo() == 0)
12330 break;
12331 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012332 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012333 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12334 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012335 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012336 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012337}
Chris Lattner259e97c2006-01-31 19:43:35 +000012338
Owen Andersonbc146b02010-09-21 20:42:50 +000012339unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12340 unsigned Depth) const {
12341 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12342 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12343 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012344
Owen Andersonbc146b02010-09-21 20:42:50 +000012345 // Fallback case.
12346 return 1;
12347}
12348
Evan Cheng206ee9d2006-07-07 08:33:52 +000012349/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012350/// node is a GlobalAddress + offset.
12351bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012352 const GlobalValue* &GA,
12353 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012354 if (N->getOpcode() == X86ISD::Wrapper) {
12355 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012356 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012357 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012358 return true;
12359 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012360 }
Evan Chengad4196b2008-05-12 19:56:52 +000012361 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012362}
12363
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012364/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12365/// same as extracting the high 128-bit part of 256-bit vector and then
12366/// inserting the result into the low part of a new 256-bit vector
12367static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12368 EVT VT = SVOp->getValueType(0);
12369 int NumElems = VT.getVectorNumElements();
12370
12371 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12372 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12373 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12374 SVOp->getMaskElt(j) >= 0)
12375 return false;
12376
12377 return true;
12378}
12379
12380/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12381/// same as extracting the low 128-bit part of 256-bit vector and then
12382/// inserting the result into the high part of a new 256-bit vector
12383static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12384 EVT VT = SVOp->getValueType(0);
12385 int NumElems = VT.getVectorNumElements();
12386
12387 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12388 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12389 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12390 SVOp->getMaskElt(j) >= 0)
12391 return false;
12392
12393 return true;
12394}
12395
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012396/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12397static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12398 TargetLowering::DAGCombinerInfo &DCI) {
12399 DebugLoc dl = N->getDebugLoc();
12400 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12401 SDValue V1 = SVOp->getOperand(0);
12402 SDValue V2 = SVOp->getOperand(1);
12403 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012404 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012405
12406 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12407 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12408 //
12409 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012410 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012411 // V UNDEF BUILD_VECTOR UNDEF
12412 // \ / \ /
12413 // CONCAT_VECTOR CONCAT_VECTOR
12414 // \ /
12415 // \ /
12416 // RESULT: V + zero extended
12417 //
12418 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12419 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12420 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12421 return SDValue();
12422
12423 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12424 return SDValue();
12425
12426 // To match the shuffle mask, the first half of the mask should
12427 // be exactly the first vector, and all the rest a splat with the
12428 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012429 for (int i = 0; i < NumElems/2; ++i)
12430 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12431 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12432 return SDValue();
12433
12434 // Emit a zeroed vector and insert the desired subvector on its
12435 // first half.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000012436 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012437 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12438 DAG.getConstant(0, MVT::i32), DAG, dl);
12439 return DCI.CombineTo(N, InsV);
12440 }
12441
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012442 //===--------------------------------------------------------------------===//
12443 // Combine some shuffles into subvector extracts and inserts:
12444 //
12445
12446 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12447 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12448 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12449 DAG, dl);
12450 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12451 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12452 return DCI.CombineTo(N, InsV);
12453 }
12454
12455 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12456 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12457 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12458 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12459 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12460 return DCI.CombineTo(N, InsV);
12461 }
12462
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012463 return SDValue();
12464}
12465
12466/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012467static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012468 TargetLowering::DAGCombinerInfo &DCI,
12469 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012470 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012471 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012472
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012473 // Don't create instructions with illegal types after legalize types has run.
12474 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12475 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12476 return SDValue();
12477
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012478 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12479 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12480 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012481 return PerformShuffleCombine256(N, DAG, DCI);
12482
12483 // Only handle 128 wide vector from here on.
12484 if (VT.getSizeInBits() != 128)
12485 return SDValue();
12486
12487 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12488 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12489 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012490 SmallVector<SDValue, 16> Elts;
12491 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012492 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012493
Nate Begemanfdea31a2010-03-24 20:49:50 +000012494 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012495}
Evan Chengd880b972008-05-09 21:53:03 +000012496
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012497/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12498/// generation and convert it from being a bunch of shuffles and extracts
12499/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012500static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12501 const TargetLowering &TLI) {
12502 SDValue InputVector = N->getOperand(0);
12503
12504 // Only operate on vectors of 4 elements, where the alternative shuffling
12505 // gets to be more expensive.
12506 if (InputVector.getValueType() != MVT::v4i32)
12507 return SDValue();
12508
12509 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12510 // single use which is a sign-extend or zero-extend, and all elements are
12511 // used.
12512 SmallVector<SDNode *, 4> Uses;
12513 unsigned ExtractedElements = 0;
12514 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12515 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12516 if (UI.getUse().getResNo() != InputVector.getResNo())
12517 return SDValue();
12518
12519 SDNode *Extract = *UI;
12520 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12521 return SDValue();
12522
12523 if (Extract->getValueType(0) != MVT::i32)
12524 return SDValue();
12525 if (!Extract->hasOneUse())
12526 return SDValue();
12527 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12528 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12529 return SDValue();
12530 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12531 return SDValue();
12532
12533 // Record which element was extracted.
12534 ExtractedElements |=
12535 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12536
12537 Uses.push_back(Extract);
12538 }
12539
12540 // If not all the elements were used, this may not be worthwhile.
12541 if (ExtractedElements != 15)
12542 return SDValue();
12543
12544 // Ok, we've now decided to do the transformation.
12545 DebugLoc dl = InputVector.getDebugLoc();
12546
12547 // Store the value to a temporary stack slot.
12548 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012549 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12550 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012551
12552 // Replace each use (extract) with a load of the appropriate element.
12553 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12554 UE = Uses.end(); UI != UE; ++UI) {
12555 SDNode *Extract = *UI;
12556
Nadav Rotem86694292011-05-17 08:31:57 +000012557 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012558 SDValue Idx = Extract->getOperand(1);
12559 unsigned EltSize =
12560 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12561 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12562 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12563
Nadav Rotem86694292011-05-17 08:31:57 +000012564 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012565 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012566
12567 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012568 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012569 ScalarAddr, MachinePointerInfo(),
12570 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012571
12572 // Replace the exact with the load.
12573 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12574 }
12575
12576 // The replacement was made in place; don't return anything.
12577 return SDValue();
12578}
12579
Duncan Sands6bcd2192011-09-17 16:49:39 +000012580/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
12581/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012582static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012583 const X86Subtarget *Subtarget) {
12584 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012585 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012586 // Get the LHS/RHS of the select.
12587 SDValue LHS = N->getOperand(1);
12588 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000012589 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000012590
Dan Gohman670e5392009-09-21 18:03:22 +000012591 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012592 // instructions match the semantics of the common C idiom x<y?x:y but not
12593 // x<=y?x:y, because of how they handle negative zero (which can be
12594 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000012595 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
12596 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
12597 (Subtarget->hasXMMInt() ||
12598 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012599 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012600
Chris Lattner47b4ce82009-03-11 05:48:52 +000012601 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000012602 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000012603 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12604 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012605 switch (CC) {
12606 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012607 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012608 // Converting this to a min would handle NaNs incorrectly, and swapping
12609 // the operands would cause it to handle comparisons between positive
12610 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012611 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012612 if (!UnsafeFPMath &&
12613 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12614 break;
12615 std::swap(LHS, RHS);
12616 }
Dan Gohman670e5392009-09-21 18:03:22 +000012617 Opcode = X86ISD::FMIN;
12618 break;
12619 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012620 // Converting this to a min would handle comparisons between positive
12621 // and negative zero incorrectly.
12622 if (!UnsafeFPMath &&
12623 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12624 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012625 Opcode = X86ISD::FMIN;
12626 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012627 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012628 // Converting this to a min would handle both negative zeros and NaNs
12629 // incorrectly, but we can swap the operands to fix both.
12630 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012631 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012632 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012633 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012634 Opcode = X86ISD::FMIN;
12635 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012636
Dan Gohman670e5392009-09-21 18:03:22 +000012637 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012638 // Converting this to a max would handle comparisons between positive
12639 // and negative zero incorrectly.
12640 if (!UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000012641 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012642 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012643 Opcode = X86ISD::FMAX;
12644 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012645 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012646 // Converting this to a max would handle NaNs incorrectly, and swapping
12647 // the operands would cause it to handle comparisons between positive
12648 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012649 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012650 if (!UnsafeFPMath &&
12651 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12652 break;
12653 std::swap(LHS, RHS);
12654 }
Dan Gohman670e5392009-09-21 18:03:22 +000012655 Opcode = X86ISD::FMAX;
12656 break;
12657 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012658 // Converting this to a max would handle both negative zeros and NaNs
12659 // incorrectly, but we can swap the operands to fix both.
12660 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012661 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012662 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012663 case ISD::SETGE:
12664 Opcode = X86ISD::FMAX;
12665 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000012666 }
Dan Gohman670e5392009-09-21 18:03:22 +000012667 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000012668 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
12669 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012670 switch (CC) {
12671 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012672 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012673 // Converting this to a min would handle comparisons between positive
12674 // and negative zero incorrectly, and swapping the operands would
12675 // cause it to handle NaNs incorrectly.
12676 if (!UnsafeFPMath &&
12677 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000012678 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012679 break;
12680 std::swap(LHS, RHS);
12681 }
Dan Gohman670e5392009-09-21 18:03:22 +000012682 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000012683 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012684 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012685 // Converting this to a min would handle NaNs incorrectly.
12686 if (!UnsafeFPMath &&
12687 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
12688 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012689 Opcode = X86ISD::FMIN;
12690 break;
12691 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012692 // Converting this to a min would handle both negative zeros and NaNs
12693 // incorrectly, but we can swap the operands to fix both.
12694 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012695 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012696 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012697 case ISD::SETGE:
12698 Opcode = X86ISD::FMIN;
12699 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012700
Dan Gohman670e5392009-09-21 18:03:22 +000012701 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012702 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012703 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012704 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012705 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000012706 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012707 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012708 // Converting this to a max would handle comparisons between positive
12709 // and negative zero incorrectly, and swapping the operands would
12710 // cause it to handle NaNs incorrectly.
12711 if (!UnsafeFPMath &&
12712 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000012713 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012714 break;
12715 std::swap(LHS, RHS);
12716 }
Dan Gohman670e5392009-09-21 18:03:22 +000012717 Opcode = X86ISD::FMAX;
12718 break;
12719 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012720 // Converting this to a max would handle both negative zeros and NaNs
12721 // incorrectly, but we can swap the operands to fix both.
12722 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012723 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012724 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012725 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012726 Opcode = X86ISD::FMAX;
12727 break;
12728 }
Chris Lattner83e6c992006-10-04 06:57:07 +000012729 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012730
Chris Lattner47b4ce82009-03-11 05:48:52 +000012731 if (Opcode)
12732 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000012733 }
Eric Christopherfd179292009-08-27 18:07:15 +000012734
Chris Lattnerd1980a52009-03-12 06:52:53 +000012735 // If this is a select between two integer constants, try to do some
12736 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000012737 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
12738 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000012739 // Don't do this for crazy integer types.
12740 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
12741 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000012742 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012743 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000012744
Chris Lattnercee56e72009-03-13 05:53:31 +000012745 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000012746 // Efficiently invertible.
12747 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
12748 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
12749 isa<ConstantSDNode>(Cond.getOperand(1))))) {
12750 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000012751 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012752 }
Eric Christopherfd179292009-08-27 18:07:15 +000012753
Chris Lattnerd1980a52009-03-12 06:52:53 +000012754 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012755 if (FalseC->getAPIntValue() == 0 &&
12756 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012757 if (NeedsCondInvert) // Invert the condition if needed.
12758 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12759 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012760
Chris Lattnerd1980a52009-03-12 06:52:53 +000012761 // Zero extend the condition if needed.
12762 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012763
Chris Lattnercee56e72009-03-13 05:53:31 +000012764 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000012765 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012766 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012767 }
Eric Christopherfd179292009-08-27 18:07:15 +000012768
Chris Lattner97a29a52009-03-13 05:22:11 +000012769 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000012770 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000012771 if (NeedsCondInvert) // Invert the condition if needed.
12772 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12773 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012774
Chris Lattner97a29a52009-03-13 05:22:11 +000012775 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012776 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12777 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012778 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000012779 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000012780 }
Eric Christopherfd179292009-08-27 18:07:15 +000012781
Chris Lattnercee56e72009-03-13 05:53:31 +000012782 // Optimize cases that will turn into an LEA instruction. This requires
12783 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012784 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012785 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012786 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012787
Chris Lattnercee56e72009-03-13 05:53:31 +000012788 bool isFastMultiplier = false;
12789 if (Diff < 10) {
12790 switch ((unsigned char)Diff) {
12791 default: break;
12792 case 1: // result = add base, cond
12793 case 2: // result = lea base( , cond*2)
12794 case 3: // result = lea base(cond, cond*2)
12795 case 4: // result = lea base( , cond*4)
12796 case 5: // result = lea base(cond, cond*4)
12797 case 8: // result = lea base( , cond*8)
12798 case 9: // result = lea base(cond, cond*8)
12799 isFastMultiplier = true;
12800 break;
12801 }
12802 }
Eric Christopherfd179292009-08-27 18:07:15 +000012803
Chris Lattnercee56e72009-03-13 05:53:31 +000012804 if (isFastMultiplier) {
12805 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
12806 if (NeedsCondInvert) // Invert the condition if needed.
12807 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12808 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012809
Chris Lattnercee56e72009-03-13 05:53:31 +000012810 // Zero extend the condition if needed.
12811 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12812 Cond);
12813 // Scale the condition by the difference.
12814 if (Diff != 1)
12815 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12816 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012817
Chris Lattnercee56e72009-03-13 05:53:31 +000012818 // Add the base if non-zero.
12819 if (FalseC->getAPIntValue() != 0)
12820 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12821 SDValue(FalseC, 0));
12822 return Cond;
12823 }
Eric Christopherfd179292009-08-27 18:07:15 +000012824 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012825 }
12826 }
Eric Christopherfd179292009-08-27 18:07:15 +000012827
Dan Gohman475871a2008-07-27 21:46:04 +000012828 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000012829}
12830
Chris Lattnerd1980a52009-03-12 06:52:53 +000012831/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
12832static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
12833 TargetLowering::DAGCombinerInfo &DCI) {
12834 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000012835
Chris Lattnerd1980a52009-03-12 06:52:53 +000012836 // If the flag operand isn't dead, don't touch this CMOV.
12837 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
12838 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000012839
Evan Chengb5a55d92011-05-24 01:48:22 +000012840 SDValue FalseOp = N->getOperand(0);
12841 SDValue TrueOp = N->getOperand(1);
12842 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
12843 SDValue Cond = N->getOperand(3);
12844 if (CC == X86::COND_E || CC == X86::COND_NE) {
12845 switch (Cond.getOpcode()) {
12846 default: break;
12847 case X86ISD::BSR:
12848 case X86ISD::BSF:
12849 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
12850 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
12851 return (CC == X86::COND_E) ? FalseOp : TrueOp;
12852 }
12853 }
12854
Chris Lattnerd1980a52009-03-12 06:52:53 +000012855 // If this is a select between two integer constants, try to do some
12856 // optimizations. Note that the operands are ordered the opposite of SELECT
12857 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000012858 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
12859 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012860 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
12861 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000012862 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
12863 CC = X86::GetOppositeBranchCondition(CC);
12864 std::swap(TrueC, FalseC);
12865 }
Eric Christopherfd179292009-08-27 18:07:15 +000012866
Chris Lattnerd1980a52009-03-12 06:52:53 +000012867 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012868 // This is efficient for any integer data type (including i8/i16) and
12869 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012870 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012871 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12872 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012873
Chris Lattnerd1980a52009-03-12 06:52:53 +000012874 // Zero extend the condition if needed.
12875 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012876
Chris Lattnerd1980a52009-03-12 06:52:53 +000012877 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
12878 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012879 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012880 if (N->getNumValues() == 2) // Dead flag value?
12881 return DCI.CombineTo(N, Cond, SDValue());
12882 return Cond;
12883 }
Eric Christopherfd179292009-08-27 18:07:15 +000012884
Chris Lattnercee56e72009-03-13 05:53:31 +000012885 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
12886 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000012887 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012888 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12889 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012890
Chris Lattner97a29a52009-03-13 05:22:11 +000012891 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012892 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12893 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012894 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12895 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000012896
Chris Lattner97a29a52009-03-13 05:22:11 +000012897 if (N->getNumValues() == 2) // Dead flag value?
12898 return DCI.CombineTo(N, Cond, SDValue());
12899 return Cond;
12900 }
Eric Christopherfd179292009-08-27 18:07:15 +000012901
Chris Lattnercee56e72009-03-13 05:53:31 +000012902 // Optimize cases that will turn into an LEA instruction. This requires
12903 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012904 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012905 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012906 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012907
Chris Lattnercee56e72009-03-13 05:53:31 +000012908 bool isFastMultiplier = false;
12909 if (Diff < 10) {
12910 switch ((unsigned char)Diff) {
12911 default: break;
12912 case 1: // result = add base, cond
12913 case 2: // result = lea base( , cond*2)
12914 case 3: // result = lea base(cond, cond*2)
12915 case 4: // result = lea base( , cond*4)
12916 case 5: // result = lea base(cond, cond*4)
12917 case 8: // result = lea base( , cond*8)
12918 case 9: // result = lea base(cond, cond*8)
12919 isFastMultiplier = true;
12920 break;
12921 }
12922 }
Eric Christopherfd179292009-08-27 18:07:15 +000012923
Chris Lattnercee56e72009-03-13 05:53:31 +000012924 if (isFastMultiplier) {
12925 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012926 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12927 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000012928 // Zero extend the condition if needed.
12929 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12930 Cond);
12931 // Scale the condition by the difference.
12932 if (Diff != 1)
12933 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12934 DAG.getConstant(Diff, Cond.getValueType()));
12935
12936 // Add the base if non-zero.
12937 if (FalseC->getAPIntValue() != 0)
12938 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12939 SDValue(FalseC, 0));
12940 if (N->getNumValues() == 2) // Dead flag value?
12941 return DCI.CombineTo(N, Cond, SDValue());
12942 return Cond;
12943 }
Eric Christopherfd179292009-08-27 18:07:15 +000012944 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012945 }
12946 }
12947 return SDValue();
12948}
12949
12950
Evan Cheng0b0cd912009-03-28 05:57:29 +000012951/// PerformMulCombine - Optimize a single multiply with constant into two
12952/// in order to implement it with two cheaper instructions, e.g.
12953/// LEA + SHL, LEA + LEA.
12954static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
12955 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000012956 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
12957 return SDValue();
12958
Owen Andersone50ed302009-08-10 22:56:29 +000012959 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012960 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000012961 return SDValue();
12962
12963 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
12964 if (!C)
12965 return SDValue();
12966 uint64_t MulAmt = C->getZExtValue();
12967 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
12968 return SDValue();
12969
12970 uint64_t MulAmt1 = 0;
12971 uint64_t MulAmt2 = 0;
12972 if ((MulAmt % 9) == 0) {
12973 MulAmt1 = 9;
12974 MulAmt2 = MulAmt / 9;
12975 } else if ((MulAmt % 5) == 0) {
12976 MulAmt1 = 5;
12977 MulAmt2 = MulAmt / 5;
12978 } else if ((MulAmt % 3) == 0) {
12979 MulAmt1 = 3;
12980 MulAmt2 = MulAmt / 3;
12981 }
12982 if (MulAmt2 &&
12983 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
12984 DebugLoc DL = N->getDebugLoc();
12985
12986 if (isPowerOf2_64(MulAmt2) &&
12987 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
12988 // If second multiplifer is pow2, issue it first. We want the multiply by
12989 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
12990 // is an add.
12991 std::swap(MulAmt1, MulAmt2);
12992
12993 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000012994 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000012995 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000012996 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000012997 else
Evan Cheng73f24c92009-03-30 21:36:47 +000012998 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000012999 DAG.getConstant(MulAmt1, VT));
13000
Eric Christopherfd179292009-08-27 18:07:15 +000013001 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013002 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000013003 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000013004 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013005 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000013006 DAG.getConstant(MulAmt2, VT));
13007
13008 // Do not add new nodes to DAG combiner worklist.
13009 DCI.CombineTo(N, NewMul, false);
13010 }
13011 return SDValue();
13012}
13013
Evan Chengad9c0a32009-12-15 00:53:42 +000013014static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
13015 SDValue N0 = N->getOperand(0);
13016 SDValue N1 = N->getOperand(1);
13017 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13018 EVT VT = N0.getValueType();
13019
13020 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13021 // since the result of setcc_c is all zero's or all ones.
13022 if (N1C && N0.getOpcode() == ISD::AND &&
13023 N0.getOperand(1).getOpcode() == ISD::Constant) {
13024 SDValue N00 = N0.getOperand(0);
13025 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13026 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13027 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13028 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13029 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13030 APInt ShAmt = N1C->getAPIntValue();
13031 Mask = Mask.shl(ShAmt);
13032 if (Mask != 0)
13033 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13034 N00, DAG.getConstant(Mask, VT));
13035 }
13036 }
13037
13038 return SDValue();
13039}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013040
Nate Begeman740ab032009-01-26 00:52:55 +000013041/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13042/// when possible.
13043static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13044 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013045 EVT VT = N->getValueType(0);
13046 if (!VT.isVector() && VT.isInteger() &&
13047 N->getOpcode() == ISD::SHL)
13048 return PerformSHLCombine(N, DAG);
13049
Nate Begeman740ab032009-01-26 00:52:55 +000013050 // On X86 with SSE2 support, we can transform this to a vector shift if
13051 // all elements are shifted by the same amount. We can't do this in legalize
13052 // because the a constant vector is typically transformed to a constant pool
13053 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013054 if (!Subtarget->hasXMMInt())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013055 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013056
Owen Anderson825b72b2009-08-11 20:47:22 +000013057 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013058 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013059
Mon P Wang3becd092009-01-28 08:12:05 +000013060 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013061 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013062 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013063 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013064 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13065 unsigned NumElts = VT.getVectorNumElements();
13066 unsigned i = 0;
13067 for (; i != NumElts; ++i) {
13068 SDValue Arg = ShAmtOp.getOperand(i);
13069 if (Arg.getOpcode() == ISD::UNDEF) continue;
13070 BaseShAmt = Arg;
13071 break;
13072 }
13073 for (; i != NumElts; ++i) {
13074 SDValue Arg = ShAmtOp.getOperand(i);
13075 if (Arg.getOpcode() == ISD::UNDEF) continue;
13076 if (Arg != BaseShAmt) {
13077 return SDValue();
13078 }
13079 }
13080 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013081 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013082 SDValue InVec = ShAmtOp.getOperand(0);
13083 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13084 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13085 unsigned i = 0;
13086 for (; i != NumElts; ++i) {
13087 SDValue Arg = InVec.getOperand(i);
13088 if (Arg.getOpcode() == ISD::UNDEF) continue;
13089 BaseShAmt = Arg;
13090 break;
13091 }
13092 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13093 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013094 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013095 if (C->getZExtValue() == SplatIdx)
13096 BaseShAmt = InVec.getOperand(1);
13097 }
13098 }
13099 if (BaseShAmt.getNode() == 0)
13100 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13101 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013102 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013103 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013104
Mon P Wangefa42202009-09-03 19:56:25 +000013105 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013106 if (EltVT.bitsGT(MVT::i32))
13107 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13108 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013109 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013110
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013111 // The shift amount is identical so we can do a vector shift.
13112 SDValue ValOp = N->getOperand(0);
13113 switch (N->getOpcode()) {
13114 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013115 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013116 break;
13117 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013118 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013119 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013120 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013121 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013122 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013123 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013124 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013125 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013126 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013127 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013128 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013129 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013130 break;
13131 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013132 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013133 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013134 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013135 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013136 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013137 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013138 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013139 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013140 break;
13141 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013142 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013143 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013144 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013145 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013146 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013147 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013148 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013149 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013150 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013151 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013152 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013153 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013154 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013155 }
13156 return SDValue();
13157}
13158
Nate Begemanb65c1752010-12-17 22:55:37 +000013159
Stuart Hastings865f0932011-06-03 23:53:54 +000013160// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13161// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13162// and friends. Likewise for OR -> CMPNEQSS.
13163static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13164 TargetLowering::DAGCombinerInfo &DCI,
13165 const X86Subtarget *Subtarget) {
13166 unsigned opcode;
13167
13168 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13169 // we're requiring SSE2 for both.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013170 if (Subtarget->hasXMMInt() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013171 SDValue N0 = N->getOperand(0);
13172 SDValue N1 = N->getOperand(1);
13173 SDValue CMP0 = N0->getOperand(1);
13174 SDValue CMP1 = N1->getOperand(1);
13175 DebugLoc DL = N->getDebugLoc();
13176
13177 // The SETCCs should both refer to the same CMP.
13178 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13179 return SDValue();
13180
13181 SDValue CMP00 = CMP0->getOperand(0);
13182 SDValue CMP01 = CMP0->getOperand(1);
13183 EVT VT = CMP00.getValueType();
13184
13185 if (VT == MVT::f32 || VT == MVT::f64) {
13186 bool ExpectingFlags = false;
13187 // Check for any users that want flags:
13188 for (SDNode::use_iterator UI = N->use_begin(),
13189 UE = N->use_end();
13190 !ExpectingFlags && UI != UE; ++UI)
13191 switch (UI->getOpcode()) {
13192 default:
13193 case ISD::BR_CC:
13194 case ISD::BRCOND:
13195 case ISD::SELECT:
13196 ExpectingFlags = true;
13197 break;
13198 case ISD::CopyToReg:
13199 case ISD::SIGN_EXTEND:
13200 case ISD::ZERO_EXTEND:
13201 case ISD::ANY_EXTEND:
13202 break;
13203 }
13204
13205 if (!ExpectingFlags) {
13206 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13207 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13208
13209 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13210 X86::CondCode tmp = cc0;
13211 cc0 = cc1;
13212 cc1 = tmp;
13213 }
13214
13215 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13216 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13217 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13218 X86ISD::NodeType NTOperator = is64BitFP ?
13219 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13220 // FIXME: need symbolic constants for these magic numbers.
13221 // See X86ATTInstPrinter.cpp:printSSECC().
13222 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13223 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13224 DAG.getConstant(x86cc, MVT::i8));
13225 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13226 OnesOrZeroesF);
13227 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13228 DAG.getConstant(1, MVT::i32));
13229 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13230 return OneBitOfTruth;
13231 }
13232 }
13233 }
13234 }
13235 return SDValue();
13236}
13237
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013238/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13239/// so it can be folded inside ANDNP.
13240static bool CanFoldXORWithAllOnes(const SDNode *N) {
13241 EVT VT = N->getValueType(0);
13242
13243 // Match direct AllOnes for 128 and 256-bit vectors
13244 if (ISD::isBuildVectorAllOnes(N))
13245 return true;
13246
13247 // Look through a bit convert.
13248 if (N->getOpcode() == ISD::BITCAST)
13249 N = N->getOperand(0).getNode();
13250
13251 // Sometimes the operand may come from a insert_subvector building a 256-bit
13252 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013253 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013254 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13255 SDValue V1 = N->getOperand(0);
13256 SDValue V2 = N->getOperand(1);
13257
13258 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13259 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13260 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13261 ISD::isBuildVectorAllOnes(V2.getNode()))
13262 return true;
13263 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013264
13265 return false;
13266}
13267
Nate Begemanb65c1752010-12-17 22:55:37 +000013268static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13269 TargetLowering::DAGCombinerInfo &DCI,
13270 const X86Subtarget *Subtarget) {
13271 if (DCI.isBeforeLegalizeOps())
13272 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013273
Stuart Hastings865f0932011-06-03 23:53:54 +000013274 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13275 if (R.getNode())
13276 return R;
13277
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013278 // Want to form ANDNP nodes:
13279 // 1) In the hopes of then easily combining them with OR and AND nodes
13280 // to form PBLEND/PSIGN.
13281 // 2) To match ANDN packed intrinsics
Nate Begemanb65c1752010-12-17 22:55:37 +000013282 EVT VT = N->getValueType(0);
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013283 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013284 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013285
Nate Begemanb65c1752010-12-17 22:55:37 +000013286 SDValue N0 = N->getOperand(0);
13287 SDValue N1 = N->getOperand(1);
13288 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013289
Nate Begemanb65c1752010-12-17 22:55:37 +000013290 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013291 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013292 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13293 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013294 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013295
13296 // Check RHS for vnot
13297 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013298 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13299 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013300 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013301
Nate Begemanb65c1752010-12-17 22:55:37 +000013302 return SDValue();
13303}
13304
Evan Cheng760d1942010-01-04 21:22:48 +000013305static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013306 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013307 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013308 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013309 return SDValue();
13310
Stuart Hastings865f0932011-06-03 23:53:54 +000013311 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13312 if (R.getNode())
13313 return R;
13314
Evan Cheng760d1942010-01-04 21:22:48 +000013315 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000013316 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000013317 return SDValue();
13318
Evan Cheng760d1942010-01-04 21:22:48 +000013319 SDValue N0 = N->getOperand(0);
13320 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013321
Nate Begemanb65c1752010-12-17 22:55:37 +000013322 // look for psign/blend
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013323 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013324 if (VT == MVT::v2i64) {
13325 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013326 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000013327 std::swap(N0, N1);
13328 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013329 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013330 SDValue Mask = N1.getOperand(0);
13331 SDValue X = N1.getOperand(1);
13332 SDValue Y;
13333 if (N0.getOperand(0) == Mask)
13334 Y = N0.getOperand(1);
13335 if (N0.getOperand(1) == Mask)
13336 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013337
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013338 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000013339 if (!Y.getNode())
13340 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013341
Nate Begemanb65c1752010-12-17 22:55:37 +000013342 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13343 if (Mask.getOpcode() != ISD::BITCAST ||
13344 X.getOpcode() != ISD::BITCAST ||
13345 Y.getOpcode() != ISD::BITCAST)
13346 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013347
Nate Begemanb65c1752010-12-17 22:55:37 +000013348 // Look through mask bitcast.
13349 Mask = Mask.getOperand(0);
13350 EVT MaskVT = Mask.getValueType();
13351
13352 // Validate that the Mask operand is a vector sra node. The sra node
13353 // will be an intrinsic.
13354 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13355 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013356
Nate Begemanb65c1752010-12-17 22:55:37 +000013357 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13358 // there is no psrai.b
13359 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13360 case Intrinsic::x86_sse2_psrai_w:
13361 case Intrinsic::x86_sse2_psrai_d:
13362 break;
13363 default: return SDValue();
13364 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013365
Nate Begemanb65c1752010-12-17 22:55:37 +000013366 // Check that the SRA is all signbits.
13367 SDValue SraC = Mask.getOperand(2);
13368 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13369 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13370 if ((SraAmt + 1) != EltBits)
13371 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013372
Nate Begemanb65c1752010-12-17 22:55:37 +000013373 DebugLoc DL = N->getDebugLoc();
13374
13375 // Now we know we at least have a plendvb with the mask val. See if
13376 // we can form a psignb/w/d.
13377 // psign = x.type == y.type == mask.type && y = sub(0, x);
13378 X = X.getOperand(0);
13379 Y = Y.getOperand(0);
13380 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13381 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
13382 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
13383 unsigned Opc = 0;
13384 switch (EltBits) {
13385 case 8: Opc = X86ISD::PSIGNB; break;
13386 case 16: Opc = X86ISD::PSIGNW; break;
13387 case 32: Opc = X86ISD::PSIGND; break;
13388 default: break;
13389 }
13390 if (Opc) {
13391 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
13392 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
13393 }
13394 }
13395 // PBLENDVB only available on SSE 4.1
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013396 if (!(Subtarget->hasSSE41() || Subtarget->hasAVX()))
Nate Begemanb65c1752010-12-17 22:55:37 +000013397 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013398
Nate Begemanb65c1752010-12-17 22:55:37 +000013399 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
13400 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
13401 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000013402 Mask = DAG.getNode(ISD::VSELECT, DL, MVT::v16i8, Mask, X, Y);
Nate Begemanb65c1752010-12-17 22:55:37 +000013403 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
13404 }
13405 }
13406 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013407
Nate Begemanb65c1752010-12-17 22:55:37 +000013408 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013409 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13410 std::swap(N0, N1);
13411 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13412 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013413 if (!N0.hasOneUse() || !N1.hasOneUse())
13414 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013415
13416 SDValue ShAmt0 = N0.getOperand(1);
13417 if (ShAmt0.getValueType() != MVT::i8)
13418 return SDValue();
13419 SDValue ShAmt1 = N1.getOperand(1);
13420 if (ShAmt1.getValueType() != MVT::i8)
13421 return SDValue();
13422 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13423 ShAmt0 = ShAmt0.getOperand(0);
13424 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13425 ShAmt1 = ShAmt1.getOperand(0);
13426
13427 DebugLoc DL = N->getDebugLoc();
13428 unsigned Opc = X86ISD::SHLD;
13429 SDValue Op0 = N0.getOperand(0);
13430 SDValue Op1 = N1.getOperand(0);
13431 if (ShAmt0.getOpcode() == ISD::SUB) {
13432 Opc = X86ISD::SHRD;
13433 std::swap(Op0, Op1);
13434 std::swap(ShAmt0, ShAmt1);
13435 }
13436
Evan Cheng8b1190a2010-04-28 01:18:01 +000013437 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013438 if (ShAmt1.getOpcode() == ISD::SUB) {
13439 SDValue Sum = ShAmt1.getOperand(0);
13440 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013441 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13442 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13443 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13444 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013445 return DAG.getNode(Opc, DL, VT,
13446 Op0, Op1,
13447 DAG.getNode(ISD::TRUNCATE, DL,
13448 MVT::i8, ShAmt0));
13449 }
13450 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13451 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13452 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013453 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013454 return DAG.getNode(Opc, DL, VT,
13455 N0.getOperand(0), N1.getOperand(0),
13456 DAG.getNode(ISD::TRUNCATE, DL,
13457 MVT::i8, ShAmt0));
13458 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013459
Evan Cheng760d1942010-01-04 21:22:48 +000013460 return SDValue();
13461}
13462
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013463/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
13464static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
13465 const X86Subtarget *Subtarget) {
13466 LoadSDNode *Ld = cast<LoadSDNode>(N);
13467 EVT RegVT = Ld->getValueType(0);
13468 EVT MemVT = Ld->getMemoryVT();
13469 DebugLoc dl = Ld->getDebugLoc();
13470 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13471
13472 ISD::LoadExtType Ext = Ld->getExtensionType();
13473
Nadav Rotemca6f2962011-09-18 19:00:23 +000013474 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013475 // shuffle. We need SSE4 for the shuffles.
13476 // TODO: It is possible to support ZExt by zeroing the undef values
13477 // during the shuffle phase or after the shuffle.
13478 if (RegVT.isVector() && Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
13479 assert(MemVT != RegVT && "Cannot extend to the same type");
13480 assert(MemVT.isVector() && "Must load a vector from memory");
13481
13482 unsigned NumElems = RegVT.getVectorNumElements();
13483 unsigned RegSz = RegVT.getSizeInBits();
13484 unsigned MemSz = MemVT.getSizeInBits();
13485 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000013486 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013487 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
13488
13489 // Attempt to load the original value using a single load op.
13490 // Find a scalar type which is equal to the loaded word size.
13491 MVT SclrLoadTy = 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) && Tp.getSizeInBits() == MemSz) {
13496 SclrLoadTy = Tp;
13497 break;
13498 }
13499 }
13500
13501 // Proceed if a load word is found.
13502 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
13503
13504 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
13505 RegSz/SclrLoadTy.getSizeInBits());
13506
13507 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
13508 RegSz/MemVT.getScalarType().getSizeInBits());
13509 // Can't shuffle using an illegal type.
13510 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13511
13512 // Perform a single load.
13513 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
13514 Ld->getBasePtr(),
13515 Ld->getPointerInfo(), Ld->isVolatile(),
13516 Ld->isNonTemporal(), Ld->getAlignment());
13517
13518 // Insert the word loaded into a vector.
13519 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
13520 LoadUnitVecVT, ScalarLoad);
13521
13522 // Bitcast the loaded value to a vector of the original element type, in
13523 // the size of the target vector type.
13524 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, ScalarInVector);
13525 unsigned SizeRatio = RegSz/MemSz;
13526
13527 // Redistribute the loaded elements into the different locations.
13528 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13529 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
13530
13531 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
13532 DAG.getUNDEF(SlicedVec.getValueType()),
13533 ShuffleVec.data());
13534
13535 // Bitcast to the requested type.
13536 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
13537 // Replace the original load with the new sequence
13538 // and return the new chain.
13539 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
13540 return SDValue(ScalarLoad.getNode(), 1);
13541 }
13542
13543 return SDValue();
13544}
13545
Chris Lattner149a4e52008-02-22 02:09:43 +000013546/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013547static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000013548 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000013549 StoreSDNode *St = cast<StoreSDNode>(N);
13550 EVT VT = St->getValue().getValueType();
13551 EVT StVT = St->getMemoryVT();
13552 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000013553 SDValue StoredVal = St->getOperand(1);
13554 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13555
13556 // If we are saving a concatination of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000013557 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
13558 // 128-bit ones. If in the future the cost becomes only one memory access the
13559 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000013560 if (VT.getSizeInBits() == 256 &&
13561 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
13562 StoredVal.getNumOperands() == 2) {
13563
13564 SDValue Value0 = StoredVal.getOperand(0);
13565 SDValue Value1 = StoredVal.getOperand(1);
13566
13567 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
13568 SDValue Ptr0 = St->getBasePtr();
13569 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
13570
13571 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
13572 St->getPointerInfo(), St->isVolatile(),
13573 St->isNonTemporal(), St->getAlignment());
13574 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
13575 St->getPointerInfo(), St->isVolatile(),
13576 St->isNonTemporal(), St->getAlignment());
13577 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
13578 }
Nadav Rotem614061b2011-08-10 19:30:14 +000013579
13580 // Optimize trunc store (of multiple scalars) to shuffle and store.
13581 // First, pack all of the elements in one place. Next, store to memory
13582 // in fewer chunks.
13583 if (St->isTruncatingStore() && VT.isVector()) {
13584 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13585 unsigned NumElems = VT.getVectorNumElements();
13586 assert(StVT != VT && "Cannot truncate to the same type");
13587 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
13588 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
13589
13590 // From, To sizes and ElemCount must be pow of two
13591 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013592 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000013593 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013594 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013595
Nadav Rotem614061b2011-08-10 19:30:14 +000013596 unsigned SizeRatio = FromSz / ToSz;
13597
13598 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
13599
13600 // Create a type on which we perform the shuffle
13601 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
13602 StVT.getScalarType(), NumElems*SizeRatio);
13603
13604 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
13605
13606 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
13607 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13608 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
13609
13610 // Can't shuffle using an illegal type
13611 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13612
13613 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
13614 DAG.getUNDEF(WideVec.getValueType()),
13615 ShuffleVec.data());
13616 // At this point all of the data is stored at the bottom of the
13617 // register. We now need to save it to mem.
13618
13619 // Find the largest store unit
13620 MVT StoreType = MVT::i8;
13621 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13622 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13623 MVT Tp = (MVT::SimpleValueType)tp;
13624 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
13625 StoreType = Tp;
13626 }
13627
13628 // Bitcast the original vector into a vector of store-size units
13629 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
13630 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
13631 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
13632 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
13633 SmallVector<SDValue, 8> Chains;
13634 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
13635 TLI.getPointerTy());
13636 SDValue Ptr = St->getBasePtr();
13637
13638 // Perform one or more big stores into memory.
13639 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
13640 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
13641 StoreType, ShuffWide,
13642 DAG.getIntPtrConstant(i));
13643 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
13644 St->getPointerInfo(), St->isVolatile(),
13645 St->isNonTemporal(), St->getAlignment());
13646 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
13647 Chains.push_back(Ch);
13648 }
13649
13650 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
13651 Chains.size());
13652 }
13653
13654
Chris Lattner149a4e52008-02-22 02:09:43 +000013655 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
13656 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000013657 // A preferable solution to the general problem is to figure out the right
13658 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000013659
13660 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000013661 if (VT.getSizeInBits() != 64)
13662 return SDValue();
13663
Devang Patel578efa92009-06-05 21:57:13 +000013664 const Function *F = DAG.getMachineFunction().getFunction();
13665 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000013666 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013667 && Subtarget->hasXMMInt();
Evan Cheng536e6672009-03-12 05:59:15 +000013668 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000013669 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000013670 isa<LoadSDNode>(St->getValue()) &&
13671 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
13672 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013673 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013674 LoadSDNode *Ld = 0;
13675 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000013676 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000013677 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013678 // Must be a store of a load. We currently handle two cases: the load
13679 // is a direct child, and it's under an intervening TokenFactor. It is
13680 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000013681 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000013682 Ld = cast<LoadSDNode>(St->getChain());
13683 else if (St->getValue().hasOneUse() &&
13684 ChainVal->getOpcode() == ISD::TokenFactor) {
13685 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013686 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000013687 TokenFactorIndex = i;
13688 Ld = cast<LoadSDNode>(St->getValue());
13689 } else
13690 Ops.push_back(ChainVal->getOperand(i));
13691 }
13692 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000013693
Evan Cheng536e6672009-03-12 05:59:15 +000013694 if (!Ld || !ISD::isNormalLoad(Ld))
13695 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013696
Evan Cheng536e6672009-03-12 05:59:15 +000013697 // If this is not the MMX case, i.e. we are just turning i64 load/store
13698 // into f64 load/store, avoid the transformation if there are multiple
13699 // uses of the loaded value.
13700 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
13701 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013702
Evan Cheng536e6672009-03-12 05:59:15 +000013703 DebugLoc LdDL = Ld->getDebugLoc();
13704 DebugLoc StDL = N->getDebugLoc();
13705 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
13706 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
13707 // pair instead.
13708 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013709 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000013710 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
13711 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013712 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013713 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000013714 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000013715 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000013716 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000013717 Ops.size());
13718 }
Evan Cheng536e6672009-03-12 05:59:15 +000013719 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013720 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013721 St->isVolatile(), St->isNonTemporal(),
13722 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000013723 }
Evan Cheng536e6672009-03-12 05:59:15 +000013724
13725 // Otherwise, lower to two pairs of 32-bit loads / stores.
13726 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013727 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
13728 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013729
Owen Anderson825b72b2009-08-11 20:47:22 +000013730 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013731 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013732 Ld->isVolatile(), Ld->isNonTemporal(),
13733 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000013734 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013735 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000013736 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013737 MinAlign(Ld->getAlignment(), 4));
13738
13739 SDValue NewChain = LoLd.getValue(1);
13740 if (TokenFactorIndex != -1) {
13741 Ops.push_back(LoLd);
13742 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000013743 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000013744 Ops.size());
13745 }
13746
13747 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013748 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
13749 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013750
13751 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013752 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013753 St->isVolatile(), St->isNonTemporal(),
13754 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013755 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013756 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000013757 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013758 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013759 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000013760 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000013761 }
Dan Gohman475871a2008-07-27 21:46:04 +000013762 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000013763}
13764
Duncan Sands17470be2011-09-22 20:15:48 +000013765/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
13766/// and return the operands for the horizontal operation in LHS and RHS. A
13767/// horizontal operation performs the binary operation on successive elements
13768/// of its first operand, then on successive elements of its second operand,
13769/// returning the resulting values in a vector. For example, if
13770/// A = < float a0, float a1, float a2, float a3 >
13771/// and
13772/// B = < float b0, float b1, float b2, float b3 >
13773/// then the result of doing a horizontal operation on A and B is
13774/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
13775/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
13776/// A horizontal-op B, for some already available A and B, and if so then LHS is
13777/// set to A, RHS to B, and the routine returns 'true'.
13778/// Note that the binary operation should have the property that if one of the
13779/// operands is UNDEF then the result is UNDEF.
13780static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool isCommutative) {
13781 // Look for the following pattern: if
13782 // A = < float a0, float a1, float a2, float a3 >
13783 // B = < float b0, float b1, float b2, float b3 >
13784 // and
13785 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
13786 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
13787 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
13788 // which is A horizontal-op B.
13789
13790 // At least one of the operands should be a vector shuffle.
13791 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
13792 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
13793 return false;
13794
13795 EVT VT = LHS.getValueType();
13796 unsigned N = VT.getVectorNumElements();
13797
13798 // View LHS in the form
13799 // LHS = VECTOR_SHUFFLE A, B, LMask
13800 // If LHS is not a shuffle then pretend it is the shuffle
13801 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
13802 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
13803 // type VT.
13804 SDValue A, B;
13805 SmallVector<int, 8> LMask(N);
13806 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
13807 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
13808 A = LHS.getOperand(0);
13809 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
13810 B = LHS.getOperand(1);
13811 cast<ShuffleVectorSDNode>(LHS.getNode())->getMask(LMask);
13812 } else {
13813 if (LHS.getOpcode() != ISD::UNDEF)
13814 A = LHS;
13815 for (unsigned i = 0; i != N; ++i)
13816 LMask[i] = i;
13817 }
13818
13819 // Likewise, view RHS in the form
13820 // RHS = VECTOR_SHUFFLE C, D, RMask
13821 SDValue C, D;
13822 SmallVector<int, 8> RMask(N);
13823 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
13824 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
13825 C = RHS.getOperand(0);
13826 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
13827 D = RHS.getOperand(1);
13828 cast<ShuffleVectorSDNode>(RHS.getNode())->getMask(RMask);
13829 } else {
13830 if (RHS.getOpcode() != ISD::UNDEF)
13831 C = RHS;
13832 for (unsigned i = 0; i != N; ++i)
13833 RMask[i] = i;
13834 }
13835
13836 // Check that the shuffles are both shuffling the same vectors.
13837 if (!(A == C && B == D) && !(A == D && B == C))
13838 return false;
13839
13840 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
13841 if (!A.getNode() && !B.getNode())
13842 return false;
13843
13844 // If A and B occur in reverse order in RHS, then "swap" them (which means
13845 // rewriting the mask).
13846 if (A != C)
13847 for (unsigned i = 0; i != N; ++i) {
13848 unsigned Idx = RMask[i];
13849 if (Idx < N)
13850 RMask[i] += N;
13851 else if (Idx < 2*N)
13852 RMask[i] -= N;
13853 }
13854
13855 // At this point LHS and RHS are equivalent to
13856 // LHS = VECTOR_SHUFFLE A, B, LMask
13857 // RHS = VECTOR_SHUFFLE A, B, RMask
13858 // Check that the masks correspond to performing a horizontal operation.
13859 for (unsigned i = 0; i != N; ++i) {
13860 unsigned LIdx = LMask[i], RIdx = RMask[i];
13861
13862 // Ignore any UNDEF components.
13863 if (LIdx >= 2*N || RIdx >= 2*N || (!A.getNode() && (LIdx < N || RIdx < N))
13864 || (!B.getNode() && (LIdx >= N || RIdx >= N)))
13865 continue;
13866
13867 // Check that successive elements are being operated on. If not, this is
13868 // not a horizontal operation.
13869 if (!(LIdx == 2*i && RIdx == 2*i + 1) &&
13870 !(isCommutative && LIdx == 2*i + 1 && RIdx == 2*i))
13871 return false;
13872 }
13873
13874 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
13875 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
13876 return true;
13877}
13878
13879/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
13880static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
13881 const X86Subtarget *Subtarget) {
13882 EVT VT = N->getValueType(0);
13883 SDValue LHS = N->getOperand(0);
13884 SDValue RHS = N->getOperand(1);
13885
13886 // Try to synthesize horizontal adds from adds of shuffles.
13887 if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
13888 (VT == MVT::v4f32 || VT == MVT::v2f64) &&
13889 isHorizontalBinOp(LHS, RHS, true))
13890 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
13891 return SDValue();
13892}
13893
13894/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
13895static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
13896 const X86Subtarget *Subtarget) {
13897 EVT VT = N->getValueType(0);
13898 SDValue LHS = N->getOperand(0);
13899 SDValue RHS = N->getOperand(1);
13900
13901 // Try to synthesize horizontal subs from subs of shuffles.
13902 if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
13903 (VT == MVT::v4f32 || VT == MVT::v2f64) &&
13904 isHorizontalBinOp(LHS, RHS, false))
13905 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
13906 return SDValue();
13907}
13908
Chris Lattner6cf73262008-01-25 06:14:17 +000013909/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
13910/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013911static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000013912 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
13913 // F[X]OR(0.0, x) -> x
13914 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000013915 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13916 if (C->getValueAPF().isPosZero())
13917 return N->getOperand(1);
13918 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13919 if (C->getValueAPF().isPosZero())
13920 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000013921 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013922}
13923
13924/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013925static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000013926 // FAND(0.0, x) -> 0.0
13927 // FAND(x, 0.0) -> 0.0
13928 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13929 if (C->getValueAPF().isPosZero())
13930 return N->getOperand(0);
13931 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13932 if (C->getValueAPF().isPosZero())
13933 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000013934 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013935}
13936
Dan Gohmane5af2d32009-01-29 01:59:02 +000013937static SDValue PerformBTCombine(SDNode *N,
13938 SelectionDAG &DAG,
13939 TargetLowering::DAGCombinerInfo &DCI) {
13940 // BT ignores high bits in the bit index operand.
13941 SDValue Op1 = N->getOperand(1);
13942 if (Op1.hasOneUse()) {
13943 unsigned BitWidth = Op1.getValueSizeInBits();
13944 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
13945 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013946 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
13947 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000013948 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000013949 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
13950 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
13951 DCI.CommitTargetLoweringOpt(TLO);
13952 }
13953 return SDValue();
13954}
Chris Lattner83e6c992006-10-04 06:57:07 +000013955
Eli Friedman7a5e5552009-06-07 06:52:44 +000013956static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
13957 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013958 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000013959 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000013960 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000013961 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000013962 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000013963 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013964 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000013965 }
13966 return SDValue();
13967}
13968
Evan Cheng2e489c42009-12-16 00:53:11 +000013969static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
13970 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
13971 // (and (i32 x86isd::setcc_carry), 1)
13972 // This eliminates the zext. This transformation is necessary because
13973 // ISD::SETCC is always legalized to i8.
13974 DebugLoc dl = N->getDebugLoc();
13975 SDValue N0 = N->getOperand(0);
13976 EVT VT = N->getValueType(0);
13977 if (N0.getOpcode() == ISD::AND &&
13978 N0.hasOneUse() &&
13979 N0.getOperand(0).hasOneUse()) {
13980 SDValue N00 = N0.getOperand(0);
13981 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
13982 return SDValue();
13983 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
13984 if (!C || C->getZExtValue() != 1)
13985 return SDValue();
13986 return DAG.getNode(ISD::AND, dl, VT,
13987 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
13988 N00.getOperand(0), N00.getOperand(1)),
13989 DAG.getConstant(1, VT));
13990 }
13991
13992 return SDValue();
13993}
13994
Chris Lattnerc19d1c32010-12-19 22:08:31 +000013995// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
13996static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
13997 unsigned X86CC = N->getConstantOperandVal(0);
13998 SDValue EFLAG = N->getOperand(1);
13999 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014000
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014001 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
14002 // a zext and produces an all-ones bit which is more useful than 0/1 in some
14003 // cases.
14004 if (X86CC == X86::COND_B)
14005 return DAG.getNode(ISD::AND, DL, MVT::i8,
14006 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
14007 DAG.getConstant(X86CC, MVT::i8), EFLAG),
14008 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014009
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014010 return SDValue();
14011}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014012
Benjamin Kramer1396c402011-06-18 11:09:41 +000014013static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
14014 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014015 SDValue Op0 = N->getOperand(0);
14016 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
14017 // a 32-bit target where SSE doesn't support i64->FP operations.
14018 if (Op0.getOpcode() == ISD::LOAD) {
14019 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
14020 EVT VT = Ld->getValueType(0);
14021 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
14022 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
14023 !XTLI->getSubtarget()->is64Bit() &&
14024 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000014025 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
14026 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014027 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
14028 return FILDChain;
14029 }
14030 }
14031 return SDValue();
14032}
14033
Chris Lattner23a01992010-12-20 01:37:09 +000014034// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
14035static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
14036 X86TargetLowering::DAGCombinerInfo &DCI) {
14037 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
14038 // the result is either zero or one (depending on the input carry bit).
14039 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
14040 if (X86::isZeroNode(N->getOperand(0)) &&
14041 X86::isZeroNode(N->getOperand(1)) &&
14042 // We don't have a good way to replace an EFLAGS use, so only do this when
14043 // dead right now.
14044 SDValue(N, 1).use_empty()) {
14045 DebugLoc DL = N->getDebugLoc();
14046 EVT VT = N->getValueType(0);
14047 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
14048 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
14049 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
14050 DAG.getConstant(X86::COND_B,MVT::i8),
14051 N->getOperand(2)),
14052 DAG.getConstant(1, VT));
14053 return DCI.CombineTo(N, Res1, CarryOut);
14054 }
14055
14056 return SDValue();
14057}
14058
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014059// fold (add Y, (sete X, 0)) -> adc 0, Y
14060// (add Y, (setne X, 0)) -> sbb -1, Y
14061// (sub (sete X, 0), Y) -> sbb 0, Y
14062// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014063static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014064 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014065
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014066 // Look through ZExts.
14067 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
14068 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
14069 return SDValue();
14070
14071 SDValue SetCC = Ext.getOperand(0);
14072 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
14073 return SDValue();
14074
14075 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
14076 if (CC != X86::COND_E && CC != X86::COND_NE)
14077 return SDValue();
14078
14079 SDValue Cmp = SetCC.getOperand(1);
14080 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000014081 !X86::isZeroNode(Cmp.getOperand(1)) ||
14082 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014083 return SDValue();
14084
14085 SDValue CmpOp0 = Cmp.getOperand(0);
14086 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
14087 DAG.getConstant(1, CmpOp0.getValueType()));
14088
14089 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
14090 if (CC == X86::COND_NE)
14091 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
14092 DL, OtherVal.getValueType(), OtherVal,
14093 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
14094 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
14095 DL, OtherVal.getValueType(), OtherVal,
14096 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
14097}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014098
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014099static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG) {
14100 SDValue Op0 = N->getOperand(0);
14101 SDValue Op1 = N->getOperand(1);
14102
14103 // X86 can't encode an immediate LHS of a sub. See if we can push the
14104 // negation into a preceding instruction.
14105 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014106 // If the RHS of the sub is a XOR with one use and a constant, invert the
14107 // immediate. Then add one to the LHS of the sub so we can turn
14108 // X-Y -> X+~Y+1, saving one register.
14109 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
14110 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000014111 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014112 EVT VT = Op0.getValueType();
14113 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
14114 Op1.getOperand(0),
14115 DAG.getConstant(~XorC, VT));
14116 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000014117 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014118 }
14119 }
14120
14121 return OptimizeConditionalInDecrement(N, DAG);
14122}
14123
Dan Gohman475871a2008-07-27 21:46:04 +000014124SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000014125 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014126 SelectionDAG &DAG = DCI.DAG;
14127 switch (N->getOpcode()) {
14128 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014129 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014130 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000014131 case ISD::VSELECT:
Chris Lattneraf723b92008-01-25 05:46:26 +000014132 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014133 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014134 case ISD::ADD: return OptimizeConditionalInDecrement(N, DAG);
14135 case ISD::SUB: return PerformSubCombine(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000014136 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000014137 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000014138 case ISD::SHL:
14139 case ISD::SRA:
14140 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000014141 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000014142 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014143 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000014144 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014145 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000014146 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
14147 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000014148 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000014149 case X86ISD::FOR: return PerformFORCombine(N, DAG);
14150 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000014151 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014152 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000014153 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014154 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014155 case X86ISD::SHUFPS: // Handle all target specific shuffles
14156 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000014157 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014158 case X86ISD::PUNPCKHBW:
14159 case X86ISD::PUNPCKHWD:
14160 case X86ISD::PUNPCKHDQ:
14161 case X86ISD::PUNPCKHQDQ:
14162 case X86ISD::UNPCKHPS:
14163 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +000014164 case X86ISD::VUNPCKHPSY:
14165 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014166 case X86ISD::PUNPCKLBW:
14167 case X86ISD::PUNPCKLWD:
14168 case X86ISD::PUNPCKLDQ:
14169 case X86ISD::PUNPCKLQDQ:
14170 case X86ISD::UNPCKLPS:
14171 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000014172 case X86ISD::VUNPCKLPSY:
14173 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014174 case X86ISD::MOVHLPS:
14175 case X86ISD::MOVLHPS:
14176 case X86ISD::PSHUFD:
14177 case X86ISD::PSHUFHW:
14178 case X86ISD::PSHUFLW:
14179 case X86ISD::MOVSS:
14180 case X86ISD::MOVSD:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000014181 case X86ISD::VPERMILPS:
14182 case X86ISD::VPERMILPSY:
14183 case X86ISD::VPERMILPD:
14184 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000014185 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014186 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014187 }
14188
Dan Gohman475871a2008-07-27 21:46:04 +000014189 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014190}
14191
Evan Chenge5b51ac2010-04-17 06:13:15 +000014192/// isTypeDesirableForOp - Return true if the target has native support for
14193/// the specified value type and it is 'desirable' to use the type for the
14194/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14195/// instruction encodings are longer and some i16 instructions are slow.
14196bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14197 if (!isTypeLegal(VT))
14198 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014199 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014200 return true;
14201
14202 switch (Opc) {
14203 default:
14204 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014205 case ISD::LOAD:
14206 case ISD::SIGN_EXTEND:
14207 case ISD::ZERO_EXTEND:
14208 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014209 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014210 case ISD::SRL:
14211 case ISD::SUB:
14212 case ISD::ADD:
14213 case ISD::MUL:
14214 case ISD::AND:
14215 case ISD::OR:
14216 case ISD::XOR:
14217 return false;
14218 }
14219}
14220
14221/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014222/// beneficial for dag combiner to promote the specified node. If true, it
14223/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014224bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014225 EVT VT = Op.getValueType();
14226 if (VT != MVT::i16)
14227 return false;
14228
Evan Cheng4c26e932010-04-19 19:29:22 +000014229 bool Promote = false;
14230 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014231 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014232 default: break;
14233 case ISD::LOAD: {
14234 LoadSDNode *LD = cast<LoadSDNode>(Op);
14235 // If the non-extending load has a single use and it's not live out, then it
14236 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014237 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14238 Op.hasOneUse()*/) {
14239 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14240 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14241 // The only case where we'd want to promote LOAD (rather then it being
14242 // promoted as an operand is when it's only use is liveout.
14243 if (UI->getOpcode() != ISD::CopyToReg)
14244 return false;
14245 }
14246 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014247 Promote = true;
14248 break;
14249 }
14250 case ISD::SIGN_EXTEND:
14251 case ISD::ZERO_EXTEND:
14252 case ISD::ANY_EXTEND:
14253 Promote = true;
14254 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014255 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014256 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014257 SDValue N0 = Op.getOperand(0);
14258 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014259 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014260 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014261 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014262 break;
14263 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014264 case ISD::ADD:
14265 case ISD::MUL:
14266 case ISD::AND:
14267 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014268 case ISD::XOR:
14269 Commute = true;
14270 // fallthrough
14271 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014272 SDValue N0 = Op.getOperand(0);
14273 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014274 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014275 return false;
14276 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014277 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014278 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014279 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014280 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014281 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014282 }
14283 }
14284
14285 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014286 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014287}
14288
Evan Cheng60c07e12006-07-05 22:17:51 +000014289//===----------------------------------------------------------------------===//
14290// X86 Inline Assembly Support
14291//===----------------------------------------------------------------------===//
14292
Chris Lattnerb8105652009-07-20 17:51:36 +000014293bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14294 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014295
14296 std::string AsmStr = IA->getAsmString();
14297
14298 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014299 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014300 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014301
14302 switch (AsmPieces.size()) {
14303 default: return false;
14304 case 1:
14305 AsmStr = AsmPieces[0];
14306 AsmPieces.clear();
14307 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
14308
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014309 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000014310 // we will turn this bswap into something that will be lowered to logical ops
14311 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14312 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014313 // bswap $0
14314 if (AsmPieces.size() == 2 &&
14315 (AsmPieces[0] == "bswap" ||
14316 AsmPieces[0] == "bswapq" ||
14317 AsmPieces[0] == "bswapl") &&
14318 (AsmPieces[1] == "$0" ||
14319 AsmPieces[1] == "${0:q}")) {
14320 // No need to check constraints, nothing other than the equivalent of
14321 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014322 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014323 if (!Ty || Ty->getBitWidth() % 16 != 0)
14324 return false;
14325 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014326 }
14327 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014328 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014329 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014330 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014331 AsmPieces[1] == "$$8," &&
14332 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014333 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14334 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014335 const std::string &ConstraintsStr = IA->getConstraintString();
14336 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014337 std::sort(AsmPieces.begin(), AsmPieces.end());
14338 if (AsmPieces.size() == 4 &&
14339 AsmPieces[0] == "~{cc}" &&
14340 AsmPieces[1] == "~{dirflag}" &&
14341 AsmPieces[2] == "~{flags}" &&
14342 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014343 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014344 if (!Ty || Ty->getBitWidth() % 16 != 0)
14345 return false;
14346 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014347 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014348 }
14349 break;
14350 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014351 if (CI->getType()->isIntegerTy(32) &&
14352 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14353 SmallVector<StringRef, 4> Words;
14354 SplitString(AsmPieces[0], Words, " \t,");
14355 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14356 Words[2] == "${0:w}") {
14357 Words.clear();
14358 SplitString(AsmPieces[1], Words, " \t,");
14359 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14360 Words[2] == "$0") {
14361 Words.clear();
14362 SplitString(AsmPieces[2], Words, " \t,");
14363 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14364 Words[2] == "${0:w}") {
14365 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014366 const std::string &ConstraintsStr = IA->getConstraintString();
14367 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014368 std::sort(AsmPieces.begin(), AsmPieces.end());
14369 if (AsmPieces.size() == 4 &&
14370 AsmPieces[0] == "~{cc}" &&
14371 AsmPieces[1] == "~{dirflag}" &&
14372 AsmPieces[2] == "~{flags}" &&
14373 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014374 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014375 if (!Ty || Ty->getBitWidth() % 16 != 0)
14376 return false;
14377 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014378 }
14379 }
14380 }
14381 }
14382 }
Evan Cheng55d42002011-01-08 01:24:27 +000014383
14384 if (CI->getType()->isIntegerTy(64)) {
14385 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14386 if (Constraints.size() >= 2 &&
14387 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14388 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14389 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14390 SmallVector<StringRef, 4> Words;
14391 SplitString(AsmPieces[0], Words, " \t");
14392 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014393 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014394 SplitString(AsmPieces[1], Words, " \t");
14395 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14396 Words.clear();
14397 SplitString(AsmPieces[2], Words, " \t,");
14398 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14399 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014400 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014401 if (!Ty || Ty->getBitWidth() % 16 != 0)
14402 return false;
14403 return IntrinsicLowering::LowerToByteSwap(CI);
14404 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014405 }
14406 }
14407 }
14408 }
14409 break;
14410 }
14411 return false;
14412}
14413
14414
14415
Chris Lattnerf4dff842006-07-11 02:54:03 +000014416/// getConstraintType - Given a constraint letter, return the type of
14417/// constraint it is for this target.
14418X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014419X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14420 if (Constraint.size() == 1) {
14421 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014422 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014423 case 'q':
14424 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014425 case 'f':
14426 case 't':
14427 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014428 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014429 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014430 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014431 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014432 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014433 case 'a':
14434 case 'b':
14435 case 'c':
14436 case 'd':
14437 case 'S':
14438 case 'D':
14439 case 'A':
14440 return C_Register;
14441 case 'I':
14442 case 'J':
14443 case 'K':
14444 case 'L':
14445 case 'M':
14446 case 'N':
14447 case 'G':
14448 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014449 case 'e':
14450 case 'Z':
14451 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014452 default:
14453 break;
14454 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014455 }
Chris Lattner4234f572007-03-25 02:14:49 +000014456 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014457}
14458
John Thompson44ab89e2010-10-29 17:29:13 +000014459/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014460/// This object must already have been set up with the operand type
14461/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014462TargetLowering::ConstraintWeight
14463 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014464 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014465 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014466 Value *CallOperandVal = info.CallOperandVal;
14467 // If we don't have a value, we can't do a match,
14468 // but allow it at the lowest weight.
14469 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014470 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014471 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014472 // Look at the constraint type.
14473 switch (*constraint) {
14474 default:
John Thompson44ab89e2010-10-29 17:29:13 +000014475 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
14476 case 'R':
14477 case 'q':
14478 case 'Q':
14479 case 'a':
14480 case 'b':
14481 case 'c':
14482 case 'd':
14483 case 'S':
14484 case 'D':
14485 case 'A':
14486 if (CallOperandVal->getType()->isIntegerTy())
14487 weight = CW_SpecificReg;
14488 break;
14489 case 'f':
14490 case 't':
14491 case 'u':
14492 if (type->isFloatingPointTy())
14493 weight = CW_SpecificReg;
14494 break;
14495 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000014496 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000014497 weight = CW_SpecificReg;
14498 break;
14499 case 'x':
14500 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014501 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000014502 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014503 break;
14504 case 'I':
14505 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
14506 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000014507 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014508 }
14509 break;
John Thompson44ab89e2010-10-29 17:29:13 +000014510 case 'J':
14511 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14512 if (C->getZExtValue() <= 63)
14513 weight = CW_Constant;
14514 }
14515 break;
14516 case 'K':
14517 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14518 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
14519 weight = CW_Constant;
14520 }
14521 break;
14522 case 'L':
14523 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14524 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
14525 weight = CW_Constant;
14526 }
14527 break;
14528 case 'M':
14529 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14530 if (C->getZExtValue() <= 3)
14531 weight = CW_Constant;
14532 }
14533 break;
14534 case 'N':
14535 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14536 if (C->getZExtValue() <= 0xff)
14537 weight = CW_Constant;
14538 }
14539 break;
14540 case 'G':
14541 case 'C':
14542 if (dyn_cast<ConstantFP>(CallOperandVal)) {
14543 weight = CW_Constant;
14544 }
14545 break;
14546 case 'e':
14547 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14548 if ((C->getSExtValue() >= -0x80000000LL) &&
14549 (C->getSExtValue() <= 0x7fffffffLL))
14550 weight = CW_Constant;
14551 }
14552 break;
14553 case 'Z':
14554 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14555 if (C->getZExtValue() <= 0xffffffff)
14556 weight = CW_Constant;
14557 }
14558 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014559 }
14560 return weight;
14561}
14562
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014563/// LowerXConstraint - try to replace an X constraint, which matches anything,
14564/// with another that has more specific requirements based on the type of the
14565/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000014566const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000014567LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000014568 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
14569 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000014570 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014571 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000014572 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014573 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000014574 return "x";
14575 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014576
Chris Lattner5e764232008-04-26 23:02:14 +000014577 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014578}
14579
Chris Lattner48884cd2007-08-25 00:47:38 +000014580/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
14581/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000014582void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000014583 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000014584 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000014585 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000014586 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000014587
Eric Christopher100c8332011-06-02 23:16:42 +000014588 // Only support length 1 constraints for now.
14589 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000014590
Eric Christopher100c8332011-06-02 23:16:42 +000014591 char ConstraintLetter = Constraint[0];
14592 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014593 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000014594 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000014595 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014596 if (C->getZExtValue() <= 31) {
14597 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014598 break;
14599 }
Devang Patel84f7fd22007-03-17 00:13:28 +000014600 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014601 return;
Evan Cheng364091e2008-09-22 23:57:37 +000014602 case 'J':
14603 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014604 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000014605 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14606 break;
14607 }
14608 }
14609 return;
14610 case 'K':
14611 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014612 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000014613 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14614 break;
14615 }
14616 }
14617 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000014618 case 'N':
14619 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014620 if (C->getZExtValue() <= 255) {
14621 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014622 break;
14623 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000014624 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014625 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000014626 case 'e': {
14627 // 32-bit signed value
14628 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014629 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14630 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014631 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014632 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000014633 break;
14634 }
14635 // FIXME gcc accepts some relocatable values here too, but only in certain
14636 // memory models; it's complicated.
14637 }
14638 return;
14639 }
14640 case 'Z': {
14641 // 32-bit unsigned value
14642 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014643 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14644 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014645 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14646 break;
14647 }
14648 }
14649 // FIXME gcc accepts some relocatable values here too, but only in certain
14650 // memory models; it's complicated.
14651 return;
14652 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014653 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014654 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000014655 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014656 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014657 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000014658 break;
14659 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014660
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014661 // In any sort of PIC mode addresses need to be computed at runtime by
14662 // adding in a register or some sort of table lookup. These can't
14663 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000014664 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014665 return;
14666
Chris Lattnerdc43a882007-05-03 16:52:29 +000014667 // If we are in non-pic codegen mode, we allow the address of a global (with
14668 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000014669 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014670 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000014671
Chris Lattner49921962009-05-08 18:23:14 +000014672 // Match either (GA), (GA+C), (GA+C1+C2), etc.
14673 while (1) {
14674 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
14675 Offset += GA->getOffset();
14676 break;
14677 } else if (Op.getOpcode() == ISD::ADD) {
14678 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14679 Offset += C->getZExtValue();
14680 Op = Op.getOperand(0);
14681 continue;
14682 }
14683 } else if (Op.getOpcode() == ISD::SUB) {
14684 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14685 Offset += -C->getZExtValue();
14686 Op = Op.getOperand(0);
14687 continue;
14688 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014689 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014690
Chris Lattner49921962009-05-08 18:23:14 +000014691 // Otherwise, this isn't something we can handle, reject it.
14692 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014693 }
Eric Christopherfd179292009-08-27 18:07:15 +000014694
Dan Gohman46510a72010-04-15 01:51:59 +000014695 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014696 // If we require an extra load to get this address, as in PIC mode, we
14697 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000014698 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
14699 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014700 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000014701
Devang Patel0d881da2010-07-06 22:08:15 +000014702 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
14703 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000014704 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014705 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014706 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014707
Gabor Greifba36cb52008-08-28 21:40:38 +000014708 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000014709 Ops.push_back(Result);
14710 return;
14711 }
Dale Johannesen1784d162010-06-25 21:55:36 +000014712 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014713}
14714
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014715std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000014716X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000014717 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000014718 // First, see if this is a constraint that directly corresponds to an LLVM
14719 // register class.
14720 if (Constraint.size() == 1) {
14721 // GCC Constraint Letters
14722 switch (Constraint[0]) {
14723 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000014724 // TODO: Slight differences here in allocation order and leaving
14725 // RIP in the class. Do they matter any more here than they do
14726 // in the normal allocation?
14727 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
14728 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014729 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014730 return std::make_pair(0U, X86::GR32RegisterClass);
14731 else if (VT == MVT::i16)
14732 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014733 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014734 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014735 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000014736 return std::make_pair(0U, X86::GR64RegisterClass);
14737 break;
14738 }
14739 // 32-bit fallthrough
14740 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014741 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014742 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
14743 else if (VT == MVT::i16)
14744 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014745 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014746 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
14747 else if (VT == MVT::i64)
14748 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
14749 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014750 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000014751 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014752 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000014753 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014754 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000014755 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000014756 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000014757 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000014758 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014759 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014760 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014761 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
14762 if (VT == MVT::i16)
14763 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
14764 if (VT == MVT::i32 || !Subtarget->is64Bit())
14765 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
14766 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014767 case 'f': // FP Stack registers.
14768 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
14769 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000014770 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014771 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014772 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014773 return std::make_pair(0U, X86::RFP64RegisterClass);
14774 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000014775 case 'y': // MMX_REGS if MMX allowed.
14776 if (!Subtarget->hasMMX()) break;
14777 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014778 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014779 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014780 // FALL THROUGH.
14781 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014782 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000014783
Owen Anderson825b72b2009-08-11 20:47:22 +000014784 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000014785 default: break;
14786 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014787 case MVT::f32:
14788 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000014789 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014790 case MVT::f64:
14791 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000014792 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014793 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014794 case MVT::v16i8:
14795 case MVT::v8i16:
14796 case MVT::v4i32:
14797 case MVT::v2i64:
14798 case MVT::v4f32:
14799 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000014800 return std::make_pair(0U, X86::VR128RegisterClass);
14801 }
Chris Lattnerad043e82007-04-09 05:11:28 +000014802 break;
14803 }
14804 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014805
Chris Lattnerf76d1802006-07-31 23:26:50 +000014806 // Use the default implementation in TargetLowering to convert the register
14807 // constraint into a member of a register class.
14808 std::pair<unsigned, const TargetRegisterClass*> Res;
14809 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000014810
14811 // Not found as a standard register?
14812 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014813 // Map st(0) -> st(7) -> ST0
14814 if (Constraint.size() == 7 && Constraint[0] == '{' &&
14815 tolower(Constraint[1]) == 's' &&
14816 tolower(Constraint[2]) == 't' &&
14817 Constraint[3] == '(' &&
14818 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
14819 Constraint[5] == ')' &&
14820 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000014821
Chris Lattner56d77c72009-09-13 22:41:48 +000014822 Res.first = X86::ST0+Constraint[4]-'0';
14823 Res.second = X86::RFP80RegisterClass;
14824 return Res;
14825 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014826
Chris Lattner56d77c72009-09-13 22:41:48 +000014827 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014828 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000014829 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000014830 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014831 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000014832 }
Chris Lattner56d77c72009-09-13 22:41:48 +000014833
14834 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014835 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014836 Res.first = X86::EFLAGS;
14837 Res.second = X86::CCRRegisterClass;
14838 return Res;
14839 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014840
Dale Johannesen330169f2008-11-13 21:52:36 +000014841 // 'A' means EAX + EDX.
14842 if (Constraint == "A") {
14843 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000014844 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014845 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000014846 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000014847 return Res;
14848 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014849
Chris Lattnerf76d1802006-07-31 23:26:50 +000014850 // Otherwise, check to see if this is a register class of the wrong value
14851 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
14852 // turn into {ax},{dx}.
14853 if (Res.second->hasType(VT))
14854 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014855
Chris Lattnerf76d1802006-07-31 23:26:50 +000014856 // All of the single-register GCC register classes map their values onto
14857 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
14858 // really want an 8-bit or 32-bit register, map to the appropriate register
14859 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000014860 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014861 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014862 unsigned DestReg = 0;
14863 switch (Res.first) {
14864 default: break;
14865 case X86::AX: DestReg = X86::AL; break;
14866 case X86::DX: DestReg = X86::DL; break;
14867 case X86::CX: DestReg = X86::CL; break;
14868 case X86::BX: DestReg = X86::BL; break;
14869 }
14870 if (DestReg) {
14871 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014872 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014873 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014874 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014875 unsigned DestReg = 0;
14876 switch (Res.first) {
14877 default: break;
14878 case X86::AX: DestReg = X86::EAX; break;
14879 case X86::DX: DestReg = X86::EDX; break;
14880 case X86::CX: DestReg = X86::ECX; break;
14881 case X86::BX: DestReg = X86::EBX; break;
14882 case X86::SI: DestReg = X86::ESI; break;
14883 case X86::DI: DestReg = X86::EDI; break;
14884 case X86::BP: DestReg = X86::EBP; break;
14885 case X86::SP: DestReg = X86::ESP; break;
14886 }
14887 if (DestReg) {
14888 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014889 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014890 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014891 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014892 unsigned DestReg = 0;
14893 switch (Res.first) {
14894 default: break;
14895 case X86::AX: DestReg = X86::RAX; break;
14896 case X86::DX: DestReg = X86::RDX; break;
14897 case X86::CX: DestReg = X86::RCX; break;
14898 case X86::BX: DestReg = X86::RBX; break;
14899 case X86::SI: DestReg = X86::RSI; break;
14900 case X86::DI: DestReg = X86::RDI; break;
14901 case X86::BP: DestReg = X86::RBP; break;
14902 case X86::SP: DestReg = X86::RSP; break;
14903 }
14904 if (DestReg) {
14905 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014906 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014907 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000014908 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000014909 } else if (Res.second == X86::FR32RegisterClass ||
14910 Res.second == X86::FR64RegisterClass ||
14911 Res.second == X86::VR128RegisterClass) {
14912 // Handle references to XMM physical registers that got mapped into the
14913 // wrong class. This can happen with constraints like {xmm0} where the
14914 // target independent register mapper will just pick the first match it can
14915 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000014916 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014917 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000014918 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014919 Res.second = X86::FR64RegisterClass;
14920 else if (X86::VR128RegisterClass->hasType(VT))
14921 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000014922 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014923
Chris Lattnerf76d1802006-07-31 23:26:50 +000014924 return Res;
14925}