blob: 69de3a7513b4229eaf21b1f431fbfc9c714e705e [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000041#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000044#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000045#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000048#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000050#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000053#include "llvm/Support/raw_ostream.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000054#include "llvm/Target/TargetOptions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000055using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000056using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000057
Evan Chengb1712452010-01-27 06:25:16 +000058STATISTIC(NumTailCalls, "Number of tail calls");
59
Evan Cheng10e86422008-04-25 19:11:04 +000060// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000061static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000062 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000063
David Greenea5f26012011-02-07 19:36:54 +000064static SDValue Insert128BitVector(SDValue Result,
65 SDValue Vec,
66 SDValue Idx,
67 SelectionDAG &DAG,
68 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000069
David Greenea5f26012011-02-07 19:36:54 +000070static SDValue Extract128BitVector(SDValue Vec,
71 SDValue Idx,
72 SelectionDAG &DAG,
73 DebugLoc dl);
74
75/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
76/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000077/// simple subregister reference. Idx is an index in the 128 bits we
78/// want. It need not be aligned to a 128-bit bounday. That makes
79/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000080static SDValue Extract128BitVector(SDValue Vec,
81 SDValue Idx,
82 SelectionDAG &DAG,
83 DebugLoc dl) {
84 EVT VT = Vec.getValueType();
85 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000086 EVT ElVT = VT.getVectorElementType();
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000087 int Factor = VT.getSizeInBits()/128;
88 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
89 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000090
91 // Extract from UNDEF is UNDEF.
92 if (Vec.getOpcode() == ISD::UNDEF)
93 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
94
95 if (isa<ConstantSDNode>(Idx)) {
96 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
97
98 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
99 // we can match to VEXTRACTF128.
100 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
101
102 // This is the index of the first element of the 128-bit chunk
103 // we want.
104 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
105 * ElemsPerChunk);
106
107 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000108 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
109 VecIdx);
110
111 return Result;
112 }
113
114 return SDValue();
115}
116
117/// Generate a DAG to put 128-bits into a vector > 128 bits. This
118/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000119/// simple superregister reference. Idx is an index in the 128 bits
120/// we want. It need not be aligned to a 128-bit bounday. That makes
121/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000122static SDValue Insert128BitVector(SDValue Result,
123 SDValue Vec,
124 SDValue Idx,
125 SelectionDAG &DAG,
126 DebugLoc dl) {
127 if (isa<ConstantSDNode>(Idx)) {
128 EVT VT = Vec.getValueType();
129 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
130
131 EVT ElVT = VT.getVectorElementType();
David Greenea5f26012011-02-07 19:36:54 +0000132 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
David Greenea5f26012011-02-07 19:36:54 +0000133 EVT ResultVT = Result.getValueType();
134
135 // Insert the relevant 128 bits.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000136 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000137
138 // This is the index of the first element of the 128-bit chunk
139 // we want.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000140 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
David Greenea5f26012011-02-07 19:36:54 +0000141 * ElemsPerChunk);
142
143 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000144 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
145 VecIdx);
146 return Result;
147 }
148
149 return SDValue();
150}
151
Chris Lattnerf0144122009-07-28 03:13:23 +0000152static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000153 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
154 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000155
Evan Cheng2bffee22011-02-01 01:14:13 +0000156 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000157 if (is64Bit)
158 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000159 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000160 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000161
Evan Cheng203576a2011-07-20 19:50:42 +0000162 if (Subtarget->isTargetELF())
163 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000164 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000165 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000166 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000167}
168
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000169X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000170 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000171 Subtarget = &TM.getSubtarget<X86Subtarget>();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000172 X86ScalarSSEf64 = Subtarget->hasXMMInt();
173 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000174 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000175
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000176 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000177 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000178
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000179 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000180 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000181
182 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000183 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000184 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
185 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000186
Eric Christopherde5e1012011-03-11 01:05:58 +0000187 // For 64-bit since we have so many registers use the ILP scheduler, for
188 // 32-bit code use the register pressure specific scheduling.
189 if (Subtarget->is64Bit())
190 setSchedulingPreference(Sched::ILP);
191 else
192 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000193 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000194
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000195 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000196 // Setup Windows compiler runtime calls.
197 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000198 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000199 setLibcallName(RTLIB::SREM_I64, "_allrem");
200 setLibcallName(RTLIB::UREM_I64, "_aullrem");
201 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000202 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000203 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000204 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000205 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000206 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
207 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
208 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000209 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
210 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000211 }
212
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000213 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000214 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000215 setUseUnderscoreSetJmp(false);
216 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000217 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000218 // MS runtime is weird: it exports _setjmp, but longjmp!
219 setUseUnderscoreSetJmp(true);
220 setUseUnderscoreLongJmp(false);
221 } else {
222 setUseUnderscoreSetJmp(true);
223 setUseUnderscoreLongJmp(true);
224 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000225
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000226 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000228 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000230 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000232
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000234
Scott Michelfdc40a02009-02-17 22:15:04 +0000235 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000237 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000239 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
241 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000242
243 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
245 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
246 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
247 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
248 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
249 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000250
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000251 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
252 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000253 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
254 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
255 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000256
Evan Cheng25ab6902006-09-08 06:48:29 +0000257 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
259 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000260 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000261 // We have an algorithm for SSE2->double, and we turn this into a
262 // 64-bit FILD followed by conditional FADD for other targets.
263 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000264 // We have an algorithm for SSE2, and we turn this into a 64-bit
265 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000266 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000267 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000268
269 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
270 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
272 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000273
Devang Patel6a784892009-06-05 18:48:29 +0000274 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000275 // SSE has no i16 to fp conversion, only i32
276 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000278 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000280 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
282 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000283 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000284 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
286 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000287 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000288
Dale Johannesen73328d12007-09-19 23:55:34 +0000289 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
290 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
292 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000293
Evan Cheng02568ff2006-01-30 22:13:22 +0000294 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
295 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
297 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000298
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000299 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000301 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000303 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
305 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000306 }
307
308 // Handle FP_TO_UINT by promoting the destination to a larger signed
309 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
311 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
312 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000313
Evan Cheng25ab6902006-09-08 06:48:29 +0000314 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
316 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000317 } else if (!UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000318 // Since AVX is a superset of SSE3, only check for SSE here.
319 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 // Expand FP_TO_UINT into a select.
321 // FIXME: We would like to use a Custom expander here eventually to do
322 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000325 // With SSE3 we can use fisttpll to convert to a signed i64; without
326 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000328 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000329
Chris Lattner399610a2006-12-05 18:22:22 +0000330 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000331 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000332 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
333 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000334 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000335 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000336 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000337 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000338 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000339 }
Chris Lattner21f66852005-12-23 05:15:23 +0000340
Dan Gohmanb00ee212008-02-18 19:34:53 +0000341 // Scalar integer divide and remainder are lowered to use operations that
342 // produce two results, to match the available instructions. This exposes
343 // the two-result form to trivial CSE, which is able to combine x/y and x%y
344 // into a single instruction.
345 //
346 // Scalar integer multiply-high is also lowered to use two-result
347 // operations, to match the available instructions. However, plain multiply
348 // (low) operations are left as Legal, as there are single-result
349 // instructions for this in x86. Using the two-result multiply instructions
350 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000351 for (unsigned i = 0, e = 4; i != e; ++i) {
352 MVT VT = IntVTs[i];
353 setOperationAction(ISD::MULHS, VT, Expand);
354 setOperationAction(ISD::MULHU, VT, Expand);
355 setOperationAction(ISD::SDIV, VT, Expand);
356 setOperationAction(ISD::UDIV, VT, Expand);
357 setOperationAction(ISD::SREM, VT, Expand);
358 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000359
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000360 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000361 setOperationAction(ISD::ADDC, VT, Custom);
362 setOperationAction(ISD::ADDE, VT, Custom);
363 setOperationAction(ISD::SUBC, VT, Custom);
364 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000365 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000366
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
368 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
369 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
370 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000371 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
375 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
376 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
377 setOperationAction(ISD::FREM , MVT::f32 , Expand);
378 setOperationAction(ISD::FREM , MVT::f64 , Expand);
379 setOperationAction(ISD::FREM , MVT::f80 , Expand);
380 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000381
Craig Topper909652f2011-10-14 03:21:46 +0000382 if (Subtarget->hasBMI()) {
383 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
384 } else {
385 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
386 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
387 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
388 if (Subtarget->is64Bit())
389 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
390 }
Craig Topper37f21672011-10-11 06:44:02 +0000391
392 if (Subtarget->hasLZCNT()) {
393 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
394 } else {
395 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
396 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
397 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
398 if (Subtarget->is64Bit())
399 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000400 }
401
Benjamin Kramer1292c222010-12-04 20:32:23 +0000402 if (Subtarget->hasPOPCNT()) {
403 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
404 } else {
405 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
406 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
407 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
408 if (Subtarget->is64Bit())
409 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
410 }
411
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
413 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000414
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000415 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000416 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000417 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000418 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000419 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
421 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
422 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
423 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
424 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000425 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
427 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
428 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
429 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000430 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000432 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000433 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000434 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000435
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000436 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
438 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
439 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
440 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000441 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000442 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
443 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000444 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000445 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000446 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
447 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
448 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
449 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000450 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000451 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000452 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
454 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
455 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000456 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000457 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
458 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
459 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000460 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000461
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000462 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000464
Eric Christopher9a9d2752010-07-22 02:48:34 +0000465 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000466 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000467
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000468 // On X86 and X86-64, atomic operations are lowered to locked instructions.
469 // Locked instructions, in turn, have implicit fence semantics (all memory
470 // operations are flushed before issuing the locked instruction, and they
471 // are not buffered), so we can fold away the common pattern of
472 // fence-atomic-fence.
473 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000474
Mon P Wang63307c32008-05-05 19:05:59 +0000475 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000476 for (unsigned i = 0, e = 4; i != e; ++i) {
477 MVT VT = IntVTs[i];
478 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
479 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000480 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000481 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000482
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000483 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000484 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
486 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
487 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
488 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
489 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
490 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
491 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000492 }
493
Eli Friedman43f51ae2011-08-26 21:21:21 +0000494 if (Subtarget->hasCmpxchg16b()) {
495 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
496 }
497
Evan Cheng3c992d22006-03-07 02:02:57 +0000498 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000499 if (!Subtarget->isTargetDarwin() &&
500 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000501 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000502 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000503 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000504
Owen Anderson825b72b2009-08-11 20:47:22 +0000505 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
506 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
507 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
508 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000509 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000510 setExceptionPointerRegister(X86::RAX);
511 setExceptionSelectorRegister(X86::RDX);
512 } else {
513 setExceptionPointerRegister(X86::EAX);
514 setExceptionSelectorRegister(X86::EDX);
515 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
517 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000518
Duncan Sands4a544a72011-09-06 13:37:06 +0000519 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
520 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000521
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000523
Nate Begemanacc398c2006-01-25 18:21:52 +0000524 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000525 setOperationAction(ISD::VASTART , MVT::Other, Custom);
526 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000527 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000528 setOperationAction(ISD::VAARG , MVT::Other, Custom);
529 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000530 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000531 setOperationAction(ISD::VAARG , MVT::Other, Expand);
532 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000533 }
Evan Chengae642192007-03-02 23:16:35 +0000534
Owen Anderson825b72b2009-08-11 20:47:22 +0000535 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
536 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000537
538 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
539 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
540 MVT::i64 : MVT::i32, Custom);
541 else if (EnableSegmentedStacks)
542 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
543 MVT::i64 : MVT::i32, Custom);
544 else
545 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
546 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000547
Evan Chengc7ce29b2009-02-13 22:36:38 +0000548 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000549 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000550 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
552 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000553
Evan Cheng223547a2006-01-31 22:28:30 +0000554 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000555 setOperationAction(ISD::FABS , MVT::f64, Custom);
556 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000557
558 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000559 setOperationAction(ISD::FNEG , MVT::f64, Custom);
560 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000561
Evan Cheng68c47cb2007-01-05 07:55:56 +0000562 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000563 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
564 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000565
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000566 // Lower this to FGETSIGNx86 plus an AND.
567 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
568 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
569
Evan Chengd25e9e82006-02-02 00:28:23 +0000570 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000571 setOperationAction(ISD::FSIN , MVT::f64, Expand);
572 setOperationAction(ISD::FCOS , MVT::f64, Expand);
573 setOperationAction(ISD::FSIN , MVT::f32, Expand);
574 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000575
Chris Lattnera54aa942006-01-29 06:26:08 +0000576 // Expand FP immediates into loads from the stack, except for the special
577 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000578 addLegalFPImmediate(APFloat(+0.0)); // xorpd
579 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000580 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000581 // Use SSE for f32, x87 for f64.
582 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000583 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
584 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000585
586 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000587 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000588
589 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000590 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000591
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000593
594 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000595 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
596 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000597
598 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000599 setOperationAction(ISD::FSIN , MVT::f32, Expand);
600 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000601
Nate Begemane1795842008-02-14 08:57:00 +0000602 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000603 addLegalFPImmediate(APFloat(+0.0f)); // xorps
604 addLegalFPImmediate(APFloat(+0.0)); // FLD0
605 addLegalFPImmediate(APFloat(+1.0)); // FLD1
606 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
607 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
608
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000609 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
611 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000612 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000613 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000614 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000615 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
617 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000618
Owen Anderson825b72b2009-08-11 20:47:22 +0000619 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
620 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
621 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
622 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000623
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000624 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000625 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
626 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000627 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000628 addLegalFPImmediate(APFloat(+0.0)); // FLD0
629 addLegalFPImmediate(APFloat(+1.0)); // FLD1
630 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
631 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000632 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
633 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
634 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
635 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000636 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000637
Cameron Zwarich33390842011-07-08 21:39:21 +0000638 // We don't support FMA.
639 setOperationAction(ISD::FMA, MVT::f64, Expand);
640 setOperationAction(ISD::FMA, MVT::f32, Expand);
641
Dale Johannesen59a58732007-08-05 18:49:15 +0000642 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000643 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
645 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
646 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000647 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000648 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000649 addLegalFPImmediate(TmpFlt); // FLD0
650 TmpFlt.changeSign();
651 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000652
653 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000654 APFloat TmpFlt2(+1.0);
655 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
656 &ignored);
657 addLegalFPImmediate(TmpFlt2); // FLD1
658 TmpFlt2.changeSign();
659 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
660 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000661
Evan Chengc7ce29b2009-02-13 22:36:38 +0000662 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
664 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000665 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000666
667 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000668 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000669
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000670 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000671 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
672 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
673 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000674
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 setOperationAction(ISD::FLOG, MVT::f80, Expand);
676 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
677 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
678 setOperationAction(ISD::FEXP, MVT::f80, Expand);
679 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000680
Mon P Wangf007a8b2008-11-06 05:31:54 +0000681 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000682 // (for widening) or expand (for scalarization). Then we will selectively
683 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
685 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
686 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000702 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
703 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000704 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000725 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000726 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000735 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000736 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000740 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000741 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
742 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
743 setTruncStoreAction((MVT::SimpleValueType)VT,
744 (MVT::SimpleValueType)InnerVT, Expand);
745 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
746 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
747 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000748 }
749
Evan Chengc7ce29b2009-02-13 22:36:38 +0000750 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
751 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000752 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000753 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000754 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000755 }
756
Dale Johannesen0488fb62010-09-30 23:57:10 +0000757 // MMX-sized vectors (other than x86mmx) are expected to be expanded
758 // into smaller operations.
759 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
760 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
761 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
762 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
763 setOperationAction(ISD::AND, MVT::v8i8, Expand);
764 setOperationAction(ISD::AND, MVT::v4i16, Expand);
765 setOperationAction(ISD::AND, MVT::v2i32, Expand);
766 setOperationAction(ISD::AND, MVT::v1i64, Expand);
767 setOperationAction(ISD::OR, MVT::v8i8, Expand);
768 setOperationAction(ISD::OR, MVT::v4i16, Expand);
769 setOperationAction(ISD::OR, MVT::v2i32, Expand);
770 setOperationAction(ISD::OR, MVT::v1i64, Expand);
771 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
772 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
773 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
774 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
775 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
776 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
777 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
778 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
779 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
780 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
781 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
782 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
783 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000784 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
785 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
786 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
787 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000788
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000789 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000791
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
793 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
794 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
795 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
796 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
797 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
798 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
799 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
800 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
801 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
802 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000803 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000804 }
805
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000806 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000808
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000809 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
810 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
812 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
813 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
814 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000815
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
817 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
818 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
819 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
820 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
821 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
822 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
823 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
824 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
825 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
826 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
827 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
828 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
829 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
830 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
831 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000832
Nadav Rotem354efd82011-09-18 14:57:03 +0000833 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000834 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
835 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
836 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000837
Owen Anderson825b72b2009-08-11 20:47:22 +0000838 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
839 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
840 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
841 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
842 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000843
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000844 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
845 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
846 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
847 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
848 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
849
Evan Cheng2c3ae372006-04-12 21:21:57 +0000850 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
852 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000853 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000854 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000855 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000856 // Do not attempt to custom lower non-128-bit vectors
857 if (!VT.is128BitVector())
858 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000859 setOperationAction(ISD::BUILD_VECTOR,
860 VT.getSimpleVT().SimpleTy, Custom);
861 setOperationAction(ISD::VECTOR_SHUFFLE,
862 VT.getSimpleVT().SimpleTy, Custom);
863 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
864 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000865 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000866
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
868 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
869 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
870 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
871 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
872 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000873
Nate Begemancdd1eec2008-02-12 22:51:28 +0000874 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
876 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000877 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000878
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000879 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
881 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000882 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000883
884 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000885 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000886 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000887
Owen Andersond6662ad2009-08-10 20:46:15 +0000888 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000890 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000892 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000894 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000896 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000898 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000899
Owen Anderson825b72b2009-08-11 20:47:22 +0000900 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000901
Evan Cheng2c3ae372006-04-12 21:21:57 +0000902 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
904 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
905 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
906 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000907
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
909 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000910 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000911
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000912 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000913 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
914 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
915 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
916 setOperationAction(ISD::FRINT, MVT::f32, Legal);
917 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
918 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
919 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
920 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
921 setOperationAction(ISD::FRINT, MVT::f64, Legal);
922 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
923
Nate Begeman14d12ca2008-02-11 04:19:36 +0000924 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000926
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000927 // Can turn SHL into an integer multiply.
928 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000929 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000930
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000931 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
932 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
933 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
934 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
935 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000936
Nate Begeman14d12ca2008-02-11 04:19:36 +0000937 // i8 and i16 vectors are custom , because the source register and source
938 // source memory operand types are not the same width. f32 vectors are
939 // custom since the immediate controlling the insert encodes additional
940 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
942 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
943 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
944 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000945
Owen Anderson825b72b2009-08-11 20:47:22 +0000946 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
947 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
948 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
949 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000950
951 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000952 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
953 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000954 }
955 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000956
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000957 if (Subtarget->hasXMMInt()) {
Nadav Rotem43012222011-05-11 08:12:09 +0000958 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
959 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
960 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000961 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000962
963 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
964 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
965 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
966
967 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
968 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000969 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000970 }
971
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000972 if (Subtarget->hasSSE42() || Subtarget->hasAVX())
Duncan Sands28b77e92011-09-06 19:07:46 +0000973 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000974
David Greene9b9838d2009-06-29 16:47:10 +0000975 if (!UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000976 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
977 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
978 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
979 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
980 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
981 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000982
Owen Anderson825b72b2009-08-11 20:47:22 +0000983 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000984 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
985 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000986
Owen Anderson825b72b2009-08-11 20:47:22 +0000987 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
988 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
989 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
990 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
991 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
992 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000993
Owen Anderson825b72b2009-08-11 20:47:22 +0000994 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
995 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
996 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
997 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
998 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
999 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001000
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001001 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1002 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001003 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001004
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00001005 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
1006 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
1007 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
1008 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
1009 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
1010 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
1011
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001012 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1013 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1014 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1015 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1016
1017 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1018 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1019 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1020 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1021
1022 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
1023 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
1024
Duncan Sands28b77e92011-09-06 19:07:46 +00001025 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1026 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1027 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1028 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001029
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001030 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1031 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1032 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1033
Craig Topperaaa643c2011-11-09 07:28:55 +00001034 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1035 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1036 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1037 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001038
Craig Topperaaa643c2011-11-09 07:28:55 +00001039 if (Subtarget->hasAVX2()) {
1040 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1041 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1042 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1043 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001044
Craig Topperaaa643c2011-11-09 07:28:55 +00001045 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1046 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1047 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1048 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001049
Craig Topperaaa643c2011-11-09 07:28:55 +00001050 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1051 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1052 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
1053 // Don't lower v32i8 because there is no 128-bit byte mul
1054 } else {
1055 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1056 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1057 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1058 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1059
1060 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1061 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1062 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1063 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1064
1065 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1066 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1067 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1068 // Don't lower v32i8 because there is no 128-bit byte mul
1069 }
Craig Topper13894fa2011-08-24 06:14:18 +00001070
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001071 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001072 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001073 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1074 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1075 EVT VT = SVT;
1076
1077 // Extract subvector is special because the value type
1078 // (result) is 128-bit but the source is 256-bit wide.
1079 if (VT.is128BitVector())
1080 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1081
1082 // Do not attempt to custom lower other non-256-bit vectors
1083 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001084 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001085
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001086 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1087 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1088 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1089 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001090 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001091 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001092 }
1093
David Greene54d8eba2011-01-27 22:38:56 +00001094 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001095 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1096 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1097 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001098
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001099 // Do not attempt to promote non-256-bit vectors
1100 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001101 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001102
1103 setOperationAction(ISD::AND, SVT, Promote);
1104 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1105 setOperationAction(ISD::OR, SVT, Promote);
1106 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1107 setOperationAction(ISD::XOR, SVT, Promote);
1108 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1109 setOperationAction(ISD::LOAD, SVT, Promote);
1110 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1111 setOperationAction(ISD::SELECT, SVT, Promote);
1112 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001113 }
David Greene9b9838d2009-06-29 16:47:10 +00001114 }
1115
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001116 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1117 // of this type with custom code.
1118 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1119 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1120 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1121 }
1122
Evan Cheng6be2c582006-04-05 23:38:46 +00001123 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001124 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001125
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001126
Eli Friedman962f5492010-06-02 19:35:46 +00001127 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1128 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001129 //
Eli Friedman962f5492010-06-02 19:35:46 +00001130 // FIXME: We really should do custom legalization for addition and
1131 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1132 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001133 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1134 // Add/Sub/Mul with overflow operations are custom lowered.
1135 MVT VT = IntVTs[i];
1136 setOperationAction(ISD::SADDO, VT, Custom);
1137 setOperationAction(ISD::UADDO, VT, Custom);
1138 setOperationAction(ISD::SSUBO, VT, Custom);
1139 setOperationAction(ISD::USUBO, VT, Custom);
1140 setOperationAction(ISD::SMULO, VT, Custom);
1141 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001142 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001143
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001144 // There are no 8-bit 3-address imul/mul instructions
1145 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1146 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001147
Evan Chengd54f2d52009-03-31 19:38:51 +00001148 if (!Subtarget->is64Bit()) {
1149 // These libcalls are not available in 32-bit.
1150 setLibcallName(RTLIB::SHL_I128, 0);
1151 setLibcallName(RTLIB::SRL_I128, 0);
1152 setLibcallName(RTLIB::SRA_I128, 0);
1153 }
1154
Evan Cheng206ee9d2006-07-07 08:33:52 +00001155 // We have target-specific dag combine patterns for the following nodes:
1156 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001157 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001158 setTargetDAGCombine(ISD::BUILD_VECTOR);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001159 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001160 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001161 setTargetDAGCombine(ISD::SHL);
1162 setTargetDAGCombine(ISD::SRA);
1163 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001164 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001165 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001166 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001167 setTargetDAGCombine(ISD::FADD);
1168 setTargetDAGCombine(ISD::FSUB);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001169 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001170 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001171 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001172 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001173 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001174 if (Subtarget->is64Bit())
1175 setTargetDAGCombine(ISD::MUL);
Craig Topperb4c94572011-10-21 06:55:01 +00001176 if (Subtarget->hasBMI())
1177 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001178
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001179 computeRegisterProperties();
1180
Evan Cheng05219282011-01-06 06:52:41 +00001181 // On Darwin, -Os means optimize for size without hurting performance,
1182 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001183 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001184 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001185 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001186 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1187 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1188 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001189 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001190 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001191
1192 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001193}
1194
Scott Michel5b8f82e2008-03-10 15:42:14 +00001195
Duncan Sands28b77e92011-09-06 19:07:46 +00001196EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1197 if (!VT.isVector()) return MVT::i8;
1198 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001199}
1200
1201
Evan Cheng29286502008-01-23 23:17:41 +00001202/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1203/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001204static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001205 if (MaxAlign == 16)
1206 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001207 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001208 if (VTy->getBitWidth() == 128)
1209 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001210 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001211 unsigned EltAlign = 0;
1212 getMaxByValAlign(ATy->getElementType(), EltAlign);
1213 if (EltAlign > MaxAlign)
1214 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001215 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001216 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1217 unsigned EltAlign = 0;
1218 getMaxByValAlign(STy->getElementType(i), EltAlign);
1219 if (EltAlign > MaxAlign)
1220 MaxAlign = EltAlign;
1221 if (MaxAlign == 16)
1222 break;
1223 }
1224 }
1225 return;
1226}
1227
1228/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1229/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001230/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1231/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001232unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001233 if (Subtarget->is64Bit()) {
1234 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001235 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001236 if (TyAlign > 8)
1237 return TyAlign;
1238 return 8;
1239 }
1240
Evan Cheng29286502008-01-23 23:17:41 +00001241 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001242 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001243 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001244 return Align;
1245}
Chris Lattner2b02a442007-02-25 08:29:00 +00001246
Evan Chengf0df0312008-05-15 08:39:06 +00001247/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001248/// and store operations as a result of memset, memcpy, and memmove
1249/// lowering. If DstAlign is zero that means it's safe to destination
1250/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1251/// means there isn't a need to check it against alignment requirement,
1252/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001253/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001254/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1255/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1256/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001257/// It returns EVT::Other if the type should be determined using generic
1258/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001259EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001260X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1261 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001262 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001263 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001264 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001265 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1266 // linux. This is because the stack realignment code can't handle certain
1267 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001268 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001269 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001270 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001271 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001272 (Subtarget->isUnalignedMemAccessFast() ||
1273 ((DstAlign == 0 || DstAlign >= 16) &&
1274 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001275 Subtarget->getStackAlignment() >= 16) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001276 if (Subtarget->hasAVX() &&
1277 Subtarget->getStackAlignment() >= 32)
1278 return MVT::v8f32;
1279 if (Subtarget->hasXMMInt())
Evan Cheng255f20f2010-04-01 06:04:33 +00001280 return MVT::v4i32;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001281 if (Subtarget->hasXMM())
Evan Cheng255f20f2010-04-01 06:04:33 +00001282 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001283 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001284 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001285 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001286 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001287 // Do not use f64 to lower memcpy if source is string constant. It's
1288 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001289 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001290 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001291 }
Evan Chengf0df0312008-05-15 08:39:06 +00001292 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001293 return MVT::i64;
1294 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001295}
1296
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001297/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1298/// current function. The returned value is a member of the
1299/// MachineJumpTableInfo::JTEntryKind enum.
1300unsigned X86TargetLowering::getJumpTableEncoding() const {
1301 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1302 // symbol.
1303 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1304 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001305 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001306
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001307 // Otherwise, use the normal jump table encoding heuristics.
1308 return TargetLowering::getJumpTableEncoding();
1309}
1310
Chris Lattnerc64daab2010-01-26 05:02:42 +00001311const MCExpr *
1312X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1313 const MachineBasicBlock *MBB,
1314 unsigned uid,MCContext &Ctx) const{
1315 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1316 Subtarget->isPICStyleGOT());
1317 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1318 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001319 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1320 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001321}
1322
Evan Chengcc415862007-11-09 01:32:10 +00001323/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1324/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001325SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001326 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001327 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001328 // This doesn't have DebugLoc associated with it, but is not really the
1329 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001330 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001331 return Table;
1332}
1333
Chris Lattner589c6f62010-01-26 06:28:43 +00001334/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1335/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1336/// MCExpr.
1337const MCExpr *X86TargetLowering::
1338getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1339 MCContext &Ctx) const {
1340 // X86-64 uses RIP relative addressing based on the jump table label.
1341 if (Subtarget->isPICStyleRIPRel())
1342 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1343
1344 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001345 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001346}
1347
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001348// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001349std::pair<const TargetRegisterClass*, uint8_t>
1350X86TargetLowering::findRepresentativeClass(EVT VT) const{
1351 const TargetRegisterClass *RRC = 0;
1352 uint8_t Cost = 1;
1353 switch (VT.getSimpleVT().SimpleTy) {
1354 default:
1355 return TargetLowering::findRepresentativeClass(VT);
1356 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1357 RRC = (Subtarget->is64Bit()
1358 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1359 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001360 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001361 RRC = X86::VR64RegisterClass;
1362 break;
1363 case MVT::f32: case MVT::f64:
1364 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1365 case MVT::v4f32: case MVT::v2f64:
1366 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1367 case MVT::v4f64:
1368 RRC = X86::VR128RegisterClass;
1369 break;
1370 }
1371 return std::make_pair(RRC, Cost);
1372}
1373
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001374bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1375 unsigned &Offset) const {
1376 if (!Subtarget->isTargetLinux())
1377 return false;
1378
1379 if (Subtarget->is64Bit()) {
1380 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1381 Offset = 0x28;
1382 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1383 AddressSpace = 256;
1384 else
1385 AddressSpace = 257;
1386 } else {
1387 // %gs:0x14 on i386
1388 Offset = 0x14;
1389 AddressSpace = 256;
1390 }
1391 return true;
1392}
1393
1394
Chris Lattner2b02a442007-02-25 08:29:00 +00001395//===----------------------------------------------------------------------===//
1396// Return Value Calling Convention Implementation
1397//===----------------------------------------------------------------------===//
1398
Chris Lattner59ed56b2007-02-28 04:55:35 +00001399#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001400
Michael J. Spencerec38de22010-10-10 22:04:20 +00001401bool
Eric Christopher471e4222011-06-08 23:55:35 +00001402X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1403 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001404 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001405 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001406 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001407 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001408 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001409 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001410}
1411
Dan Gohman98ca4f22009-08-05 01:29:28 +00001412SDValue
1413X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001414 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001415 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001416 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001417 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001418 MachineFunction &MF = DAG.getMachineFunction();
1419 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001420
Chris Lattner9774c912007-02-27 05:28:59 +00001421 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001422 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001423 RVLocs, *DAG.getContext());
1424 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001425
Evan Chengdcea1632010-02-04 02:40:39 +00001426 // Add the regs to the liveout set for the function.
1427 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1428 for (unsigned i = 0; i != RVLocs.size(); ++i)
1429 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1430 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001431
Dan Gohman475871a2008-07-27 21:46:04 +00001432 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001433
Dan Gohman475871a2008-07-27 21:46:04 +00001434 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001435 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1436 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001437 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1438 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001439
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001440 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001441 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1442 CCValAssign &VA = RVLocs[i];
1443 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001444 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001445 EVT ValVT = ValToCopy.getValueType();
1446
Dale Johannesenc4510512010-09-24 19:05:48 +00001447 // If this is x86-64, and we disabled SSE, we can't return FP values,
1448 // or SSE or MMX vectors.
1449 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1450 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001451 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001452 report_fatal_error("SSE register return with SSE disabled");
1453 }
1454 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1455 // llvm-gcc has never done it right and no one has noticed, so this
1456 // should be OK for now.
1457 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001458 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001459 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001460
Chris Lattner447ff682008-03-11 03:23:40 +00001461 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1462 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001463 if (VA.getLocReg() == X86::ST0 ||
1464 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001465 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1466 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001467 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001468 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001469 RetOps.push_back(ValToCopy);
1470 // Don't emit a copytoreg.
1471 continue;
1472 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001473
Evan Cheng242b38b2009-02-23 09:03:22 +00001474 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1475 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001476 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001477 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001478 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001479 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001480 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1481 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001482 // If we don't have SSE2 available, convert to v4f32 so the generated
1483 // register is legal.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001484 if (!Subtarget->hasXMMInt())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001485 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001486 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001487 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001488 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001489
Dale Johannesendd64c412009-02-04 00:33:20 +00001490 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001491 Flag = Chain.getValue(1);
1492 }
Dan Gohman61a92132008-04-21 23:59:07 +00001493
1494 // The x86-64 ABI for returning structs by value requires that we copy
1495 // the sret argument into %rax for the return. We saved the argument into
1496 // a virtual register in the entry block, so now we copy the value out
1497 // and into %rax.
1498 if (Subtarget->is64Bit() &&
1499 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1500 MachineFunction &MF = DAG.getMachineFunction();
1501 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1502 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001503 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001504 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001505 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001506
Dale Johannesendd64c412009-02-04 00:33:20 +00001507 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001508 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001509
1510 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001511 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001512 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001513
Chris Lattner447ff682008-03-11 03:23:40 +00001514 RetOps[0] = Chain; // Update chain.
1515
1516 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001517 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001518 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001519
1520 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001521 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001522}
1523
Evan Cheng3d2125c2010-11-30 23:55:39 +00001524bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1525 if (N->getNumValues() != 1)
1526 return false;
1527 if (!N->hasNUsesOfValue(1, 0))
1528 return false;
1529
1530 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001531 if (Copy->getOpcode() != ISD::CopyToReg &&
1532 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001533 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001534
1535 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001536 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001537 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001538 if (UI->getOpcode() != X86ISD::RET_FLAG)
1539 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001540 HasRet = true;
1541 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001542
Evan Cheng1bf891a2010-12-01 22:59:46 +00001543 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001544}
1545
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001546EVT
1547X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001548 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001549 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001550 // TODO: Is this also valid on 32-bit?
1551 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001552 ReturnMVT = MVT::i8;
1553 else
1554 ReturnMVT = MVT::i32;
1555
1556 EVT MinVT = getRegisterType(Context, ReturnMVT);
1557 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001558}
1559
Dan Gohman98ca4f22009-08-05 01:29:28 +00001560/// LowerCallResult - Lower the result values of a call into the
1561/// appropriate copies out of appropriate physical registers.
1562///
1563SDValue
1564X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001565 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001566 const SmallVectorImpl<ISD::InputArg> &Ins,
1567 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001568 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001569
Chris Lattnere32bbf62007-02-28 07:09:55 +00001570 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001571 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001572 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001573 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1574 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001575 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001576
Chris Lattner3085e152007-02-25 08:59:22 +00001577 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001578 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001579 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001580 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001581
Torok Edwin3f142c32009-02-01 18:15:56 +00001582 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001583 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001584 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001585 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001586 }
1587
Evan Cheng79fb3b42009-02-20 20:43:02 +00001588 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001589
1590 // If this is a call to a function that returns an fp value on the floating
1591 // point stack, we must guarantee the the value is popped from the stack, so
1592 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001593 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001594 // instead.
1595 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1596 // If we prefer to use the value in xmm registers, copy it out as f80 and
1597 // use a truncate to move it from fp stack reg to xmm reg.
1598 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001599 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001600 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1601 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001602 Val = Chain.getValue(0);
1603
1604 // Round the f80 to the right size, which also moves it to the appropriate
1605 // xmm register.
1606 if (CopyVT != VA.getValVT())
1607 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1608 // This truncation won't change the value.
1609 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001610 } else {
1611 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1612 CopyVT, InFlag).getValue(1);
1613 Val = Chain.getValue(0);
1614 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001615 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001616 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001617 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001618
Dan Gohman98ca4f22009-08-05 01:29:28 +00001619 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001620}
1621
1622
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001623//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001624// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001625//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001626// StdCall calling convention seems to be standard for many Windows' API
1627// routines and around. It differs from C calling convention just a little:
1628// callee should clean up the stack, not caller. Symbols should be also
1629// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001630// For info on fast calling convention see Fast Calling Convention (tail call)
1631// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001632
Dan Gohman98ca4f22009-08-05 01:29:28 +00001633/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001634/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001635static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1636 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001637 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001638
Dan Gohman98ca4f22009-08-05 01:29:28 +00001639 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001640}
1641
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001642/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001643/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001644static bool
1645ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1646 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001647 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001648
Dan Gohman98ca4f22009-08-05 01:29:28 +00001649 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001650}
1651
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001652/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1653/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001654/// the specific parameter attribute. The copy will be passed as a byval
1655/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001656static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001657CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001658 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1659 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001660 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001661
Dale Johannesendd64c412009-02-04 00:33:20 +00001662 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001663 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001664 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001665}
1666
Chris Lattner29689432010-03-11 00:22:57 +00001667/// IsTailCallConvention - Return true if the calling convention is one that
1668/// supports tail call optimization.
1669static bool IsTailCallConvention(CallingConv::ID CC) {
1670 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1671}
1672
Evan Cheng485fafc2011-03-21 01:19:09 +00001673bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1674 if (!CI->isTailCall())
1675 return false;
1676
1677 CallSite CS(CI);
1678 CallingConv::ID CalleeCC = CS.getCallingConv();
1679 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1680 return false;
1681
1682 return true;
1683}
1684
Evan Cheng0c439eb2010-01-27 00:07:07 +00001685/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1686/// a tailcall target by changing its ABI.
1687static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001688 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001689}
1690
Dan Gohman98ca4f22009-08-05 01:29:28 +00001691SDValue
1692X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001693 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001694 const SmallVectorImpl<ISD::InputArg> &Ins,
1695 DebugLoc dl, SelectionDAG &DAG,
1696 const CCValAssign &VA,
1697 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001698 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001699 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001700 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001701 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001702 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001703 EVT ValVT;
1704
1705 // If value is passed by pointer we have address passed instead of the value
1706 // itself.
1707 if (VA.getLocInfo() == CCValAssign::Indirect)
1708 ValVT = VA.getLocVT();
1709 else
1710 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001711
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001712 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001713 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001714 // In case of tail call optimization mark all arguments mutable. Since they
1715 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001716 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001717 unsigned Bytes = Flags.getByValSize();
1718 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1719 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001720 return DAG.getFrameIndex(FI, getPointerTy());
1721 } else {
1722 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001723 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001724 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1725 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001726 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001727 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001728 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001729}
1730
Dan Gohman475871a2008-07-27 21:46:04 +00001731SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001732X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001733 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001734 bool isVarArg,
1735 const SmallVectorImpl<ISD::InputArg> &Ins,
1736 DebugLoc dl,
1737 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001738 SmallVectorImpl<SDValue> &InVals)
1739 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001740 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001741 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001742
Gordon Henriksen86737662008-01-05 16:56:59 +00001743 const Function* Fn = MF.getFunction();
1744 if (Fn->hasExternalLinkage() &&
1745 Subtarget->isTargetCygMing() &&
1746 Fn->getName() == "main")
1747 FuncInfo->setForceFramePointer(true);
1748
Evan Cheng1bc78042006-04-26 01:20:17 +00001749 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001750 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001751 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001752
Chris Lattner29689432010-03-11 00:22:57 +00001753 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1754 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001755
Chris Lattner638402b2007-02-28 07:00:42 +00001756 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001757 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001758 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001759 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001760
1761 // Allocate shadow area for Win64
1762 if (IsWin64) {
1763 CCInfo.AllocateStack(32, 8);
1764 }
1765
Duncan Sands45907662010-10-31 13:21:44 +00001766 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001767
Chris Lattnerf39f7712007-02-28 05:46:49 +00001768 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001769 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001770 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1771 CCValAssign &VA = ArgLocs[i];
1772 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1773 // places.
1774 assert(VA.getValNo() != LastVal &&
1775 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001776 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001777 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001778
Chris Lattnerf39f7712007-02-28 05:46:49 +00001779 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001780 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001781 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001782 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001783 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001784 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001785 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001786 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001787 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001788 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001789 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001790 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1791 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001792 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001793 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001794 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001795 RC = X86::VR64RegisterClass;
1796 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001797 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001798
Devang Patel68e6bee2011-02-21 23:21:26 +00001799 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001800 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001801
Chris Lattnerf39f7712007-02-28 05:46:49 +00001802 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1803 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1804 // right size.
1805 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001806 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001807 DAG.getValueType(VA.getValVT()));
1808 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001809 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001810 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001811 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001812 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001813
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001814 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001815 // Handle MMX values passed in XMM regs.
1816 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001817 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1818 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001819 } else
1820 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001821 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001822 } else {
1823 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001824 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001825 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001826
1827 // If value is passed via pointer - do a load.
1828 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001829 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001830 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001831
Dan Gohman98ca4f22009-08-05 01:29:28 +00001832 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001833 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001834
Dan Gohman61a92132008-04-21 23:59:07 +00001835 // The x86-64 ABI for returning structs by value requires that we copy
1836 // the sret argument into %rax for the return. Save the argument into
1837 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001838 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001839 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1840 unsigned Reg = FuncInfo->getSRetReturnReg();
1841 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001842 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001843 FuncInfo->setSRetReturnReg(Reg);
1844 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001845 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001846 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001847 }
1848
Chris Lattnerf39f7712007-02-28 05:46:49 +00001849 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001850 // Align stack specially for tail calls.
1851 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001852 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001853
Evan Cheng1bc78042006-04-26 01:20:17 +00001854 // If the function takes variable number of arguments, make a frame index for
1855 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001856 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001857 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1858 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001859 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001860 }
1861 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001862 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1863
1864 // FIXME: We should really autogenerate these arrays
1865 static const unsigned GPR64ArgRegsWin64[] = {
1866 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001867 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001868 static const unsigned GPR64ArgRegs64Bit[] = {
1869 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1870 };
1871 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001872 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1873 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1874 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001875 const unsigned *GPR64ArgRegs;
1876 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001877
1878 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001879 // The XMM registers which might contain var arg parameters are shadowed
1880 // in their paired GPR. So we only need to save the GPR to their home
1881 // slots.
1882 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001883 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001884 } else {
1885 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1886 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001887
1888 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001889 }
1890 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1891 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001892
Devang Patel578efa92009-06-05 21:57:13 +00001893 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001894 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001895 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001896 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001897 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001898 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001899 // Kernel mode asks for SSE to be disabled, so don't push them
1900 // on the stack.
1901 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001902
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001903 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001904 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001905 // Get to the caller-allocated home save location. Add 8 to account
1906 // for the return address.
1907 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001908 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001909 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001910 // Fixup to set vararg frame on shadow area (4 x i64).
1911 if (NumIntRegs < 4)
1912 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001913 } else {
1914 // For X86-64, if there are vararg parameters that are passed via
1915 // registers, then we must store them to their spots on the stack so they
1916 // may be loaded by deferencing the result of va_next.
1917 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1918 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1919 FuncInfo->setRegSaveFrameIndex(
1920 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001921 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001922 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001923
Gordon Henriksen86737662008-01-05 16:56:59 +00001924 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001925 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001926 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1927 getPointerTy());
1928 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001929 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001930 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1931 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001932 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001933 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001934 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001935 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001936 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001937 MachinePointerInfo::getFixedStack(
1938 FuncInfo->getRegSaveFrameIndex(), Offset),
1939 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001940 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001941 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001942 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001943
Dan Gohmanface41a2009-08-16 21:24:25 +00001944 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1945 // Now store the XMM (fp + vector) parameter registers.
1946 SmallVector<SDValue, 11> SaveXMMOps;
1947 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001948
Devang Patel68e6bee2011-02-21 23:21:26 +00001949 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001950 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1951 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001952
Dan Gohman1e93df62010-04-17 14:41:14 +00001953 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1954 FuncInfo->getRegSaveFrameIndex()));
1955 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1956 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001957
Dan Gohmanface41a2009-08-16 21:24:25 +00001958 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001959 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001960 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001961 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1962 SaveXMMOps.push_back(Val);
1963 }
1964 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1965 MVT::Other,
1966 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001967 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001968
1969 if (!MemOps.empty())
1970 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1971 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001972 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001973 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001974
Gordon Henriksen86737662008-01-05 16:56:59 +00001975 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001976 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001977 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001978 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001979 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001980 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001981 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001982 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001983 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001984
Gordon Henriksen86737662008-01-05 16:56:59 +00001985 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001986 // RegSaveFrameIndex is X86-64 only.
1987 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001988 if (CallConv == CallingConv::X86_FastCall ||
1989 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001990 // fastcc functions can't have varargs.
1991 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001992 }
Evan Cheng25caf632006-05-23 21:06:34 +00001993
Rafael Espindola76927d752011-08-30 19:39:58 +00001994 FuncInfo->setArgumentStackSize(StackSize);
1995
Dan Gohman98ca4f22009-08-05 01:29:28 +00001996 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001997}
1998
Dan Gohman475871a2008-07-27 21:46:04 +00001999SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002000X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2001 SDValue StackPtr, SDValue Arg,
2002 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002003 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002004 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002005 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002006 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002007 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002008 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002009 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002010
2011 return DAG.getStore(Chain, dl, Arg, PtrOff,
2012 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002013 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002014}
2015
Bill Wendling64e87322009-01-16 19:25:27 +00002016/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002017/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002018SDValue
2019X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002020 SDValue &OutRetAddr, SDValue Chain,
2021 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002022 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002023 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002024 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002025 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002026
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002027 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002028 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002029 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002030 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002031}
2032
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002033/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002034/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002035static SDValue
2036EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002037 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002038 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002039 // Store the return address to the appropriate stack slot.
2040 if (!FPDiff) return Chain;
2041 // Calculate the new stack slot for the return address.
2042 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002043 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002044 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002045 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002046 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002047 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002048 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002049 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002050 return Chain;
2051}
2052
Dan Gohman98ca4f22009-08-05 01:29:28 +00002053SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002054X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002055 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002056 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002057 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002058 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002059 const SmallVectorImpl<ISD::InputArg> &Ins,
2060 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002061 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002062 MachineFunction &MF = DAG.getMachineFunction();
2063 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002064 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002065 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002066 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002067
Evan Cheng5f941932010-02-05 02:21:12 +00002068 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002069 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002070 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2071 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002072 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002073
2074 // Sibcalls are automatically detected tailcalls which do not require
2075 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002076 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002077 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002078
2079 if (isTailCall)
2080 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002081 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002082
Chris Lattner29689432010-03-11 00:22:57 +00002083 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2084 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002085
Chris Lattner638402b2007-02-28 07:00:42 +00002086 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002087 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002088 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002089 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002090
2091 // Allocate shadow area for Win64
2092 if (IsWin64) {
2093 CCInfo.AllocateStack(32, 8);
2094 }
2095
Duncan Sands45907662010-10-31 13:21:44 +00002096 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002097
Chris Lattner423c5f42007-02-28 05:31:48 +00002098 // Get a count of how many bytes are to be pushed on the stack.
2099 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002100 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002101 // This is a sibcall. The memory operands are available in caller's
2102 // own caller's stack.
2103 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002104 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002105 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002106
Gordon Henriksen86737662008-01-05 16:56:59 +00002107 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002108 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002109 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002110 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002111 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2112 FPDiff = NumBytesCallerPushed - NumBytes;
2113
2114 // Set the delta of movement of the returnaddr stackslot.
2115 // But only set if delta is greater than previous delta.
2116 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2117 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2118 }
2119
Evan Chengf22f9b32010-02-06 03:28:46 +00002120 if (!IsSibcall)
2121 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002122
Dan Gohman475871a2008-07-27 21:46:04 +00002123 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002124 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002125 if (isTailCall && FPDiff)
2126 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2127 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002128
Dan Gohman475871a2008-07-27 21:46:04 +00002129 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2130 SmallVector<SDValue, 8> MemOpChains;
2131 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002132
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002133 // Walk the register/memloc assignments, inserting copies/loads. In the case
2134 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002135 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2136 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002137 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002138 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002139 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002140 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002141
Chris Lattner423c5f42007-02-28 05:31:48 +00002142 // Promote the value if needed.
2143 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002144 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002145 case CCValAssign::Full: break;
2146 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002147 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002148 break;
2149 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002150 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002151 break;
2152 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002153 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2154 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002155 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002156 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2157 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002158 } else
2159 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2160 break;
2161 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002162 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002163 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002164 case CCValAssign::Indirect: {
2165 // Store the argument.
2166 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002167 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002168 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002169 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002170 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002171 Arg = SpillSlot;
2172 break;
2173 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002174 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002175
Chris Lattner423c5f42007-02-28 05:31:48 +00002176 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002177 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2178 if (isVarArg && IsWin64) {
2179 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2180 // shadow reg if callee is a varargs function.
2181 unsigned ShadowReg = 0;
2182 switch (VA.getLocReg()) {
2183 case X86::XMM0: ShadowReg = X86::RCX; break;
2184 case X86::XMM1: ShadowReg = X86::RDX; break;
2185 case X86::XMM2: ShadowReg = X86::R8; break;
2186 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002187 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002188 if (ShadowReg)
2189 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002190 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002191 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002192 assert(VA.isMemLoc());
2193 if (StackPtr.getNode() == 0)
2194 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2195 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2196 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002197 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002198 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002199
Evan Cheng32fe1032006-05-25 00:59:30 +00002200 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002201 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002202 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002203
Evan Cheng347d5f72006-04-28 21:29:37 +00002204 // Build a sequence of copy-to-reg nodes chained together with token chain
2205 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002206 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002207 // Tail call byval lowering might overwrite argument registers so in case of
2208 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002209 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002210 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002211 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002212 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002213 InFlag = Chain.getValue(1);
2214 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002215
Chris Lattner88e1fd52009-07-09 04:24:46 +00002216 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002217 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2218 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002219 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002220 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2221 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002222 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002223 InFlag);
2224 InFlag = Chain.getValue(1);
2225 } else {
2226 // If we are tail calling and generating PIC/GOT style code load the
2227 // address of the callee into ECX. The value in ecx is used as target of
2228 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2229 // for tail calls on PIC/GOT architectures. Normally we would just put the
2230 // address of GOT into ebx and then call target@PLT. But for tail calls
2231 // ebx would be restored (since ebx is callee saved) before jumping to the
2232 // target@PLT.
2233
2234 // Note: The actual moving to ECX is done further down.
2235 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2236 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2237 !G->getGlobal()->hasProtectedVisibility())
2238 Callee = LowerGlobalAddress(Callee, DAG);
2239 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002240 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002241 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002242 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002243
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002244 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002245 // From AMD64 ABI document:
2246 // For calls that may call functions that use varargs or stdargs
2247 // (prototype-less calls or calls to functions containing ellipsis (...) in
2248 // the declaration) %al is used as hidden argument to specify the number
2249 // of SSE registers used. The contents of %al do not need to match exactly
2250 // the number of registers, but must be an ubound on the number of SSE
2251 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002252
Gordon Henriksen86737662008-01-05 16:56:59 +00002253 // Count the number of XMM registers allocated.
2254 static const unsigned XMMArgRegs[] = {
2255 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2256 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2257 };
2258 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002259 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002260 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002261
Dale Johannesendd64c412009-02-04 00:33:20 +00002262 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002263 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002264 InFlag = Chain.getValue(1);
2265 }
2266
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002267
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002268 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002269 if (isTailCall) {
2270 // Force all the incoming stack arguments to be loaded from the stack
2271 // before any new outgoing arguments are stored to the stack, because the
2272 // outgoing stack slots may alias the incoming argument stack slots, and
2273 // the alias isn't otherwise explicit. This is slightly more conservative
2274 // than necessary, because it means that each store effectively depends
2275 // on every argument instead of just those arguments it would clobber.
2276 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2277
Dan Gohman475871a2008-07-27 21:46:04 +00002278 SmallVector<SDValue, 8> MemOpChains2;
2279 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002280 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002281 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002282 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002283 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002284 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2285 CCValAssign &VA = ArgLocs[i];
2286 if (VA.isRegLoc())
2287 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002288 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002289 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002290 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002291 // Create frame index.
2292 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002293 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002294 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002295 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002296
Duncan Sands276dcbd2008-03-21 09:14:45 +00002297 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002298 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002299 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002300 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002301 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002302 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002303 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002304
Dan Gohman98ca4f22009-08-05 01:29:28 +00002305 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2306 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002307 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002308 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002309 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002310 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002311 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002312 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002313 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002314 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002315 }
2316 }
2317
2318 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002319 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002320 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002321
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002322 // Copy arguments to their registers.
2323 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002324 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002325 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002326 InFlag = Chain.getValue(1);
2327 }
Dan Gohman475871a2008-07-27 21:46:04 +00002328 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002329
Gordon Henriksen86737662008-01-05 16:56:59 +00002330 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002331 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002332 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002333 }
2334
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002335 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2336 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2337 // In the 64-bit large code model, we have to make all calls
2338 // through a register, since the call instruction's 32-bit
2339 // pc-relative offset may not be large enough to hold the whole
2340 // address.
2341 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002342 // If the callee is a GlobalAddress node (quite common, every direct call
2343 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2344 // it.
2345
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002346 // We should use extra load for direct calls to dllimported functions in
2347 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002348 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002349 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002350 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002351 bool ExtraLoad = false;
2352 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002353
Chris Lattner48a7d022009-07-09 05:02:21 +00002354 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2355 // external symbols most go through the PLT in PIC mode. If the symbol
2356 // has hidden or protected visibility, or if it is static or local, then
2357 // we don't need to use the PLT - we can directly call it.
2358 if (Subtarget->isTargetELF() &&
2359 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002360 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002361 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002362 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002363 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002364 (!Subtarget->getTargetTriple().isMacOSX() ||
2365 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002366 // PC-relative references to external symbols should go through $stub,
2367 // unless we're building with the leopard linker or later, which
2368 // automatically synthesizes these stubs.
2369 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002370 } else if (Subtarget->isPICStyleRIPRel() &&
2371 isa<Function>(GV) &&
2372 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2373 // If the function is marked as non-lazy, generate an indirect call
2374 // which loads from the GOT directly. This avoids runtime overhead
2375 // at the cost of eager binding (and one extra byte of encoding).
2376 OpFlags = X86II::MO_GOTPCREL;
2377 WrapperKind = X86ISD::WrapperRIP;
2378 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002379 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002380
Devang Patel0d881da2010-07-06 22:08:15 +00002381 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002382 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002383
2384 // Add a wrapper if needed.
2385 if (WrapperKind != ISD::DELETED_NODE)
2386 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2387 // Add extra indirection if needed.
2388 if (ExtraLoad)
2389 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2390 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002391 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002392 }
Bill Wendling056292f2008-09-16 21:48:12 +00002393 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002394 unsigned char OpFlags = 0;
2395
Evan Cheng1bf891a2010-12-01 22:59:46 +00002396 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2397 // external symbols should go through the PLT.
2398 if (Subtarget->isTargetELF() &&
2399 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2400 OpFlags = X86II::MO_PLT;
2401 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002402 (!Subtarget->getTargetTriple().isMacOSX() ||
2403 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002404 // PC-relative references to external symbols should go through $stub,
2405 // unless we're building with the leopard linker or later, which
2406 // automatically synthesizes these stubs.
2407 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002408 }
Eric Christopherfd179292009-08-27 18:07:15 +00002409
Chris Lattner48a7d022009-07-09 05:02:21 +00002410 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2411 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002412 }
2413
Chris Lattnerd96d0722007-02-25 06:40:16 +00002414 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002415 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002416 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002417
Evan Chengf22f9b32010-02-06 03:28:46 +00002418 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002419 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2420 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002421 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002422 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002423
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002424 Ops.push_back(Chain);
2425 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002426
Dan Gohman98ca4f22009-08-05 01:29:28 +00002427 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002428 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002429
Gordon Henriksen86737662008-01-05 16:56:59 +00002430 // Add argument registers to the end of the list so that they are known live
2431 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002432 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2433 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2434 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002435
Evan Cheng586ccac2008-03-18 23:36:35 +00002436 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002437 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002438 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2439
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002440 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002441 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002442 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002443
Gabor Greifba36cb52008-08-28 21:40:38 +00002444 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002445 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002446
Dan Gohman98ca4f22009-08-05 01:29:28 +00002447 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002448 // We used to do:
2449 //// If this is the first return lowered for this function, add the regs
2450 //// to the liveout set for the function.
2451 // This isn't right, although it's probably harmless on x86; liveouts
2452 // should be computed from returns not tail calls. Consider a void
2453 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002454 return DAG.getNode(X86ISD::TC_RETURN, dl,
2455 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002456 }
2457
Dale Johannesenace16102009-02-03 19:33:06 +00002458 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002459 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002460
Chris Lattner2d297092006-05-23 18:50:38 +00002461 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002462 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002463 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002464 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002465 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002466 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002467 // pops the hidden struct pointer, so we have to push it back.
2468 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002469 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002470 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002471 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002472
Gordon Henriksenae636f82008-01-03 16:47:34 +00002473 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002474 if (!IsSibcall) {
2475 Chain = DAG.getCALLSEQ_END(Chain,
2476 DAG.getIntPtrConstant(NumBytes, true),
2477 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2478 true),
2479 InFlag);
2480 InFlag = Chain.getValue(1);
2481 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002482
Chris Lattner3085e152007-02-25 08:59:22 +00002483 // Handle result values, copying them out of physregs into vregs that we
2484 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002485 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2486 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002487}
2488
Evan Cheng25ab6902006-09-08 06:48:29 +00002489
2490//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002491// Fast Calling Convention (tail call) implementation
2492//===----------------------------------------------------------------------===//
2493
2494// Like std call, callee cleans arguments, convention except that ECX is
2495// reserved for storing the tail called function address. Only 2 registers are
2496// free for argument passing (inreg). Tail call optimization is performed
2497// provided:
2498// * tailcallopt is enabled
2499// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002500// On X86_64 architecture with GOT-style position independent code only local
2501// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002502// To keep the stack aligned according to platform abi the function
2503// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2504// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002505// If a tail called function callee has more arguments than the caller the
2506// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002507// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002508// original REtADDR, but before the saved framepointer or the spilled registers
2509// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2510// stack layout:
2511// arg1
2512// arg2
2513// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002514// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002515// move area ]
2516// (possible EBP)
2517// ESI
2518// EDI
2519// local1 ..
2520
2521/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2522/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002523unsigned
2524X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2525 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002526 MachineFunction &MF = DAG.getMachineFunction();
2527 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002528 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002529 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002530 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002531 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002532 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002533 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2534 // Number smaller than 12 so just add the difference.
2535 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2536 } else {
2537 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002538 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002539 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002540 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002541 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002542}
2543
Evan Cheng5f941932010-02-05 02:21:12 +00002544/// MatchingStackOffset - Return true if the given stack call argument is
2545/// already available in the same position (relatively) of the caller's
2546/// incoming argument stack.
2547static
2548bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2549 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2550 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002551 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2552 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002553 if (Arg.getOpcode() == ISD::CopyFromReg) {
2554 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002555 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002556 return false;
2557 MachineInstr *Def = MRI->getVRegDef(VR);
2558 if (!Def)
2559 return false;
2560 if (!Flags.isByVal()) {
2561 if (!TII->isLoadFromStackSlot(Def, FI))
2562 return false;
2563 } else {
2564 unsigned Opcode = Def->getOpcode();
2565 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2566 Def->getOperand(1).isFI()) {
2567 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002568 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002569 } else
2570 return false;
2571 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002572 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2573 if (Flags.isByVal())
2574 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002575 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002576 // define @foo(%struct.X* %A) {
2577 // tail call @bar(%struct.X* byval %A)
2578 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002579 return false;
2580 SDValue Ptr = Ld->getBasePtr();
2581 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2582 if (!FINode)
2583 return false;
2584 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002585 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002586 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002587 FI = FINode->getIndex();
2588 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002589 } else
2590 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002591
Evan Cheng4cae1332010-03-05 08:38:04 +00002592 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002593 if (!MFI->isFixedObjectIndex(FI))
2594 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002595 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002596}
2597
Dan Gohman98ca4f22009-08-05 01:29:28 +00002598/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2599/// for tail call optimization. Targets which want to do tail call
2600/// optimization should implement this function.
2601bool
2602X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002603 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002604 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002605 bool isCalleeStructRet,
2606 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002607 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002608 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002609 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002610 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002611 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002612 CalleeCC != CallingConv::C)
2613 return false;
2614
Evan Cheng7096ae42010-01-29 06:45:59 +00002615 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002616 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002617 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002618 CallingConv::ID CallerCC = CallerF->getCallingConv();
2619 bool CCMatch = CallerCC == CalleeCC;
2620
Dan Gohman1797ed52010-02-08 20:27:50 +00002621 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002622 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002623 return true;
2624 return false;
2625 }
2626
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002627 // Look for obvious safe cases to perform tail call optimization that do not
2628 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002629
Evan Cheng2c12cb42010-03-26 16:26:03 +00002630 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2631 // emit a special epilogue.
2632 if (RegInfo->needsStackRealignment(MF))
2633 return false;
2634
Evan Chenga375d472010-03-15 18:54:48 +00002635 // Also avoid sibcall optimization if either caller or callee uses struct
2636 // return semantics.
2637 if (isCalleeStructRet || isCallerStructRet)
2638 return false;
2639
Chad Rosier2416da32011-06-24 21:15:36 +00002640 // An stdcall caller is expected to clean up its arguments; the callee
2641 // isn't going to do that.
2642 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2643 return false;
2644
Chad Rosier871f6642011-05-18 19:59:50 +00002645 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002646 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002647 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002648
2649 // Optimizing for varargs on Win64 is unlikely to be safe without
2650 // additional testing.
2651 if (Subtarget->isTargetWin64())
2652 return false;
2653
Chad Rosier871f6642011-05-18 19:59:50 +00002654 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002655 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2656 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002657
Chad Rosier871f6642011-05-18 19:59:50 +00002658 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2659 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2660 if (!ArgLocs[i].isRegLoc())
2661 return false;
2662 }
2663
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002664 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2665 // Therefore if it's not used by the call it is not safe to optimize this into
2666 // a sibcall.
2667 bool Unused = false;
2668 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2669 if (!Ins[i].Used) {
2670 Unused = true;
2671 break;
2672 }
2673 }
2674 if (Unused) {
2675 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002676 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2677 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002678 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002679 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002680 CCValAssign &VA = RVLocs[i];
2681 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2682 return false;
2683 }
2684 }
2685
Evan Cheng13617962010-04-30 01:12:32 +00002686 // If the calling conventions do not match, then we'd better make sure the
2687 // results are returned in the same way as what the caller expects.
2688 if (!CCMatch) {
2689 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002690 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2691 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002692 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2693
2694 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002695 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2696 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002697 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2698
2699 if (RVLocs1.size() != RVLocs2.size())
2700 return false;
2701 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2702 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2703 return false;
2704 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2705 return false;
2706 if (RVLocs1[i].isRegLoc()) {
2707 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2708 return false;
2709 } else {
2710 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2711 return false;
2712 }
2713 }
2714 }
2715
Evan Chenga6bff982010-01-30 01:22:00 +00002716 // If the callee takes no arguments then go on to check the results of the
2717 // call.
2718 if (!Outs.empty()) {
2719 // Check if stack adjustment is needed. For now, do not do this if any
2720 // argument is passed on the stack.
2721 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002722 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2723 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002724
2725 // Allocate shadow area for Win64
2726 if (Subtarget->isTargetWin64()) {
2727 CCInfo.AllocateStack(32, 8);
2728 }
2729
Duncan Sands45907662010-10-31 13:21:44 +00002730 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002731 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002732 MachineFunction &MF = DAG.getMachineFunction();
2733 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2734 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002735
2736 // Check if the arguments are already laid out in the right way as
2737 // the caller's fixed stack objects.
2738 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002739 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2740 const X86InstrInfo *TII =
2741 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002742 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2743 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002744 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002745 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002746 if (VA.getLocInfo() == CCValAssign::Indirect)
2747 return false;
2748 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002749 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2750 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002751 return false;
2752 }
2753 }
2754 }
Evan Cheng9c044672010-05-29 01:35:22 +00002755
2756 // If the tailcall address may be in a register, then make sure it's
2757 // possible to register allocate for it. In 32-bit, the call address can
2758 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002759 // callee-saved registers are restored. These happen to be the same
2760 // registers used to pass 'inreg' arguments so watch out for those.
2761 if (!Subtarget->is64Bit() &&
2762 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002763 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002764 unsigned NumInRegs = 0;
2765 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2766 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002767 if (!VA.isRegLoc())
2768 continue;
2769 unsigned Reg = VA.getLocReg();
2770 switch (Reg) {
2771 default: break;
2772 case X86::EAX: case X86::EDX: case X86::ECX:
2773 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002774 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002775 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002776 }
2777 }
2778 }
Evan Chenga6bff982010-01-30 01:22:00 +00002779 }
Evan Chengb1712452010-01-27 06:25:16 +00002780
Evan Cheng86809cc2010-02-03 03:28:02 +00002781 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002782}
2783
Dan Gohman3df24e62008-09-03 23:12:08 +00002784FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002785X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2786 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002787}
2788
2789
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002790//===----------------------------------------------------------------------===//
2791// Other Lowering Hooks
2792//===----------------------------------------------------------------------===//
2793
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002794static bool MayFoldLoad(SDValue Op) {
2795 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2796}
2797
2798static bool MayFoldIntoStore(SDValue Op) {
2799 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2800}
2801
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002802static bool isTargetShuffle(unsigned Opcode) {
2803 switch(Opcode) {
2804 default: return false;
2805 case X86ISD::PSHUFD:
2806 case X86ISD::PSHUFHW:
2807 case X86ISD::PSHUFLW:
2808 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002809 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002810 case X86ISD::SHUFPS:
2811 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002812 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002813 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002814 case X86ISD::MOVLPS:
2815 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002816 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002817 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002818 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002819 case X86ISD::MOVSS:
2820 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002821 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002822 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002823 case X86ISD::VUNPCKLPSY:
2824 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002825 case X86ISD::PUNPCKLWD:
2826 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002827 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002828 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002829 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002830 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002831 case X86ISD::VUNPCKHPSY:
2832 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002833 case X86ISD::PUNPCKHWD:
2834 case X86ISD::PUNPCKHBW:
2835 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002836 case X86ISD::PUNPCKHQDQ:
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 Lopes53cae132011-08-12 21:48:26 +00002841 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002842 return true;
2843 }
2844 return false;
2845}
2846
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002847static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002848 SDValue V1, SelectionDAG &DAG) {
2849 switch(Opc) {
2850 default: llvm_unreachable("Unknown x86 shuffle node");
2851 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002852 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002853 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002854 return DAG.getNode(Opc, dl, VT, V1);
2855 }
2856
2857 return SDValue();
2858}
2859
2860static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002861 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002862 switch(Opc) {
2863 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002864 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002865 case X86ISD::PSHUFHW:
2866 case X86ISD::PSHUFLW:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002867 case X86ISD::VPERMILPS:
2868 case X86ISD::VPERMILPSY:
2869 case X86ISD::VPERMILPD:
2870 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002871 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2872 }
2873
2874 return SDValue();
2875}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002876
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002877static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2878 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2879 switch(Opc) {
2880 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002881 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002882 case X86ISD::SHUFPD:
2883 case X86ISD::SHUFPS:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002884 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002885 return DAG.getNode(Opc, dl, VT, V1, V2,
2886 DAG.getConstant(TargetMask, MVT::i8));
2887 }
2888 return SDValue();
2889}
2890
2891static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2892 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2893 switch(Opc) {
2894 default: llvm_unreachable("Unknown x86 shuffle node");
2895 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002896 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002897 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002898 case X86ISD::MOVLPS:
2899 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002900 case X86ISD::MOVSS:
2901 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002902 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002903 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002904 case X86ISD::VUNPCKLPSY:
2905 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002906 case X86ISD::PUNPCKLWD:
2907 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002908 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002909 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002910 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002911 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002912 case X86ISD::VUNPCKHPSY:
2913 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002914 case X86ISD::PUNPCKHWD:
2915 case X86ISD::PUNPCKHBW:
2916 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002917 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002918 return DAG.getNode(Opc, dl, VT, V1, V2);
2919 }
2920 return SDValue();
2921}
2922
Dan Gohmand858e902010-04-17 15:26:15 +00002923SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002924 MachineFunction &MF = DAG.getMachineFunction();
2925 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2926 int ReturnAddrIndex = FuncInfo->getRAIndex();
2927
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002928 if (ReturnAddrIndex == 0) {
2929 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002930 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002931 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002932 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002933 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002934 }
2935
Evan Cheng25ab6902006-09-08 06:48:29 +00002936 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002937}
2938
2939
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002940bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2941 bool hasSymbolicDisplacement) {
2942 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002943 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002944 return false;
2945
2946 // If we don't have a symbolic displacement - we don't have any extra
2947 // restrictions.
2948 if (!hasSymbolicDisplacement)
2949 return true;
2950
2951 // FIXME: Some tweaks might be needed for medium code model.
2952 if (M != CodeModel::Small && M != CodeModel::Kernel)
2953 return false;
2954
2955 // For small code model we assume that latest object is 16MB before end of 31
2956 // bits boundary. We may also accept pretty large negative constants knowing
2957 // that all objects are in the positive half of address space.
2958 if (M == CodeModel::Small && Offset < 16*1024*1024)
2959 return true;
2960
2961 // For kernel code model we know that all object resist in the negative half
2962 // of 32bits address space. We may not accept negative offsets, since they may
2963 // be just off and we may accept pretty large positive ones.
2964 if (M == CodeModel::Kernel && Offset > 0)
2965 return true;
2966
2967 return false;
2968}
2969
Evan Chengef41ff62011-06-23 17:54:54 +00002970/// isCalleePop - Determines whether the callee is required to pop its
2971/// own arguments. Callee pop is necessary to support tail calls.
2972bool X86::isCalleePop(CallingConv::ID CallingConv,
2973 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2974 if (IsVarArg)
2975 return false;
2976
2977 switch (CallingConv) {
2978 default:
2979 return false;
2980 case CallingConv::X86_StdCall:
2981 return !is64Bit;
2982 case CallingConv::X86_FastCall:
2983 return !is64Bit;
2984 case CallingConv::X86_ThisCall:
2985 return !is64Bit;
2986 case CallingConv::Fast:
2987 return TailCallOpt;
2988 case CallingConv::GHC:
2989 return TailCallOpt;
2990 }
2991}
2992
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002993/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2994/// specific condition code, returning the condition code and the LHS/RHS of the
2995/// comparison to make.
2996static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2997 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002998 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002999 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3000 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3001 // X > -1 -> X == 0, jump !sign.
3002 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003003 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003004 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
3005 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003006 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00003007 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003008 // X < 1 -> X <= 0
3009 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003010 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003011 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003012 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003013
Evan Chengd9558e02006-01-06 00:43:03 +00003014 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003015 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003016 case ISD::SETEQ: return X86::COND_E;
3017 case ISD::SETGT: return X86::COND_G;
3018 case ISD::SETGE: return X86::COND_GE;
3019 case ISD::SETLT: return X86::COND_L;
3020 case ISD::SETLE: return X86::COND_LE;
3021 case ISD::SETNE: return X86::COND_NE;
3022 case ISD::SETULT: return X86::COND_B;
3023 case ISD::SETUGT: return X86::COND_A;
3024 case ISD::SETULE: return X86::COND_BE;
3025 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003026 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003027 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003028
Chris Lattner4c78e022008-12-23 23:42:27 +00003029 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003030
Chris Lattner4c78e022008-12-23 23:42:27 +00003031 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003032 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3033 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003034 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3035 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003036 }
3037
Chris Lattner4c78e022008-12-23 23:42:27 +00003038 switch (SetCCOpcode) {
3039 default: break;
3040 case ISD::SETOLT:
3041 case ISD::SETOLE:
3042 case ISD::SETUGT:
3043 case ISD::SETUGE:
3044 std::swap(LHS, RHS);
3045 break;
3046 }
3047
3048 // On a floating point condition, the flags are set as follows:
3049 // ZF PF CF op
3050 // 0 | 0 | 0 | X > Y
3051 // 0 | 0 | 1 | X < Y
3052 // 1 | 0 | 0 | X == Y
3053 // 1 | 1 | 1 | unordered
3054 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003055 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003056 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003057 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003058 case ISD::SETOLT: // flipped
3059 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003060 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003061 case ISD::SETOLE: // flipped
3062 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003063 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003064 case ISD::SETUGT: // flipped
3065 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003066 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003067 case ISD::SETUGE: // flipped
3068 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003069 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003070 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003071 case ISD::SETNE: return X86::COND_NE;
3072 case ISD::SETUO: return X86::COND_P;
3073 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003074 case ISD::SETOEQ:
3075 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003076 }
Evan Chengd9558e02006-01-06 00:43:03 +00003077}
3078
Evan Cheng4a460802006-01-11 00:33:36 +00003079/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3080/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003081/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003082static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003083 switch (X86CC) {
3084 default:
3085 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003086 case X86::COND_B:
3087 case X86::COND_BE:
3088 case X86::COND_E:
3089 case X86::COND_P:
3090 case X86::COND_A:
3091 case X86::COND_AE:
3092 case X86::COND_NE:
3093 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003094 return true;
3095 }
3096}
3097
Evan Chengeb2f9692009-10-27 19:56:55 +00003098/// isFPImmLegal - Returns true if the target can instruction select the
3099/// specified FP immediate natively. If false, the legalizer will
3100/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003101bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003102 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3103 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3104 return true;
3105 }
3106 return false;
3107}
3108
Nate Begeman9008ca62009-04-27 18:41:29 +00003109/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3110/// the specified range (L, H].
3111static bool isUndefOrInRange(int Val, int Low, int Hi) {
3112 return (Val < 0) || (Val >= Low && Val < Hi);
3113}
3114
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003115/// isUndefOrInRange - Return true if every element in Mask, begining
3116/// from position Pos and ending in Pos+Size, falls within the specified
3117/// range (L, L+Pos]. or is undef.
3118static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3119 int Pos, int Size, int Low, int Hi) {
3120 for (int i = Pos, e = Pos+Size; i != e; ++i)
3121 if (!isUndefOrInRange(Mask[i], Low, Hi))
3122 return false;
3123 return true;
3124}
3125
Nate Begeman9008ca62009-04-27 18:41:29 +00003126/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3127/// specified value.
3128static bool isUndefOrEqual(int Val, int CmpVal) {
3129 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003130 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003131 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003132}
3133
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003134/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3135/// from position Pos and ending in Pos+Size, falls within the specified
3136/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003137static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3138 int Pos, int Size, int Low) {
3139 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3140 if (!isUndefOrEqual(Mask[i], Low))
3141 return false;
3142 return true;
3143}
3144
Nate Begeman9008ca62009-04-27 18:41:29 +00003145/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3146/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3147/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003148static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003149 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003150 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003151 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003152 return (Mask[0] < 2 && Mask[1] < 2);
3153 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003154}
3155
Nate Begeman9008ca62009-04-27 18:41:29 +00003156bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003157 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003158 N->getMask(M);
3159 return ::isPSHUFDMask(M, N->getValueType(0));
3160}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003161
Nate Begeman9008ca62009-04-27 18:41:29 +00003162/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3163/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003164static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003165 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003166 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003167
Nate Begeman9008ca62009-04-27 18:41:29 +00003168 // Lower quadword copied in order or undef.
3169 for (int i = 0; i != 4; ++i)
3170 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003171 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003172
Evan Cheng506d3df2006-03-29 23:07:14 +00003173 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003174 for (int i = 4; i != 8; ++i)
3175 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003176 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003177
Evan Cheng506d3df2006-03-29 23:07:14 +00003178 return true;
3179}
3180
Nate Begeman9008ca62009-04-27 18:41:29 +00003181bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003182 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003183 N->getMask(M);
3184 return ::isPSHUFHWMask(M, N->getValueType(0));
3185}
Evan Cheng506d3df2006-03-29 23:07:14 +00003186
Nate Begeman9008ca62009-04-27 18:41:29 +00003187/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3188/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003189static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003190 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003191 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003192
Rafael Espindola15684b22009-04-24 12:40:33 +00003193 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003194 for (int i = 4; i != 8; ++i)
3195 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003196 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003197
Rafael Espindola15684b22009-04-24 12:40:33 +00003198 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003199 for (int i = 0; i != 4; ++i)
3200 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003201 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003202
Rafael Espindola15684b22009-04-24 12:40:33 +00003203 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003204}
3205
Nate Begeman9008ca62009-04-27 18:41:29 +00003206bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003207 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003208 N->getMask(M);
3209 return ::isPSHUFLWMask(M, N->getValueType(0));
3210}
3211
Nate Begemana09008b2009-10-19 02:17:23 +00003212/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3213/// is suitable for input to PALIGNR.
3214static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003215 bool hasSSSE3OrAVX) {
Nate Begemana09008b2009-10-19 02:17:23 +00003216 int i, e = VT.getVectorNumElements();
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003217 if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
3218 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003219
Nate Begemana09008b2009-10-19 02:17:23 +00003220 // Do not handle v2i64 / v2f64 shuffles with palignr.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003221 if (e < 4 || !hasSSSE3OrAVX)
Nate Begemana09008b2009-10-19 02:17:23 +00003222 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003223
Nate Begemana09008b2009-10-19 02:17:23 +00003224 for (i = 0; i != e; ++i)
3225 if (Mask[i] >= 0)
3226 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003227
Nate Begemana09008b2009-10-19 02:17:23 +00003228 // All undef, not a palignr.
3229 if (i == e)
3230 return false;
3231
Eli Friedman63f8dde2011-07-25 21:36:45 +00003232 // Make sure we're shifting in the right direction.
3233 if (Mask[i] <= i)
3234 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003235
3236 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003237
Nate Begemana09008b2009-10-19 02:17:23 +00003238 // Check the rest of the elements to see if they are consecutive.
3239 for (++i; i != e; ++i) {
3240 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003241 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003242 return false;
3243 }
3244 return true;
3245}
3246
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003247/// isVSHUFPSYMask - Return true if the specified VECTOR_SHUFFLE operand
3248/// specifies a shuffle of elements that is suitable for input to 256-bit
3249/// VSHUFPSY.
3250static bool isVSHUFPSYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3251 const X86Subtarget *Subtarget) {
3252 int NumElems = VT.getVectorNumElements();
3253
3254 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3255 return false;
3256
3257 if (NumElems != 8)
3258 return false;
3259
3260 // VSHUFPSY divides the resulting vector into 4 chunks.
3261 // The sources are also splitted into 4 chunks, and each destination
3262 // chunk must come from a different source chunk.
3263 //
3264 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3265 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3266 //
3267 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3268 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3269 //
3270 int QuarterSize = NumElems/4;
3271 int HalfSize = QuarterSize*2;
3272 for (int i = 0; i < QuarterSize; ++i)
3273 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3274 return false;
3275 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3276 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3277 return false;
3278
3279 // The mask of the second half must be the same as the first but with
3280 // the appropriate offsets. This works in the same way as VPERMILPS
3281 // works with masks.
3282 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3283 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3284 return false;
3285 int FstHalfIdx = i-HalfSize;
3286 if (Mask[FstHalfIdx] < 0)
3287 continue;
3288 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3289 return false;
3290 }
3291 for (int i = QuarterSize*3; i < NumElems; ++i) {
3292 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3293 return false;
3294 int FstHalfIdx = i-HalfSize;
3295 if (Mask[FstHalfIdx] < 0)
3296 continue;
3297 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3298 return false;
3299
3300 }
3301
3302 return true;
3303}
3304
3305/// getShuffleVSHUFPSYImmediate - Return the appropriate immediate to shuffle
3306/// the specified VECTOR_MASK mask with VSHUFPSY instruction.
3307static unsigned getShuffleVSHUFPSYImmediate(SDNode *N) {
3308 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3309 EVT VT = SVOp->getValueType(0);
3310 int NumElems = VT.getVectorNumElements();
3311
3312 assert(NumElems == 8 && VT.getSizeInBits() == 256 &&
3313 "Only supports v8i32 and v8f32 types");
3314
3315 int HalfSize = NumElems/2;
3316 unsigned Mask = 0;
3317 for (int i = 0; i != NumElems ; ++i) {
3318 if (SVOp->getMaskElt(i) < 0)
3319 continue;
3320 // The mask of the first half must be equal to the second one.
3321 unsigned Shamt = (i%HalfSize)*2;
3322 unsigned Elt = SVOp->getMaskElt(i) % HalfSize;
3323 Mask |= Elt << Shamt;
3324 }
3325
3326 return Mask;
3327}
3328
3329/// isVSHUFPDYMask - Return true if the specified VECTOR_SHUFFLE operand
3330/// specifies a shuffle of elements that is suitable for input to 256-bit
3331/// VSHUFPDY. This shuffle doesn't have the same restriction as the PS
3332/// version and the mask of the second half isn't binded with the first
3333/// one.
3334static bool isVSHUFPDYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3335 const X86Subtarget *Subtarget) {
3336 int NumElems = VT.getVectorNumElements();
3337
3338 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3339 return false;
3340
3341 if (NumElems != 4)
3342 return false;
3343
3344 // VSHUFPSY divides the resulting vector into 4 chunks.
3345 // The sources are also splitted into 4 chunks, and each destination
3346 // chunk must come from a different source chunk.
3347 //
3348 // SRC1 => X3 X2 X1 X0
3349 // SRC2 => Y3 Y2 Y1 Y0
3350 //
3351 // DST => Y2..Y3, X2..X3, Y1..Y0, X1..X0
3352 //
3353 int QuarterSize = NumElems/4;
3354 int HalfSize = QuarterSize*2;
3355 for (int i = 0; i < QuarterSize; ++i)
3356 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3357 return false;
3358 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3359 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3360 return false;
3361 for (int i = QuarterSize*2; i < QuarterSize*3; ++i)
3362 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3363 return false;
3364 for (int i = QuarterSize*3; i < NumElems; ++i)
3365 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3366 return false;
3367
3368 return true;
3369}
3370
3371/// getShuffleVSHUFPDYImmediate - Return the appropriate immediate to shuffle
3372/// the specified VECTOR_MASK mask with VSHUFPDY instruction.
3373static unsigned getShuffleVSHUFPDYImmediate(SDNode *N) {
3374 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3375 EVT VT = SVOp->getValueType(0);
3376 int NumElems = VT.getVectorNumElements();
3377
3378 assert(NumElems == 4 && VT.getSizeInBits() == 256 &&
3379 "Only supports v4i64 and v4f64 types");
3380
3381 int HalfSize = NumElems/2;
3382 unsigned Mask = 0;
3383 for (int i = 0; i != NumElems ; ++i) {
3384 if (SVOp->getMaskElt(i) < 0)
3385 continue;
3386 int Elt = SVOp->getMaskElt(i) % HalfSize;
3387 Mask |= Elt << i;
3388 }
3389
3390 return Mask;
3391}
3392
Evan Cheng14aed5e2006-03-24 01:18:28 +00003393/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003394/// specifies a shuffle of elements that is suitable for input to 128-bit
3395/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003396static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003397 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003398
3399 if (VT.getSizeInBits() != 128)
3400 return false;
3401
Nate Begeman9008ca62009-04-27 18:41:29 +00003402 if (NumElems != 2 && NumElems != 4)
3403 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003404
Nate Begeman9008ca62009-04-27 18:41:29 +00003405 int Half = NumElems / 2;
3406 for (int i = 0; i < Half; ++i)
3407 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003408 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003409 for (int i = Half; i < NumElems; ++i)
3410 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003411 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003412
Evan Cheng14aed5e2006-03-24 01:18:28 +00003413 return true;
3414}
3415
Nate Begeman9008ca62009-04-27 18:41:29 +00003416bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3417 SmallVector<int, 8> M;
3418 N->getMask(M);
3419 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003420}
3421
Evan Cheng213d2cf2007-05-17 18:45:50 +00003422/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003423/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3424/// half elements to come from vector 1 (which would equal the dest.) and
3425/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003426static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003427 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003428
3429 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003430 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003431
Nate Begeman9008ca62009-04-27 18:41:29 +00003432 int Half = NumElems / 2;
3433 for (int i = 0; i < Half; ++i)
3434 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003435 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003436 for (int i = Half; i < NumElems; ++i)
3437 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003438 return false;
3439 return true;
3440}
3441
Nate Begeman9008ca62009-04-27 18:41:29 +00003442static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3443 SmallVector<int, 8> M;
3444 N->getMask(M);
3445 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003446}
3447
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003448/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3449/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003450bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003451 EVT VT = N->getValueType(0);
3452 unsigned NumElems = VT.getVectorNumElements();
3453
3454 if (VT.getSizeInBits() != 128)
3455 return false;
3456
3457 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003458 return false;
3459
Evan Cheng2064a2b2006-03-28 06:50:32 +00003460 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003461 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3462 isUndefOrEqual(N->getMaskElt(1), 7) &&
3463 isUndefOrEqual(N->getMaskElt(2), 2) &&
3464 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003465}
3466
Nate Begeman0b10b912009-11-07 23:17:15 +00003467/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3468/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3469/// <2, 3, 2, 3>
3470bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003471 EVT VT = N->getValueType(0);
3472 unsigned NumElems = VT.getVectorNumElements();
3473
3474 if (VT.getSizeInBits() != 128)
3475 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003476
Nate Begeman0b10b912009-11-07 23:17:15 +00003477 if (NumElems != 4)
3478 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003479
Nate Begeman0b10b912009-11-07 23:17:15 +00003480 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003481 isUndefOrEqual(N->getMaskElt(1), 3) &&
3482 isUndefOrEqual(N->getMaskElt(2), 2) &&
3483 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003484}
3485
Evan Cheng5ced1d82006-04-06 23:23:56 +00003486/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3487/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003488bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3489 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003490
Evan Cheng5ced1d82006-04-06 23:23:56 +00003491 if (NumElems != 2 && NumElems != 4)
3492 return false;
3493
Evan Chengc5cdff22006-04-07 21:53:05 +00003494 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003495 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003496 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003497
Evan Chengc5cdff22006-04-07 21:53:05 +00003498 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003499 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003500 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003501
3502 return true;
3503}
3504
Nate Begeman0b10b912009-11-07 23:17:15 +00003505/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3506/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3507bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003508 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003509
David Greenea20244d2011-03-02 17:23:43 +00003510 if ((NumElems != 2 && NumElems != 4)
3511 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003512 return false;
3513
Evan Chengc5cdff22006-04-07 21:53:05 +00003514 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003515 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003516 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003517
Nate Begeman9008ca62009-04-27 18:41:29 +00003518 for (unsigned i = 0; i < NumElems/2; ++i)
3519 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003520 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003521
3522 return true;
3523}
3524
Evan Cheng0038e592006-03-28 00:39:58 +00003525/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3526/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003527static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003528 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003529 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003530
3531 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3532 "Unsupported vector type for unpckh");
3533
3534 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng0038e592006-03-28 00:39:58 +00003535 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003536
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003537 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3538 // independently on 128-bit lanes.
3539 unsigned NumLanes = VT.getSizeInBits()/128;
3540 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003541
3542 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003543 unsigned End = NumLaneElts;
3544 for (unsigned s = 0; s < NumLanes; ++s) {
3545 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003546 i != End;
3547 i += 2, ++j) {
3548 int BitI = Mask[i];
3549 int BitI1 = Mask[i+1];
3550 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003551 return false;
David Greenea20244d2011-03-02 17:23:43 +00003552 if (V2IsSplat) {
3553 if (!isUndefOrEqual(BitI1, NumElts))
3554 return false;
3555 } else {
3556 if (!isUndefOrEqual(BitI1, j + NumElts))
3557 return false;
3558 }
Evan Cheng39623da2006-04-20 08:58:49 +00003559 }
David Greenea20244d2011-03-02 17:23:43 +00003560 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003561 Start += NumLaneElts;
3562 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003563 }
David Greenea20244d2011-03-02 17:23:43 +00003564
Evan Cheng0038e592006-03-28 00:39:58 +00003565 return true;
3566}
3567
Nate Begeman9008ca62009-04-27 18:41:29 +00003568bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3569 SmallVector<int, 8> M;
3570 N->getMask(M);
3571 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003572}
3573
Evan Cheng4fcb9222006-03-28 02:43:26 +00003574/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3575/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003576static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003577 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003578 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003579
3580 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3581 "Unsupported vector type for unpckh");
3582
3583 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003584 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003585
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003586 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3587 // independently on 128-bit lanes.
3588 unsigned NumLanes = VT.getSizeInBits()/128;
3589 unsigned NumLaneElts = NumElts/NumLanes;
3590
3591 unsigned Start = 0;
3592 unsigned End = NumLaneElts;
3593 for (unsigned l = 0; l != NumLanes; ++l) {
3594 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3595 i != End; i += 2, ++j) {
3596 int BitI = Mask[i];
3597 int BitI1 = Mask[i+1];
3598 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003599 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003600 if (V2IsSplat) {
3601 if (isUndefOrEqual(BitI1, NumElts))
3602 return false;
3603 } else {
3604 if (!isUndefOrEqual(BitI1, j+NumElts))
3605 return false;
3606 }
Evan Cheng39623da2006-04-20 08:58:49 +00003607 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003608 // Process the next 128 bits.
3609 Start += NumLaneElts;
3610 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003611 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003612 return true;
3613}
3614
Nate Begeman9008ca62009-04-27 18:41:29 +00003615bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3616 SmallVector<int, 8> M;
3617 N->getMask(M);
3618 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003619}
3620
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003621/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3622/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3623/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003624static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003625 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003626 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003627 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003628
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003629 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3630 // FIXME: Need a better way to get rid of this, there's no latency difference
3631 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3632 // the former later. We should also remove the "_undef" special mask.
3633 if (NumElems == 4 && VT.getSizeInBits() == 256)
3634 return false;
3635
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003636 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3637 // independently on 128-bit lanes.
3638 unsigned NumLanes = VT.getSizeInBits() / 128;
3639 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003640
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003641 for (unsigned s = 0; s < NumLanes; ++s) {
3642 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3643 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003644 i += 2, ++j) {
3645 int BitI = Mask[i];
3646 int BitI1 = Mask[i+1];
3647
3648 if (!isUndefOrEqual(BitI, j))
3649 return false;
3650 if (!isUndefOrEqual(BitI1, j))
3651 return false;
3652 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003653 }
David Greenea20244d2011-03-02 17:23:43 +00003654
Rafael Espindola15684b22009-04-24 12:40:33 +00003655 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003656}
3657
Nate Begeman9008ca62009-04-27 18:41:29 +00003658bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3659 SmallVector<int, 8> M;
3660 N->getMask(M);
3661 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3662}
3663
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003664/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3665/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3666/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003667static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003668 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003669 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3670 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003671
Nate Begeman9008ca62009-04-27 18:41:29 +00003672 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3673 int BitI = Mask[i];
3674 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003675 if (!isUndefOrEqual(BitI, j))
3676 return false;
3677 if (!isUndefOrEqual(BitI1, j))
3678 return false;
3679 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003680 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003681}
3682
Nate Begeman9008ca62009-04-27 18:41:29 +00003683bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3684 SmallVector<int, 8> M;
3685 N->getMask(M);
3686 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3687}
3688
Evan Cheng017dcc62006-04-21 01:05:10 +00003689/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3690/// specifies a shuffle of elements that is suitable for input to MOVSS,
3691/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003692static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003693 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003694 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003695
3696 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003697
Nate Begeman9008ca62009-04-27 18:41:29 +00003698 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003699 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003700
Nate Begeman9008ca62009-04-27 18:41:29 +00003701 for (int i = 1; i < NumElts; ++i)
3702 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003703 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003704
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003705 return true;
3706}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003707
Nate Begeman9008ca62009-04-27 18:41:29 +00003708bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3709 SmallVector<int, 8> M;
3710 N->getMask(M);
3711 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003712}
3713
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003714/// isVPERM2F128Mask - Match 256-bit shuffles where the elements are considered
3715/// as permutations between 128-bit chunks or halves. As an example: this
3716/// shuffle bellow:
3717/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3718/// The first half comes from the second half of V1 and the second half from the
3719/// the second half of V2.
3720static bool isVPERM2F128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3721 const X86Subtarget *Subtarget) {
3722 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3723 return false;
3724
3725 // The shuffle result is divided into half A and half B. In total the two
3726 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3727 // B must come from C, D, E or F.
3728 int HalfSize = VT.getVectorNumElements()/2;
3729 bool MatchA = false, MatchB = false;
3730
3731 // Check if A comes from one of C, D, E, F.
3732 for (int Half = 0; Half < 4; ++Half) {
3733 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3734 MatchA = true;
3735 break;
3736 }
3737 }
3738
3739 // Check if B comes from one of C, D, E, F.
3740 for (int Half = 0; Half < 4; ++Half) {
3741 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3742 MatchB = true;
3743 break;
3744 }
3745 }
3746
3747 return MatchA && MatchB;
3748}
3749
3750/// getShuffleVPERM2F128Immediate - Return the appropriate immediate to shuffle
3751/// the specified VECTOR_MASK mask with VPERM2F128 instructions.
3752static unsigned getShuffleVPERM2F128Immediate(SDNode *N) {
3753 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3754 EVT VT = SVOp->getValueType(0);
3755
3756 int HalfSize = VT.getVectorNumElements()/2;
3757
3758 int FstHalf = 0, SndHalf = 0;
3759 for (int i = 0; i < HalfSize; ++i) {
3760 if (SVOp->getMaskElt(i) > 0) {
3761 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3762 break;
3763 }
3764 }
3765 for (int i = HalfSize; i < HalfSize*2; ++i) {
3766 if (SVOp->getMaskElt(i) > 0) {
3767 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3768 break;
3769 }
3770 }
3771
3772 return (FstHalf | (SndHalf << 4));
3773}
3774
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003775/// isVPERMILPDMask - Return true if the specified VECTOR_SHUFFLE operand
3776/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3777/// Note that VPERMIL mask matching is different depending whether theunderlying
3778/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3779/// to the same elements of the low, but to the higher half of the source.
3780/// In VPERMILPD the two lanes could be shuffled independently of each other
3781/// with the same restriction that lanes can't be crossed.
3782static bool isVPERMILPDMask(const SmallVectorImpl<int> &Mask, EVT VT,
3783 const X86Subtarget *Subtarget) {
3784 int NumElts = VT.getVectorNumElements();
3785 int NumLanes = VT.getSizeInBits()/128;
3786
3787 if (!Subtarget->hasAVX())
3788 return false;
3789
Eli Friedmandca62d52011-10-10 22:28:47 +00003790 // Only match 256-bit with 64-bit types
3791 if (VT.getSizeInBits() != 256 || NumElts != 4)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003792 return false;
3793
3794 // The mask on the high lane is independent of the low. Both can match
3795 // any element in inside its own lane, but can't cross.
3796 int LaneSize = NumElts/NumLanes;
3797 for (int l = 0; l < NumLanes; ++l)
3798 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3799 int LaneStart = l*LaneSize;
3800 if (!isUndefOrInRange(Mask[i], LaneStart, LaneStart+LaneSize))
3801 return false;
3802 }
3803
3804 return true;
3805}
3806
3807/// isVPERMILPSMask - Return true if the specified VECTOR_SHUFFLE operand
3808/// specifies a shuffle of elements that is suitable for input to VPERMILPS*.
3809/// Note that VPERMIL mask matching is different depending whether theunderlying
3810/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3811/// to the same elements of the low, but to the higher half of the source.
3812/// In VPERMILPD the two lanes could be shuffled independently of each other
3813/// with the same restriction that lanes can't be crossed.
3814static bool isVPERMILPSMask(const SmallVectorImpl<int> &Mask, EVT VT,
3815 const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003816 unsigned NumElts = VT.getVectorNumElements();
3817 unsigned NumLanes = VT.getSizeInBits()/128;
3818
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003819 if (!Subtarget->hasAVX())
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003820 return false;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003821
Eli Friedmandca62d52011-10-10 22:28:47 +00003822 // Only match 256-bit with 32-bit types
3823 if (VT.getSizeInBits() != 256 || NumElts != 8)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003824 return false;
3825
3826 // The mask on the high lane should be the same as the low. Actually,
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003827 // they can differ if any of the corresponding index in a lane is undef
3828 // and the other stays in range.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003829 int LaneSize = NumElts/NumLanes;
3830 for (int i = 0; i < LaneSize; ++i) {
3831 int HighElt = i+LaneSize;
Bruno Cardoso Lopes155a92a2011-08-10 01:54:17 +00003832 bool HighValid = isUndefOrInRange(Mask[HighElt], LaneSize, NumElts);
3833 bool LowValid = isUndefOrInRange(Mask[i], 0, LaneSize);
3834
3835 if (!HighValid || !LowValid)
3836 return false;
3837 if (Mask[i] < 0 || Mask[HighElt] < 0)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003838 continue;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003839 if (Mask[HighElt]-Mask[i] != LaneSize)
3840 return false;
3841 }
3842
3843 return true;
3844}
3845
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003846/// getShuffleVPERMILPSImmediate - Return the appropriate immediate to shuffle
3847/// the specified VECTOR_MASK mask with VPERMILPS* instructions.
3848static unsigned getShuffleVPERMILPSImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003849 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3850 EVT VT = SVOp->getValueType(0);
3851
3852 int NumElts = VT.getVectorNumElements();
3853 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003854 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003855
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003856 // Although the mask is equal for both lanes do it twice to get the cases
3857 // where a mask will match because the same mask element is undef on the
3858 // first half but valid on the second. This would get pathological cases
3859 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003860 unsigned Mask = 0;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003861 for (int l = 0; l < NumLanes; ++l) {
3862 for (int i = 0; i < LaneSize; ++i) {
3863 int MaskElt = SVOp->getMaskElt(i+(l*LaneSize));
3864 if (MaskElt < 0)
3865 continue;
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003866 if (MaskElt >= LaneSize)
3867 MaskElt -= LaneSize;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003868 Mask |= MaskElt << (i*2);
3869 }
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003870 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003871
3872 return Mask;
3873}
3874
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003875/// getShuffleVPERMILPDImmediate - Return the appropriate immediate to shuffle
3876/// the specified VECTOR_MASK mask with VPERMILPD* instructions.
3877static unsigned getShuffleVPERMILPDImmediate(SDNode *N) {
3878 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3879 EVT VT = SVOp->getValueType(0);
3880
3881 int NumElts = VT.getVectorNumElements();
3882 int NumLanes = VT.getSizeInBits()/128;
3883
3884 unsigned Mask = 0;
3885 int LaneSize = NumElts/NumLanes;
3886 for (int l = 0; l < NumLanes; ++l)
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003887 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3888 int MaskElt = SVOp->getMaskElt(i);
3889 if (MaskElt < 0)
3890 continue;
3891 Mask |= (MaskElt-l*LaneSize) << i;
3892 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003893
3894 return Mask;
3895}
3896
Evan Cheng017dcc62006-04-21 01:05:10 +00003897/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3898/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003899/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003900static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003901 bool V2IsSplat = false, bool V2IsUndef = false) {
3902 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003903 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003904 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003905
Nate Begeman9008ca62009-04-27 18:41:29 +00003906 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003907 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003908
Nate Begeman9008ca62009-04-27 18:41:29 +00003909 for (int i = 1; i < NumOps; ++i)
3910 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3911 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3912 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003913 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003914
Evan Cheng39623da2006-04-20 08:58:49 +00003915 return true;
3916}
3917
Nate Begeman9008ca62009-04-27 18:41:29 +00003918static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003919 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003920 SmallVector<int, 8> M;
3921 N->getMask(M);
3922 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003923}
3924
Evan Chengd9539472006-04-14 21:59:03 +00003925/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3926/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003927/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3928bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3929 const X86Subtarget *Subtarget) {
3930 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003931 return false;
3932
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003933 // The second vector must be undef
3934 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3935 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003936
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003937 EVT VT = N->getValueType(0);
3938 unsigned NumElems = VT.getVectorNumElements();
3939
3940 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3941 (VT.getSizeInBits() == 256 && NumElems != 8))
3942 return false;
3943
3944 // "i+1" is the value the indexed mask element must have
3945 for (unsigned i = 0; i < NumElems; i += 2)
3946 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3947 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003948 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003949
3950 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003951}
3952
3953/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3954/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003955/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3956bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3957 const X86Subtarget *Subtarget) {
3958 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003959 return false;
3960
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003961 // The second vector must be undef
3962 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3963 return false;
3964
3965 EVT VT = N->getValueType(0);
3966 unsigned NumElems = VT.getVectorNumElements();
3967
3968 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3969 (VT.getSizeInBits() == 256 && NumElems != 8))
3970 return false;
3971
3972 // "i" is the value the indexed mask element must have
3973 for (unsigned i = 0; i < NumElems; i += 2)
3974 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3975 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003976 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003977
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003978 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003979}
3980
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003981/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3982/// specifies a shuffle of elements that is suitable for input to 256-bit
3983/// version of MOVDDUP.
3984static bool isMOVDDUPYMask(ShuffleVectorSDNode *N,
3985 const X86Subtarget *Subtarget) {
3986 EVT VT = N->getValueType(0);
3987 int NumElts = VT.getVectorNumElements();
3988 bool V2IsUndef = N->getOperand(1).getOpcode() == ISD::UNDEF;
3989
3990 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256 ||
3991 !V2IsUndef || NumElts != 4)
3992 return false;
3993
3994 for (int i = 0; i != NumElts/2; ++i)
3995 if (!isUndefOrEqual(N->getMaskElt(i), 0))
3996 return false;
3997 for (int i = NumElts/2; i != NumElts; ++i)
3998 if (!isUndefOrEqual(N->getMaskElt(i), NumElts/2))
3999 return false;
4000 return true;
4001}
4002
Evan Cheng0b457f02008-09-25 20:50:48 +00004003/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004004/// specifies a shuffle of elements that is suitable for input to 128-bit
4005/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00004006bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004007 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004008
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004009 if (VT.getSizeInBits() != 128)
4010 return false;
4011
4012 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004013 for (int i = 0; i < e; ++i)
4014 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004015 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004016 for (int i = 0; i < e; ++i)
4017 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004018 return false;
4019 return true;
4020}
4021
David Greenec38a03e2011-02-03 15:50:00 +00004022/// isVEXTRACTF128Index - Return true if the specified
4023/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4024/// suitable for input to VEXTRACTF128.
4025bool X86::isVEXTRACTF128Index(SDNode *N) {
4026 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4027 return false;
4028
4029 // The index should be aligned on a 128-bit boundary.
4030 uint64_t Index =
4031 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4032
4033 unsigned VL = N->getValueType(0).getVectorNumElements();
4034 unsigned VBits = N->getValueType(0).getSizeInBits();
4035 unsigned ElSize = VBits / VL;
4036 bool Result = (Index * ElSize) % 128 == 0;
4037
4038 return Result;
4039}
4040
David Greeneccacdc12011-02-04 16:08:29 +00004041/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4042/// operand specifies a subvector insert that is suitable for input to
4043/// VINSERTF128.
4044bool X86::isVINSERTF128Index(SDNode *N) {
4045 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4046 return false;
4047
4048 // The index should be aligned on a 128-bit boundary.
4049 uint64_t Index =
4050 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4051
4052 unsigned VL = N->getValueType(0).getVectorNumElements();
4053 unsigned VBits = N->getValueType(0).getSizeInBits();
4054 unsigned ElSize = VBits / VL;
4055 bool Result = (Index * ElSize) % 128 == 0;
4056
4057 return Result;
4058}
4059
Evan Cheng63d33002006-03-22 08:01:21 +00004060/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004061/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00004062unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004063 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4064 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4065
Evan Chengb9df0ca2006-03-22 02:53:00 +00004066 unsigned Shift = (NumOperands == 4) ? 2 : 1;
4067 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00004068 for (int i = 0; i < NumOperands; ++i) {
4069 int Val = SVOp->getMaskElt(NumOperands-i-1);
4070 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00004071 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00004072 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00004073 if (i != NumOperands - 1)
4074 Mask <<= Shift;
4075 }
Evan Cheng63d33002006-03-22 08:01:21 +00004076 return Mask;
4077}
4078
Evan Cheng506d3df2006-03-29 23:07:14 +00004079/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004080/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004081unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004082 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004083 unsigned Mask = 0;
4084 // 8 nodes, but we only care about the last 4.
4085 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004086 int Val = SVOp->getMaskElt(i);
4087 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004088 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00004089 if (i != 4)
4090 Mask <<= 2;
4091 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004092 return Mask;
4093}
4094
4095/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004096/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004097unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004098 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004099 unsigned Mask = 0;
4100 // 8 nodes, but we only care about the first 4.
4101 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004102 int Val = SVOp->getMaskElt(i);
4103 if (Val >= 0)
4104 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004105 if (i != 0)
4106 Mask <<= 2;
4107 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004108 return Mask;
4109}
4110
Nate Begemana09008b2009-10-19 02:17:23 +00004111/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4112/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4113unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4114 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4115 EVT VVT = N->getValueType(0);
4116 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4117 int Val = 0;
4118
4119 unsigned i, e;
4120 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4121 Val = SVOp->getMaskElt(i);
4122 if (Val >= 0)
4123 break;
4124 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004125 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004126 return (Val - i) * EltSize;
4127}
4128
David Greenec38a03e2011-02-03 15:50:00 +00004129/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4130/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4131/// instructions.
4132unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4133 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4134 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4135
4136 uint64_t Index =
4137 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4138
4139 EVT VecVT = N->getOperand(0).getValueType();
4140 EVT ElVT = VecVT.getVectorElementType();
4141
4142 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004143 return Index / NumElemsPerChunk;
4144}
4145
David Greeneccacdc12011-02-04 16:08:29 +00004146/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4147/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4148/// instructions.
4149unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4150 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4151 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4152
4153 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004154 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004155
4156 EVT VecVT = N->getValueType(0);
4157 EVT ElVT = VecVT.getVectorElementType();
4158
4159 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004160 return Index / NumElemsPerChunk;
4161}
4162
Evan Cheng37b73872009-07-30 08:33:02 +00004163/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4164/// constant +0.0.
4165bool X86::isZeroNode(SDValue Elt) {
4166 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004167 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004168 (isa<ConstantFPSDNode>(Elt) &&
4169 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4170}
4171
Nate Begeman9008ca62009-04-27 18:41:29 +00004172/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4173/// their permute mask.
4174static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4175 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004176 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004177 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004178 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004179
Nate Begeman5a5ca152009-04-29 05:20:52 +00004180 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004181 int idx = SVOp->getMaskElt(i);
4182 if (idx < 0)
4183 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004184 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004185 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004186 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004187 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004188 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004189 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4190 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004191}
4192
Evan Cheng779ccea2007-12-07 21:30:01 +00004193/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
4194/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00004195static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004196 unsigned NumElems = VT.getVectorNumElements();
4197 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004198 int idx = Mask[i];
4199 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004200 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004201 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004202 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004203 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004204 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004205 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004206}
4207
Evan Cheng533a0aa2006-04-19 20:35:22 +00004208/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4209/// match movhlps. The lower half elements should come from upper half of
4210/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004211/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004212static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004213 EVT VT = Op->getValueType(0);
4214 if (VT.getSizeInBits() != 128)
4215 return false;
4216 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004217 return false;
4218 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004219 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004220 return false;
4221 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004222 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004223 return false;
4224 return true;
4225}
4226
Evan Cheng5ced1d82006-04-06 23:23:56 +00004227/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004228/// is promoted to a vector. It also returns the LoadSDNode by reference if
4229/// required.
4230static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004231 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4232 return false;
4233 N = N->getOperand(0).getNode();
4234 if (!ISD::isNON_EXTLoad(N))
4235 return false;
4236 if (LD)
4237 *LD = cast<LoadSDNode>(N);
4238 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004239}
4240
Dan Gohman65fd6562011-11-03 21:49:52 +00004241// Test whether the given value is a vector value which will be legalized
4242// into a load.
4243static bool WillBeConstantPoolLoad(SDNode *N) {
4244 if (N->getOpcode() != ISD::BUILD_VECTOR)
4245 return false;
4246
4247 // Check for any non-constant elements.
4248 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4249 switch (N->getOperand(i).getNode()->getOpcode()) {
4250 case ISD::UNDEF:
4251 case ISD::ConstantFP:
4252 case ISD::Constant:
4253 break;
4254 default:
4255 return false;
4256 }
4257
4258 // Vectors of all-zeros and all-ones are materialized with special
4259 // instructions rather than being loaded.
4260 return !ISD::isBuildVectorAllZeros(N) &&
4261 !ISD::isBuildVectorAllOnes(N);
4262}
4263
Evan Cheng533a0aa2006-04-19 20:35:22 +00004264/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4265/// match movlp{s|d}. The lower half elements should come from lower half of
4266/// V1 (and in order), and the upper half elements should come from the upper
4267/// half of V2 (and in order). And since V1 will become the source of the
4268/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004269static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4270 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004271 EVT VT = Op->getValueType(0);
4272 if (VT.getSizeInBits() != 128)
4273 return false;
4274
Evan Cheng466685d2006-10-09 20:57:25 +00004275 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004276 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004277 // Is V2 is a vector load, don't do this transformation. We will try to use
4278 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004279 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004280 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004281
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004282 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004283
Evan Cheng533a0aa2006-04-19 20:35:22 +00004284 if (NumElems != 2 && NumElems != 4)
4285 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004286 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004287 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004288 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004289 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004290 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004291 return false;
4292 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004293}
4294
Evan Cheng39623da2006-04-20 08:58:49 +00004295/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4296/// all the same.
4297static bool isSplatVector(SDNode *N) {
4298 if (N->getOpcode() != ISD::BUILD_VECTOR)
4299 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004300
Dan Gohman475871a2008-07-27 21:46:04 +00004301 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004302 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4303 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004304 return false;
4305 return true;
4306}
4307
Evan Cheng213d2cf2007-05-17 18:45:50 +00004308/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004309/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004310/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004311static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004312 SDValue V1 = N->getOperand(0);
4313 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004314 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4315 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004316 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004317 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004318 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004319 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4320 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004321 if (Opc != ISD::BUILD_VECTOR ||
4322 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004323 return false;
4324 } else if (Idx >= 0) {
4325 unsigned Opc = V1.getOpcode();
4326 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4327 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004328 if (Opc != ISD::BUILD_VECTOR ||
4329 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004330 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004331 }
4332 }
4333 return true;
4334}
4335
4336/// getZeroVector - Returns a vector of specified type with all zero elements.
4337///
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004338static SDValue getZeroVector(EVT VT, bool HasXMMInt, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004339 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004340 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004341
Dale Johannesen0488fb62010-09-30 23:57:10 +00004342 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004343 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004344 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004345 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004346 if (HasXMMInt) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004347 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4348 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4349 } else { // SSE1
4350 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4351 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4352 }
4353 } else if (VT.getSizeInBits() == 256) { // AVX
4354 // 256-bit logic and arithmetic instructions in AVX are
4355 // all floating-point, no support for integer ops. Default
4356 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004357 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004358 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4359 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004360 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004361 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004362}
4363
Chris Lattner8a594482007-11-25 00:24:49 +00004364/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004365/// Always build ones vectors as <4 x i32>. For 256-bit types, use two
4366/// <4 x i32> inserted in a <8 x i32> appropriately. Then bitcast to their
4367/// original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00004368static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004369 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004370 assert((VT.is128BitVector() || VT.is256BitVector())
4371 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004372
Owen Anderson825b72b2009-08-11 20:47:22 +00004373 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004374 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4375 Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004376
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004377 if (VT.is256BitVector()) {
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004378 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4379 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4380 Vec = Insert128BitVector(InsV, Vec,
4381 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4382 }
4383
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004384 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004385}
4386
Evan Cheng39623da2006-04-20 08:58:49 +00004387/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4388/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004389static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004390 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004391 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004392
Evan Cheng39623da2006-04-20 08:58:49 +00004393 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004394 SmallVector<int, 8> MaskVec;
4395 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004396
Nate Begeman5a5ca152009-04-29 05:20:52 +00004397 for (unsigned i = 0; i != NumElems; ++i) {
4398 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004399 MaskVec[i] = NumElems;
4400 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004401 }
Evan Cheng39623da2006-04-20 08:58:49 +00004402 }
Evan Cheng39623da2006-04-20 08:58:49 +00004403 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004404 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4405 SVOp->getOperand(1), &MaskVec[0]);
4406 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004407}
4408
Evan Cheng017dcc62006-04-21 01:05:10 +00004409/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4410/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004411static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004412 SDValue V2) {
4413 unsigned NumElems = VT.getVectorNumElements();
4414 SmallVector<int, 8> Mask;
4415 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004416 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004417 Mask.push_back(i);
4418 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004419}
4420
Nate Begeman9008ca62009-04-27 18:41:29 +00004421/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004422static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004423 SDValue V2) {
4424 unsigned NumElems = VT.getVectorNumElements();
4425 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004426 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004427 Mask.push_back(i);
4428 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004429 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004430 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004431}
4432
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004433/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004434static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004435 SDValue V2) {
4436 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004437 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004438 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004439 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004440 Mask.push_back(i + Half);
4441 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004442 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004443 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004444}
4445
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004446// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004447// a generic shuffle instruction because the target has no such instructions.
4448// Generate shuffles which repeat i16 and i8 several times until they can be
4449// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004450static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004451 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004452 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004453 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004454
Nate Begeman9008ca62009-04-27 18:41:29 +00004455 while (NumElems > 4) {
4456 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004457 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004458 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004459 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004460 EltNo -= NumElems/2;
4461 }
4462 NumElems >>= 1;
4463 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004464 return V;
4465}
Eric Christopherfd179292009-08-27 18:07:15 +00004466
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004467/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4468static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4469 EVT VT = V.getValueType();
4470 DebugLoc dl = V.getDebugLoc();
4471 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4472 && "Vector size not supported");
4473
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004474 if (VT.getSizeInBits() == 128) {
4475 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004476 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004477 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4478 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004479 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004480 // To use VPERMILPS to splat scalars, the second half of indicies must
4481 // refer to the higher part, which is a duplication of the lower one,
4482 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004483 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4484 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004485
4486 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4487 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4488 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004489 }
4490
4491 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4492}
4493
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004494/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004495static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4496 EVT SrcVT = SV->getValueType(0);
4497 SDValue V1 = SV->getOperand(0);
4498 DebugLoc dl = SV->getDebugLoc();
4499
4500 int EltNo = SV->getSplatIndex();
4501 int NumElems = SrcVT.getVectorNumElements();
4502 unsigned Size = SrcVT.getSizeInBits();
4503
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004504 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4505 "Unknown how to promote splat for type");
4506
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004507 // Extract the 128-bit part containing the splat element and update
4508 // the splat element index when it refers to the higher register.
4509 if (Size == 256) {
4510 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4511 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4512 if (Idx > 0)
4513 EltNo -= NumElems/2;
4514 }
4515
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004516 // All i16 and i8 vector types can't be used directly by a generic shuffle
4517 // instruction because the target has no such instruction. Generate shuffles
4518 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004519 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004520 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004521 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004522 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004523
4524 // Recreate the 256-bit vector and place the same 128-bit vector
4525 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004526 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004527 if (Size == 256) {
4528 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4529 DAG.getConstant(0, MVT::i32), DAG, dl);
4530 V1 = Insert128BitVector(InsV, V1,
4531 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4532 }
4533
4534 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004535}
4536
Evan Chengba05f722006-04-21 23:03:30 +00004537/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004538/// vector of zero or undef vector. This produces a shuffle where the low
4539/// element of V2 is swizzled into the zero/undef vector, landing at element
4540/// 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 +00004541static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004542 bool isZero, bool HasXMMInt,
4543 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004544 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004545 SDValue V1 = isZero
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004546 ? getZeroVector(VT, HasXMMInt, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004547 unsigned NumElems = VT.getVectorNumElements();
4548 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004549 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004550 // If this is the insertion idx, put the low elt of V2 here.
4551 MaskVec.push_back(i == Idx ? NumElems : i);
4552 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004553}
4554
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004555/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4556/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004557static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4558 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004559 if (Depth == 6)
4560 return SDValue(); // Limit search depth.
4561
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004562 SDValue V = SDValue(N, 0);
4563 EVT VT = V.getValueType();
4564 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004565
4566 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4567 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4568 Index = SV->getMaskElt(Index);
4569
4570 if (Index < 0)
4571 return DAG.getUNDEF(VT.getVectorElementType());
4572
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004573 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004574 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004575 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004576 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004577
4578 // Recurse into target specific vector shuffles to find scalars.
4579 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004580 int NumElems = VT.getVectorNumElements();
4581 SmallVector<unsigned, 16> ShuffleMask;
4582 SDValue ImmN;
4583
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004584 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004585 case X86ISD::SHUFPS:
4586 case X86ISD::SHUFPD:
4587 ImmN = N->getOperand(N->getNumOperands()-1);
4588 DecodeSHUFPSMask(NumElems,
4589 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4590 ShuffleMask);
4591 break;
4592 case X86ISD::PUNPCKHBW:
4593 case X86ISD::PUNPCKHWD:
4594 case X86ISD::PUNPCKHDQ:
4595 case X86ISD::PUNPCKHQDQ:
4596 DecodePUNPCKHMask(NumElems, ShuffleMask);
4597 break;
4598 case X86ISD::UNPCKHPS:
4599 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00004600 case X86ISD::VUNPCKHPSY:
4601 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004602 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4603 break;
4604 case X86ISD::PUNPCKLBW:
4605 case X86ISD::PUNPCKLWD:
4606 case X86ISD::PUNPCKLDQ:
4607 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004608 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004609 break;
4610 case X86ISD::UNPCKLPS:
4611 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004612 case X86ISD::VUNPCKLPSY:
4613 case X86ISD::VUNPCKLPDY:
4614 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004615 break;
4616 case X86ISD::MOVHLPS:
4617 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4618 break;
4619 case X86ISD::MOVLHPS:
4620 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4621 break;
4622 case X86ISD::PSHUFD:
4623 ImmN = N->getOperand(N->getNumOperands()-1);
4624 DecodePSHUFMask(NumElems,
4625 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4626 ShuffleMask);
4627 break;
4628 case X86ISD::PSHUFHW:
4629 ImmN = N->getOperand(N->getNumOperands()-1);
4630 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4631 ShuffleMask);
4632 break;
4633 case X86ISD::PSHUFLW:
4634 ImmN = N->getOperand(N->getNumOperands()-1);
4635 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4636 ShuffleMask);
4637 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004638 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004639 case X86ISD::MOVSD: {
4640 // The index 0 always comes from the first element of the second source,
4641 // this is why MOVSS and MOVSD are used in the first place. The other
4642 // elements come from the other positions of the first source vector.
4643 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004644 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4645 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004646 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004647 case X86ISD::VPERMILPS:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004648 ImmN = N->getOperand(N->getNumOperands()-1);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004649 DecodeVPERMILPSMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004650 ShuffleMask);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004651 break;
4652 case X86ISD::VPERMILPSY:
4653 ImmN = N->getOperand(N->getNumOperands()-1);
4654 DecodeVPERMILPSMask(8, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4655 ShuffleMask);
4656 break;
4657 case X86ISD::VPERMILPD:
4658 ImmN = N->getOperand(N->getNumOperands()-1);
4659 DecodeVPERMILPDMask(2, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4660 ShuffleMask);
4661 break;
4662 case X86ISD::VPERMILPDY:
4663 ImmN = N->getOperand(N->getNumOperands()-1);
4664 DecodeVPERMILPDMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4665 ShuffleMask);
4666 break;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004667 case X86ISD::VPERM2F128:
4668 ImmN = N->getOperand(N->getNumOperands()-1);
4669 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4670 ShuffleMask);
4671 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004672 case X86ISD::MOVDDUP:
4673 case X86ISD::MOVLHPD:
4674 case X86ISD::MOVLPD:
4675 case X86ISD::MOVLPS:
4676 case X86ISD::MOVSHDUP:
4677 case X86ISD::MOVSLDUP:
4678 case X86ISD::PALIGN:
4679 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004680 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004681 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004682 return SDValue();
4683 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004684
4685 Index = ShuffleMask[Index];
4686 if (Index < 0)
4687 return DAG.getUNDEF(VT.getVectorElementType());
4688
4689 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4690 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4691 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004692 }
4693
4694 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004695 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004696 V = V.getOperand(0);
4697 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004698 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004699
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004700 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004701 return SDValue();
4702 }
4703
4704 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4705 return (Index == 0) ? V.getOperand(0)
4706 : DAG.getUNDEF(VT.getVectorElementType());
4707
4708 if (V.getOpcode() == ISD::BUILD_VECTOR)
4709 return V.getOperand(Index);
4710
4711 return SDValue();
4712}
4713
4714/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4715/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004716/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004717static
4718unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4719 bool ZerosFromLeft, SelectionDAG &DAG) {
4720 int i = 0;
4721
4722 while (i < NumElems) {
4723 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004724 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004725 if (!(Elt.getNode() &&
4726 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4727 break;
4728 ++i;
4729 }
4730
4731 return i;
4732}
4733
4734/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4735/// MaskE correspond consecutively to elements from one of the vector operands,
4736/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4737static
4738bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4739 int OpIdx, int NumElems, unsigned &OpNum) {
4740 bool SeenV1 = false;
4741 bool SeenV2 = false;
4742
4743 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4744 int Idx = SVOp->getMaskElt(i);
4745 // Ignore undef indicies
4746 if (Idx < 0)
4747 continue;
4748
4749 if (Idx < NumElems)
4750 SeenV1 = true;
4751 else
4752 SeenV2 = true;
4753
4754 // Only accept consecutive elements from the same vector
4755 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4756 return false;
4757 }
4758
4759 OpNum = SeenV1 ? 0 : 1;
4760 return true;
4761}
4762
4763/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4764/// logical left shift of a vector.
4765static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4766 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4767 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4768 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4769 false /* check zeros from right */, DAG);
4770 unsigned OpSrc;
4771
4772 if (!NumZeros)
4773 return false;
4774
4775 // Considering the elements in the mask that are not consecutive zeros,
4776 // check if they consecutively come from only one of the source vectors.
4777 //
4778 // V1 = {X, A, B, C} 0
4779 // \ \ \ /
4780 // vector_shuffle V1, V2 <1, 2, 3, X>
4781 //
4782 if (!isShuffleMaskConsecutive(SVOp,
4783 0, // Mask Start Index
4784 NumElems-NumZeros-1, // Mask End Index
4785 NumZeros, // Where to start looking in the src vector
4786 NumElems, // Number of elements in vector
4787 OpSrc)) // Which source operand ?
4788 return false;
4789
4790 isLeft = false;
4791 ShAmt = NumZeros;
4792 ShVal = SVOp->getOperand(OpSrc);
4793 return true;
4794}
4795
4796/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4797/// logical left shift of a vector.
4798static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4799 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4800 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4801 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4802 true /* check zeros from left */, DAG);
4803 unsigned OpSrc;
4804
4805 if (!NumZeros)
4806 return false;
4807
4808 // Considering the elements in the mask that are not consecutive zeros,
4809 // check if they consecutively come from only one of the source vectors.
4810 //
4811 // 0 { A, B, X, X } = V2
4812 // / \ / /
4813 // vector_shuffle V1, V2 <X, X, 4, 5>
4814 //
4815 if (!isShuffleMaskConsecutive(SVOp,
4816 NumZeros, // Mask Start Index
4817 NumElems-1, // Mask End Index
4818 0, // Where to start looking in the src vector
4819 NumElems, // Number of elements in vector
4820 OpSrc)) // Which source operand ?
4821 return false;
4822
4823 isLeft = true;
4824 ShAmt = NumZeros;
4825 ShVal = SVOp->getOperand(OpSrc);
4826 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004827}
4828
4829/// isVectorShift - Returns true if the shuffle can be implemented as a
4830/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004831static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004832 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004833 // Although the logic below support any bitwidth size, there are no
4834 // shift instructions which handle more than 128-bit vectors.
4835 if (SVOp->getValueType(0).getSizeInBits() > 128)
4836 return false;
4837
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004838 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4839 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4840 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004841
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004842 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004843}
4844
Evan Chengc78d3b42006-04-24 18:01:45 +00004845/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4846///
Dan Gohman475871a2008-07-27 21:46:04 +00004847static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004848 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004849 SelectionDAG &DAG,
4850 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004851 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004852 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004853
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004854 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004855 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004856 bool First = true;
4857 for (unsigned i = 0; i < 16; ++i) {
4858 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4859 if (ThisIsNonZero && First) {
4860 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004861 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004862 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004863 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004864 First = false;
4865 }
4866
4867 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004868 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004869 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4870 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004871 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004872 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004873 }
4874 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004875 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4876 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4877 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004878 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004879 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004880 } else
4881 ThisElt = LastElt;
4882
Gabor Greifba36cb52008-08-28 21:40:38 +00004883 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004884 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004885 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004886 }
4887 }
4888
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004889 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004890}
4891
Bill Wendlinga348c562007-03-22 18:42:45 +00004892/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004893///
Dan Gohman475871a2008-07-27 21:46:04 +00004894static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004895 unsigned NumNonZero, unsigned NumZero,
4896 SelectionDAG &DAG,
4897 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004898 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004899 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004900
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004901 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004902 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004903 bool First = true;
4904 for (unsigned i = 0; i < 8; ++i) {
4905 bool isNonZero = (NonZeros & (1 << i)) != 0;
4906 if (isNonZero) {
4907 if (First) {
4908 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004909 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004910 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004911 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004912 First = false;
4913 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004914 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004915 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004916 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004917 }
4918 }
4919
4920 return V;
4921}
4922
Evan Chengf26ffe92008-05-29 08:22:04 +00004923/// getVShift - Return a vector logical shift node.
4924///
Owen Andersone50ed302009-08-10 22:56:29 +00004925static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004926 unsigned NumBits, SelectionDAG &DAG,
4927 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004928 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004929 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004930 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004931 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4932 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004933 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004934 DAG.getConstant(NumBits,
4935 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004936}
4937
Dan Gohman475871a2008-07-27 21:46:04 +00004938SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004939X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004940 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004941
Evan Chengc3630942009-12-09 21:00:30 +00004942 // Check if the scalar load can be widened into a vector load. And if
4943 // the address is "base + cst" see if the cst can be "absorbed" into
4944 // the shuffle mask.
4945 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4946 SDValue Ptr = LD->getBasePtr();
4947 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4948 return SDValue();
4949 EVT PVT = LD->getValueType(0);
4950 if (PVT != MVT::i32 && PVT != MVT::f32)
4951 return SDValue();
4952
4953 int FI = -1;
4954 int64_t Offset = 0;
4955 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4956 FI = FINode->getIndex();
4957 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004958 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004959 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4960 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4961 Offset = Ptr.getConstantOperandVal(1);
4962 Ptr = Ptr.getOperand(0);
4963 } else {
4964 return SDValue();
4965 }
4966
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004967 // FIXME: 256-bit vector instructions don't require a strict alignment,
4968 // improve this code to support it better.
4969 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004970 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004971 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004972 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004973 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004974 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004975 // Can't change the alignment. FIXME: It's possible to compute
4976 // the exact stack offset and reference FI + adjust offset instead.
4977 // If someone *really* cares about this. That's the way to implement it.
4978 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004979 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004980 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004981 }
4982 }
4983
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004984 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004985 // Ptr + (Offset & ~15).
4986 if (Offset < 0)
4987 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004988 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004989 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004990 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004991 if (StartOffset)
4992 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4993 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4994
4995 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004996 int NumElems = VT.getVectorNumElements();
4997
4998 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4999 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
5000 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00005001 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005002 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005003
5004 // Canonicalize it to a v4i32 or v8i32 shuffle.
5005 SmallVector<int, 8> Mask;
5006 for (int i = 0; i < NumElems; ++i)
5007 Mask.push_back(EltNo);
5008
5009 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
5010 return DAG.getNode(ISD::BITCAST, dl, NVT,
5011 DAG.getVectorShuffle(CanonVT, dl, V1,
5012 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00005013 }
5014
5015 return SDValue();
5016}
5017
Michael J. Spencerec38de22010-10-10 22:04:20 +00005018/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5019/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005020/// load which has the same value as a build_vector whose operands are 'elts'.
5021///
5022/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005023///
Nate Begeman1449f292010-03-24 22:19:06 +00005024/// FIXME: we'd also like to handle the case where the last elements are zero
5025/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5026/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005027static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005028 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005029 EVT EltVT = VT.getVectorElementType();
5030 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005031
Nate Begemanfdea31a2010-03-24 20:49:50 +00005032 LoadSDNode *LDBase = NULL;
5033 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005034
Nate Begeman1449f292010-03-24 22:19:06 +00005035 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005036 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005037 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005038 for (unsigned i = 0; i < NumElems; ++i) {
5039 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005040
Nate Begemanfdea31a2010-03-24 20:49:50 +00005041 if (!Elt.getNode() ||
5042 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5043 return SDValue();
5044 if (!LDBase) {
5045 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5046 return SDValue();
5047 LDBase = cast<LoadSDNode>(Elt.getNode());
5048 LastLoadedElt = i;
5049 continue;
5050 }
5051 if (Elt.getOpcode() == ISD::UNDEF)
5052 continue;
5053
5054 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5055 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5056 return SDValue();
5057 LastLoadedElt = i;
5058 }
Nate Begeman1449f292010-03-24 22:19:06 +00005059
5060 // If we have found an entire vector of loads and undefs, then return a large
5061 // load of the entire vector width starting at the base pointer. If we found
5062 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005063 if (LastLoadedElt == NumElems - 1) {
5064 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005065 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005066 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005067 LDBase->isVolatile(), LDBase->isNonTemporal(),
5068 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005069 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005070 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005071 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005072 LDBase->isInvariant(), LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00005073 } else if (NumElems == 4 && LastLoadedElt == 1 &&
5074 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005075 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5076 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005077 SDValue ResNode =
5078 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5079 LDBase->getPointerInfo(),
5080 LDBase->getAlignment(),
5081 false/*isVolatile*/, true/*ReadMem*/,
5082 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005083 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005084 }
5085 return SDValue();
5086}
5087
Evan Chengc3630942009-12-09 21:00:30 +00005088SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005089X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005090 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005091
David Greenef125a292011-02-08 19:04:41 +00005092 EVT VT = Op.getValueType();
5093 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005094 unsigned NumElems = Op.getNumOperands();
5095
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005096 // Vectors containing all zeros can be matched by pxor and xorps later
5097 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5098 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5099 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005100 if (Op.getValueType() == MVT::v4i32 ||
5101 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005102 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005103
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005104 return getZeroVector(Op.getValueType(), Subtarget->hasXMMInt(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005105 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005106
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005107 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
5108 // vectors or broken into v4i32 operations on 256-bit vectors.
5109 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
5110 if (Op.getValueType() == MVT::v4i32)
5111 return Op;
5112
5113 return getOnesVector(Op.getValueType(), DAG, dl);
5114 }
5115
Owen Andersone50ed302009-08-10 22:56:29 +00005116 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005117
Evan Cheng0db9fe62006-04-25 20:13:52 +00005118 unsigned NumZero = 0;
5119 unsigned NumNonZero = 0;
5120 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005121 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005122 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005123 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005124 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005125 if (Elt.getOpcode() == ISD::UNDEF)
5126 continue;
5127 Values.insert(Elt);
5128 if (Elt.getOpcode() != ISD::Constant &&
5129 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005130 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005131 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005132 NumZero++;
5133 else {
5134 NonZeros |= (1 << i);
5135 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005136 }
5137 }
5138
Chris Lattner97a2a562010-08-26 05:24:29 +00005139 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5140 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005141 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005142
Chris Lattner67f453a2008-03-09 05:42:06 +00005143 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005144 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005145 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005146 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005147
Chris Lattner62098042008-03-09 01:05:04 +00005148 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5149 // the value are obviously zero, truncate the value to i32 and do the
5150 // insertion that way. Only do this if the value is non-constant or if the
5151 // value is a constant being inserted into element 0. It is cheaper to do
5152 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005153 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005154 (!IsAllConstants || Idx == 0)) {
5155 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005156 // Handle SSE only.
5157 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5158 EVT VecVT = MVT::v4i32;
5159 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005160
Chris Lattner62098042008-03-09 01:05:04 +00005161 // Truncate the value (which may itself be a constant) to i32, and
5162 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005163 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005164 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005165 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005166 Subtarget->hasXMMInt(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005167
Chris Lattner62098042008-03-09 01:05:04 +00005168 // Now we have our 32-bit value zero extended in the low element of
5169 // a vector. If Idx != 0, swizzle it into place.
5170 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005171 SmallVector<int, 4> Mask;
5172 Mask.push_back(Idx);
5173 for (unsigned i = 1; i != VecElts; ++i)
5174 Mask.push_back(i);
5175 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005176 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005177 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005178 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005179 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005180 }
5181 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005182
Chris Lattner19f79692008-03-08 22:59:52 +00005183 // If we have a constant or non-constant insertion into the low element of
5184 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5185 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005186 // depending on what the source datatype is.
5187 if (Idx == 0) {
5188 if (NumZero == 0) {
5189 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005190 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5191 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005192 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5193 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005194 return getShuffleVectorZeroOrUndef(Item, 0, true,Subtarget->hasXMMInt(),
Eli Friedman10415532009-06-06 06:05:10 +00005195 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005196 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5197 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005198 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5199 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005200 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5201 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005202 Subtarget->hasXMMInt(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005203 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005204 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005205 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005206
5207 // Is it a vector logical left shift?
5208 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005209 X86::isZeroNode(Op.getOperand(0)) &&
5210 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005211 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005212 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005213 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005214 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005215 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005216 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005217
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005218 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005219 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005220
Chris Lattner19f79692008-03-08 22:59:52 +00005221 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5222 // is a non-constant being inserted into an element other than the low one,
5223 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5224 // movd/movss) to move this into the low element, then shuffle it into
5225 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005226 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005227 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005228
Evan Cheng0db9fe62006-04-25 20:13:52 +00005229 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005230 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005231 Subtarget->hasXMMInt(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005232 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005233 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005234 MaskVec.push_back(i == Idx ? 0 : 1);
5235 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005236 }
5237 }
5238
Chris Lattner67f453a2008-03-09 05:42:06 +00005239 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005240 if (Values.size() == 1) {
5241 if (EVTBits == 32) {
5242 // Instead of a shuffle like this:
5243 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5244 // Check if it's possible to issue this instead.
5245 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5246 unsigned Idx = CountTrailingZeros_32(NonZeros);
5247 SDValue Item = Op.getOperand(Idx);
5248 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5249 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5250 }
Dan Gohman475871a2008-07-27 21:46:04 +00005251 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005252 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005253
Dan Gohmana3941172007-07-24 22:55:08 +00005254 // A vector full of immediates; various special cases are already
5255 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005256 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005257 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005258
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005259 // For AVX-length vectors, build the individual 128-bit pieces and use
5260 // shuffles to put them in place.
5261 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5262 SmallVector<SDValue, 32> V;
5263 for (unsigned i = 0; i < NumElems; ++i)
5264 V.push_back(Op.getOperand(i));
5265
5266 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5267
5268 // Build both the lower and upper subvector.
5269 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5270 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5271 NumElems/2);
5272
5273 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005274 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5275 DAG.getConstant(0, MVT::i32), DAG, dl);
5276 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005277 DAG, dl);
5278 }
5279
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005280 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005281 if (EVTBits == 64) {
5282 if (NumNonZero == 1) {
5283 // One half is zero or undef.
5284 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005285 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005286 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005287 return getShuffleVectorZeroOrUndef(V2, Idx, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005288 Subtarget->hasXMMInt(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005289 }
Dan Gohman475871a2008-07-27 21:46:04 +00005290 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005291 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005292
5293 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005294 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005295 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005296 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005297 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005298 }
5299
Bill Wendling826f36f2007-03-28 00:57:11 +00005300 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005301 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005302 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005303 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005304 }
5305
5306 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005307 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005308 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005309 if (NumElems == 4 && NumZero > 0) {
5310 for (unsigned i = 0; i < 4; ++i) {
5311 bool isZero = !(NonZeros & (1 << i));
5312 if (isZero)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005313 V[i] = getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005314 else
Dale Johannesenace16102009-02-03 19:33:06 +00005315 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005316 }
5317
5318 for (unsigned i = 0; i < 2; ++i) {
5319 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5320 default: break;
5321 case 0:
5322 V[i] = V[i*2]; // Must be a zero vector.
5323 break;
5324 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005325 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005326 break;
5327 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005328 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005329 break;
5330 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005331 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005332 break;
5333 }
5334 }
5335
Nate Begeman9008ca62009-04-27 18:41:29 +00005336 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005337 bool Reverse = (NonZeros & 0x3) == 2;
5338 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005339 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005340 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5341 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005342 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5343 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005344 }
5345
Nate Begemanfdea31a2010-03-24 20:49:50 +00005346 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5347 // Check for a build vector of consecutive loads.
5348 for (unsigned i = 0; i < NumElems; ++i)
5349 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005350
Nate Begemanfdea31a2010-03-24 20:49:50 +00005351 // Check for elements which are consecutive loads.
5352 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5353 if (LD.getNode())
5354 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005355
5356 // For SSE 4.1, use insertps to put the high elements into the low element.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005357 if (getSubtarget()->hasSSE41() || getSubtarget()->hasAVX()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005358 SDValue Result;
5359 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5360 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5361 else
5362 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005363
Chris Lattner24faf612010-08-28 17:59:08 +00005364 for (unsigned i = 1; i < NumElems; ++i) {
5365 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5366 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005367 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005368 }
5369 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005370 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005371
Chris Lattner6e80e442010-08-28 17:15:43 +00005372 // Otherwise, expand into a number of unpckl*, start by extending each of
5373 // our (non-undef) elements to the full vector width with the element in the
5374 // bottom slot of the vector (which generates no code for SSE).
5375 for (unsigned i = 0; i < NumElems; ++i) {
5376 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5377 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5378 else
5379 V[i] = DAG.getUNDEF(VT);
5380 }
5381
5382 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005383 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5384 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5385 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005386 unsigned EltStride = NumElems >> 1;
5387 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005388 for (unsigned i = 0; i < EltStride; ++i) {
5389 // If V[i+EltStride] is undef and this is the first round of mixing,
5390 // then it is safe to just drop this shuffle: V[i] is already in the
5391 // right place, the one element (since it's the first round) being
5392 // inserted as undef can be dropped. This isn't safe for successive
5393 // rounds because they will permute elements within both vectors.
5394 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5395 EltStride == NumElems/2)
5396 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005397
Chris Lattner6e80e442010-08-28 17:15:43 +00005398 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005399 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005400 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005401 }
5402 return V[0];
5403 }
Dan Gohman475871a2008-07-27 21:46:04 +00005404 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005405}
5406
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005407// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5408// them in a MMX register. This is better than doing a stack convert.
5409static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005410 DebugLoc dl = Op.getDebugLoc();
5411 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005412
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005413 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5414 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5415 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005416 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005417 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5418 InVec = Op.getOperand(1);
5419 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5420 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005421 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005422 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5423 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5424 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005425 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005426 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5427 Mask[0] = 0; Mask[1] = 2;
5428 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5429 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005430 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005431}
5432
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005433// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5434// to create 256-bit vectors from two other 128-bit ones.
5435static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5436 DebugLoc dl = Op.getDebugLoc();
5437 EVT ResVT = Op.getValueType();
5438
5439 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5440
5441 SDValue V1 = Op.getOperand(0);
5442 SDValue V2 = Op.getOperand(1);
5443 unsigned NumElems = ResVT.getVectorNumElements();
5444
5445 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5446 DAG.getConstant(0, MVT::i32), DAG, dl);
5447 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5448 DAG, dl);
5449}
5450
5451SDValue
5452X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005453 EVT ResVT = Op.getValueType();
5454
5455 assert(Op.getNumOperands() == 2);
5456 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5457 "Unsupported CONCAT_VECTORS for value type");
5458
5459 // We support concatenate two MMX registers and place them in a MMX register.
5460 // This is better than doing a stack convert.
5461 if (ResVT.is128BitVector())
5462 return LowerMMXCONCAT_VECTORS(Op, DAG);
5463
5464 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5465 // from two other 128-bit ones.
5466 return LowerAVXCONCAT_VECTORS(Op, DAG);
5467}
5468
Nate Begemanb9a47b82009-02-23 08:49:38 +00005469// v8i16 shuffles - Prefer shuffles in the following order:
5470// 1. [all] pshuflw, pshufhw, optional move
5471// 2. [ssse3] 1 x pshufb
5472// 3. [ssse3] 2 x pshufb + 1 x por
5473// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005474SDValue
5475X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5476 SelectionDAG &DAG) const {
5477 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005478 SDValue V1 = SVOp->getOperand(0);
5479 SDValue V2 = SVOp->getOperand(1);
5480 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005481 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005482
Nate Begemanb9a47b82009-02-23 08:49:38 +00005483 // Determine if more than 1 of the words in each of the low and high quadwords
5484 // of the result come from the same quadword of one of the two inputs. Undef
5485 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005486 unsigned LoQuad[] = { 0, 0, 0, 0 };
5487 unsigned HiQuad[] = { 0, 0, 0, 0 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005488 BitVector InputQuads(4);
5489 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005490 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005491 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005492 MaskVals.push_back(EltIdx);
5493 if (EltIdx < 0) {
5494 ++Quad[0];
5495 ++Quad[1];
5496 ++Quad[2];
5497 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005498 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005499 }
5500 ++Quad[EltIdx / 4];
5501 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005502 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005503
Nate Begemanb9a47b82009-02-23 08:49:38 +00005504 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005505 unsigned MaxQuad = 1;
5506 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005507 if (LoQuad[i] > MaxQuad) {
5508 BestLoQuad = i;
5509 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005510 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005511 }
5512
Nate Begemanb9a47b82009-02-23 08:49:38 +00005513 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005514 MaxQuad = 1;
5515 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005516 if (HiQuad[i] > MaxQuad) {
5517 BestHiQuad = i;
5518 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005519 }
5520 }
5521
Nate Begemanb9a47b82009-02-23 08:49:38 +00005522 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005523 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005524 // single pshufb instruction is necessary. If There are more than 2 input
5525 // quads, disable the next transformation since it does not help SSSE3.
5526 bool V1Used = InputQuads[0] || InputQuads[1];
5527 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005528 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005529 if (InputQuads.count() == 2 && V1Used && V2Used) {
5530 BestLoQuad = InputQuads.find_first();
5531 BestHiQuad = InputQuads.find_next(BestLoQuad);
5532 }
5533 if (InputQuads.count() > 2) {
5534 BestLoQuad = -1;
5535 BestHiQuad = -1;
5536 }
5537 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005538
Nate Begemanb9a47b82009-02-23 08:49:38 +00005539 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5540 // the shuffle mask. If a quad is scored as -1, that means that it contains
5541 // words from all 4 input quadwords.
5542 SDValue NewV;
5543 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005544 SmallVector<int, 8> MaskV;
5545 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5546 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005547 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005548 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5549 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5550 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005551
Nate Begemanb9a47b82009-02-23 08:49:38 +00005552 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5553 // source words for the shuffle, to aid later transformations.
5554 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005555 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005556 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005557 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005558 if (idx != (int)i)
5559 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005560 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005561 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005562 AllWordsInNewV = false;
5563 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005564 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005565
Nate Begemanb9a47b82009-02-23 08:49:38 +00005566 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5567 if (AllWordsInNewV) {
5568 for (int i = 0; i != 8; ++i) {
5569 int idx = MaskVals[i];
5570 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005571 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005572 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005573 if ((idx != i) && idx < 4)
5574 pshufhw = false;
5575 if ((idx != i) && idx > 3)
5576 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005577 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005578 V1 = NewV;
5579 V2Used = false;
5580 BestLoQuad = 0;
5581 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005582 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005583
Nate Begemanb9a47b82009-02-23 08:49:38 +00005584 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5585 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005586 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005587 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5588 unsigned TargetMask = 0;
5589 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005590 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005591 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5592 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5593 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005594 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005595 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005596 }
Eric Christopherfd179292009-08-27 18:07:15 +00005597
Nate Begemanb9a47b82009-02-23 08:49:38 +00005598 // If we have SSSE3, and all words of the result are from 1 input vector,
5599 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5600 // is present, fall back to case 4.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005601 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005602 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005603
Nate Begemanb9a47b82009-02-23 08:49:38 +00005604 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005605 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005606 // mask, and elements that come from V1 in the V2 mask, so that the two
5607 // results can be OR'd together.
5608 bool TwoInputs = V1Used && V2Used;
5609 for (unsigned i = 0; i != 8; ++i) {
5610 int EltIdx = MaskVals[i] * 2;
5611 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005612 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5613 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005614 continue;
5615 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005616 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5617 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005618 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005619 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005620 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005621 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005622 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005623 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005624 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005625
Nate Begemanb9a47b82009-02-23 08:49:38 +00005626 // Calculate the shuffle mask for the second input, shuffle it, and
5627 // OR it with the first shuffled input.
5628 pshufbMask.clear();
5629 for (unsigned i = 0; i != 8; ++i) {
5630 int EltIdx = MaskVals[i] * 2;
5631 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005632 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5633 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005634 continue;
5635 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005636 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5637 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005638 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005639 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005640 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005641 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005642 MVT::v16i8, &pshufbMask[0], 16));
5643 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005644 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005645 }
5646
5647 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5648 // and update MaskVals with new element order.
5649 BitVector InOrder(8);
5650 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005651 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005652 for (int i = 0; i != 4; ++i) {
5653 int idx = MaskVals[i];
5654 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005655 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005656 InOrder.set(i);
5657 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005658 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005659 InOrder.set(i);
5660 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005661 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005662 }
5663 }
5664 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005665 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005666 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005667 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005668
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005669 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5670 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005671 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5672 NewV.getOperand(0),
5673 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5674 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005675 }
Eric Christopherfd179292009-08-27 18:07:15 +00005676
Nate Begemanb9a47b82009-02-23 08:49:38 +00005677 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5678 // and update MaskVals with the new element order.
5679 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005680 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005681 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005682 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005683 for (unsigned i = 4; i != 8; ++i) {
5684 int idx = MaskVals[i];
5685 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005686 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005687 InOrder.set(i);
5688 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005689 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005690 InOrder.set(i);
5691 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005692 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005693 }
5694 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005695 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005696 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005697
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005698 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5699 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005700 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5701 NewV.getOperand(0),
5702 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5703 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005704 }
Eric Christopherfd179292009-08-27 18:07:15 +00005705
Nate Begemanb9a47b82009-02-23 08:49:38 +00005706 // In case BestHi & BestLo were both -1, which means each quadword has a word
5707 // from each of the four input quadwords, calculate the InOrder bitvector now
5708 // before falling through to the insert/extract cleanup.
5709 if (BestLoQuad == -1 && BestHiQuad == -1) {
5710 NewV = V1;
5711 for (int i = 0; i != 8; ++i)
5712 if (MaskVals[i] < 0 || MaskVals[i] == i)
5713 InOrder.set(i);
5714 }
Eric Christopherfd179292009-08-27 18:07:15 +00005715
Nate Begemanb9a47b82009-02-23 08:49:38 +00005716 // The other elements are put in the right place using pextrw and pinsrw.
5717 for (unsigned i = 0; i != 8; ++i) {
5718 if (InOrder[i])
5719 continue;
5720 int EltIdx = MaskVals[i];
5721 if (EltIdx < 0)
5722 continue;
5723 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005724 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005725 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005726 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005727 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005728 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005729 DAG.getIntPtrConstant(i));
5730 }
5731 return NewV;
5732}
5733
5734// v16i8 shuffles - Prefer shuffles in the following order:
5735// 1. [ssse3] 1 x pshufb
5736// 2. [ssse3] 2 x pshufb + 1 x por
5737// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5738static
Nate Begeman9008ca62009-04-27 18:41:29 +00005739SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005740 SelectionDAG &DAG,
5741 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005742 SDValue V1 = SVOp->getOperand(0);
5743 SDValue V2 = SVOp->getOperand(1);
5744 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005745 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005746 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005747
Nate Begemanb9a47b82009-02-23 08:49:38 +00005748 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005749 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005750 // present, fall back to case 3.
5751 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5752 bool V1Only = true;
5753 bool V2Only = true;
5754 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005755 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005756 if (EltIdx < 0)
5757 continue;
5758 if (EltIdx < 16)
5759 V2Only = false;
5760 else
5761 V1Only = false;
5762 }
Eric Christopherfd179292009-08-27 18:07:15 +00005763
Nate Begemanb9a47b82009-02-23 08:49:38 +00005764 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005765 if (TLI.getSubtarget()->hasSSSE3() || TLI.getSubtarget()->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005766 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005767
Nate Begemanb9a47b82009-02-23 08:49:38 +00005768 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005769 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005770 //
5771 // Otherwise, we have elements from both input vectors, and must zero out
5772 // elements that come from V2 in the first mask, and V1 in the second mask
5773 // so that we can OR them together.
5774 bool TwoInputs = !(V1Only || V2Only);
5775 for (unsigned i = 0; i != 16; ++i) {
5776 int EltIdx = MaskVals[i];
5777 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005778 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005779 continue;
5780 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005781 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005782 }
5783 // If all the elements are from V2, assign it to V1 and return after
5784 // building the first pshufb.
5785 if (V2Only)
5786 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005787 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005788 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005789 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005790 if (!TwoInputs)
5791 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005792
Nate Begemanb9a47b82009-02-23 08:49:38 +00005793 // Calculate the shuffle mask for the second input, shuffle it, and
5794 // OR it with the first shuffled input.
5795 pshufbMask.clear();
5796 for (unsigned i = 0; i != 16; ++i) {
5797 int EltIdx = MaskVals[i];
5798 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005799 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005800 continue;
5801 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005802 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005803 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005804 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005805 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005806 MVT::v16i8, &pshufbMask[0], 16));
5807 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005808 }
Eric Christopherfd179292009-08-27 18:07:15 +00005809
Nate Begemanb9a47b82009-02-23 08:49:38 +00005810 // No SSSE3 - Calculate in place words and then fix all out of place words
5811 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5812 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005813 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5814 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005815 SDValue NewV = V2Only ? V2 : V1;
5816 for (int i = 0; i != 8; ++i) {
5817 int Elt0 = MaskVals[i*2];
5818 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005819
Nate Begemanb9a47b82009-02-23 08:49:38 +00005820 // This word of the result is all undef, skip it.
5821 if (Elt0 < 0 && Elt1 < 0)
5822 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005823
Nate Begemanb9a47b82009-02-23 08:49:38 +00005824 // This word of the result is already in the correct place, skip it.
5825 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5826 continue;
5827 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5828 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005829
Nate Begemanb9a47b82009-02-23 08:49:38 +00005830 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5831 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5832 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005833
5834 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5835 // using a single extract together, load it and store it.
5836 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005837 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005838 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005839 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005840 DAG.getIntPtrConstant(i));
5841 continue;
5842 }
5843
Nate Begemanb9a47b82009-02-23 08:49:38 +00005844 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005845 // source byte is not also odd, shift the extracted word left 8 bits
5846 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005847 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005848 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005849 DAG.getIntPtrConstant(Elt1 / 2));
5850 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005851 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005852 DAG.getConstant(8,
5853 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005854 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005855 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5856 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005857 }
5858 // If Elt0 is defined, extract it from the appropriate source. If the
5859 // source byte is not also even, shift the extracted word right 8 bits. If
5860 // Elt1 was also defined, OR the extracted values together before
5861 // inserting them in the result.
5862 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005863 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005864 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5865 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005866 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005867 DAG.getConstant(8,
5868 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005869 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005870 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5871 DAG.getConstant(0x00FF, MVT::i16));
5872 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005873 : InsElt0;
5874 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005875 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005876 DAG.getIntPtrConstant(i));
5877 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005878 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005879}
5880
Evan Cheng7a831ce2007-12-15 03:00:47 +00005881/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005882/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005883/// done when every pair / quad of shuffle mask elements point to elements in
5884/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005885/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005886static
Nate Begeman9008ca62009-04-27 18:41:29 +00005887SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005888 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005889 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005890 SDValue V1 = SVOp->getOperand(0);
5891 SDValue V2 = SVOp->getOperand(1);
5892 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005893 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005894 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005895 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005896 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005897 case MVT::v4f32: NewVT = MVT::v2f64; break;
5898 case MVT::v4i32: NewVT = MVT::v2i64; break;
5899 case MVT::v8i16: NewVT = MVT::v4i32; break;
5900 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005901 }
5902
Nate Begeman9008ca62009-04-27 18:41:29 +00005903 int Scale = NumElems / NewWidth;
5904 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005905 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005906 int StartIdx = -1;
5907 for (int j = 0; j < Scale; ++j) {
5908 int EltIdx = SVOp->getMaskElt(i+j);
5909 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005910 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005911 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005912 StartIdx = EltIdx - (EltIdx % Scale);
5913 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005914 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005915 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005916 if (StartIdx == -1)
5917 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005918 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005919 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005920 }
5921
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005922 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5923 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005924 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005925}
5926
Evan Chengd880b972008-05-09 21:53:03 +00005927/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005928///
Owen Andersone50ed302009-08-10 22:56:29 +00005929static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005930 SDValue SrcOp, SelectionDAG &DAG,
5931 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005932 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005933 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005934 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005935 LD = dyn_cast<LoadSDNode>(SrcOp);
5936 if (!LD) {
5937 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5938 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005939 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005940 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005941 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005942 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005943 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005944 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005945 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005946 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005947 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5948 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5949 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005950 SrcOp.getOperand(0)
5951 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005952 }
5953 }
5954 }
5955
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005956 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005957 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005958 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005959 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005960}
5961
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005962/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5963/// shuffle node referes to only one lane in the sources.
5964static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5965 EVT VT = SVOp->getValueType(0);
5966 int NumElems = VT.getVectorNumElements();
5967 int HalfSize = NumElems/2;
5968 SmallVector<int, 16> M;
5969 SVOp->getMask(M);
5970 bool MatchA = false, MatchB = false;
5971
5972 for (int l = 0; l < NumElems*2; l += HalfSize) {
5973 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5974 MatchA = true;
5975 break;
5976 }
5977 }
5978
5979 for (int l = 0; l < NumElems*2; l += HalfSize) {
5980 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
5981 MatchB = true;
5982 break;
5983 }
5984 }
5985
5986 return MatchA && MatchB;
5987}
5988
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005989/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5990/// which could not be matched by any known target speficic shuffle
5991static SDValue
5992LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005993 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
5994 // If each half of a vector shuffle node referes to only one lane in the
5995 // source vectors, extract each used 128-bit lane and shuffle them using
5996 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
5997 // the work to the legalizer.
5998 DebugLoc dl = SVOp->getDebugLoc();
5999 EVT VT = SVOp->getValueType(0);
6000 int NumElems = VT.getVectorNumElements();
6001 int HalfSize = NumElems/2;
6002
6003 // Extract the reference for each half
6004 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
6005 int FstVecOpNum = 0, SndVecOpNum = 0;
6006 for (int i = 0; i < HalfSize; ++i) {
6007 int Elt = SVOp->getMaskElt(i);
6008 if (SVOp->getMaskElt(i) < 0)
6009 continue;
6010 FstVecOpNum = Elt/NumElems;
6011 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
6012 break;
6013 }
6014 for (int i = HalfSize; i < NumElems; ++i) {
6015 int Elt = SVOp->getMaskElt(i);
6016 if (SVOp->getMaskElt(i) < 0)
6017 continue;
6018 SndVecOpNum = Elt/NumElems;
6019 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
6020 break;
6021 }
6022
6023 // Extract the subvectors
6024 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
6025 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
6026 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
6027 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
6028
6029 // Generate 128-bit shuffles
6030 SmallVector<int, 16> MaskV1, MaskV2;
6031 for (int i = 0; i < HalfSize; ++i) {
6032 int Elt = SVOp->getMaskElt(i);
6033 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
6034 }
6035 for (int i = HalfSize; i < NumElems; ++i) {
6036 int Elt = SVOp->getMaskElt(i);
6037 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
6038 }
6039
6040 EVT NVT = V1.getValueType();
6041 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
6042 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
6043
6044 // Concatenate the result back
6045 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
6046 DAG.getConstant(0, MVT::i32), DAG, dl);
6047 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
6048 DAG, dl);
6049 }
6050
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006051 return SDValue();
6052}
6053
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006054/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6055/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006056static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006057LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006058 SDValue V1 = SVOp->getOperand(0);
6059 SDValue V2 = SVOp->getOperand(1);
6060 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006061 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006062
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006063 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
6064
Evan Chengace3c172008-07-22 21:13:36 +00006065 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00006066 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006067 SmallVector<int, 8> Mask1(4U, -1);
6068 SmallVector<int, 8> PermMask;
6069 SVOp->getMask(PermMask);
6070
Evan Chengace3c172008-07-22 21:13:36 +00006071 unsigned NumHi = 0;
6072 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006073 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006074 int Idx = PermMask[i];
6075 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006076 Locs[i] = std::make_pair(-1, -1);
6077 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006078 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6079 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006080 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006081 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006082 NumLo++;
6083 } else {
6084 Locs[i] = std::make_pair(1, NumHi);
6085 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006086 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006087 NumHi++;
6088 }
6089 }
6090 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006091
Evan Chengace3c172008-07-22 21:13:36 +00006092 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006093 // If no more than two elements come from either vector. This can be
6094 // implemented with two shuffles. First shuffle gather the elements.
6095 // The second shuffle, which takes the first shuffle as both of its
6096 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006097 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006098
Nate Begeman9008ca62009-04-27 18:41:29 +00006099 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006100
Evan Chengace3c172008-07-22 21:13:36 +00006101 for (unsigned i = 0; i != 4; ++i) {
6102 if (Locs[i].first == -1)
6103 continue;
6104 else {
6105 unsigned Idx = (i < 2) ? 0 : 4;
6106 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006107 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006108 }
6109 }
6110
Nate Begeman9008ca62009-04-27 18:41:29 +00006111 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006112 } else if (NumLo == 3 || NumHi == 3) {
6113 // Otherwise, we must have three elements from one vector, call it X, and
6114 // one element from the other, call it Y. First, use a shufps to build an
6115 // intermediate vector with the one element from Y and the element from X
6116 // that will be in the same half in the final destination (the indexes don't
6117 // matter). Then, use a shufps to build the final vector, taking the half
6118 // containing the element from Y from the intermediate, and the other half
6119 // from X.
6120 if (NumHi == 3) {
6121 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00006122 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006123 std::swap(V1, V2);
6124 }
6125
6126 // Find the element from V2.
6127 unsigned HiIndex;
6128 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006129 int Val = PermMask[HiIndex];
6130 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006131 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006132 if (Val >= 4)
6133 break;
6134 }
6135
Nate Begeman9008ca62009-04-27 18:41:29 +00006136 Mask1[0] = PermMask[HiIndex];
6137 Mask1[1] = -1;
6138 Mask1[2] = PermMask[HiIndex^1];
6139 Mask1[3] = -1;
6140 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006141
6142 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006143 Mask1[0] = PermMask[0];
6144 Mask1[1] = PermMask[1];
6145 Mask1[2] = HiIndex & 1 ? 6 : 4;
6146 Mask1[3] = HiIndex & 1 ? 4 : 6;
6147 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006148 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006149 Mask1[0] = HiIndex & 1 ? 2 : 0;
6150 Mask1[1] = HiIndex & 1 ? 0 : 2;
6151 Mask1[2] = PermMask[2];
6152 Mask1[3] = PermMask[3];
6153 if (Mask1[2] >= 0)
6154 Mask1[2] += 4;
6155 if (Mask1[3] >= 0)
6156 Mask1[3] += 4;
6157 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006158 }
Evan Chengace3c172008-07-22 21:13:36 +00006159 }
6160
6161 // Break it into (shuffle shuffle_hi, shuffle_lo).
6162 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006163 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006164 SmallVector<int,8> LoMask(4U, -1);
6165 SmallVector<int,8> HiMask(4U, -1);
6166
6167 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006168 unsigned MaskIdx = 0;
6169 unsigned LoIdx = 0;
6170 unsigned HiIdx = 2;
6171 for (unsigned i = 0; i != 4; ++i) {
6172 if (i == 2) {
6173 MaskPtr = &HiMask;
6174 MaskIdx = 1;
6175 LoIdx = 0;
6176 HiIdx = 2;
6177 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006178 int Idx = PermMask[i];
6179 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006180 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006181 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006182 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006183 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006184 LoIdx++;
6185 } else {
6186 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006187 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006188 HiIdx++;
6189 }
6190 }
6191
Nate Begeman9008ca62009-04-27 18:41:29 +00006192 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6193 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6194 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006195 for (unsigned i = 0; i != 4; ++i) {
6196 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006197 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006198 } else {
6199 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006200 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006201 }
6202 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006203 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006204}
6205
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006206static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006207 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006208 V = V.getOperand(0);
6209 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6210 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006211 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6212 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6213 // BUILD_VECTOR (load), undef
6214 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006215 if (MayFoldLoad(V))
6216 return true;
6217 return false;
6218}
6219
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006220// FIXME: the version above should always be used. Since there's
6221// a bug where several vector shuffles can't be folded because the
6222// DAG is not updated during lowering and a node claims to have two
6223// uses while it only has one, use this version, and let isel match
6224// another instruction if the load really happens to have more than
6225// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006226// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006227static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006228 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006229 V = V.getOperand(0);
6230 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6231 V = V.getOperand(0);
6232 if (ISD::isNormalLoad(V.getNode()))
6233 return true;
6234 return false;
6235}
6236
6237/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6238/// a vector extract, and if both can be later optimized into a single load.
6239/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6240/// here because otherwise a target specific shuffle node is going to be
6241/// emitted for this shuffle, and the optimization not done.
6242/// FIXME: This is probably not the best approach, but fix the problem
6243/// until the right path is decided.
6244static
6245bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6246 const TargetLowering &TLI) {
6247 EVT VT = V.getValueType();
6248 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6249
6250 // Be sure that the vector shuffle is present in a pattern like this:
6251 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6252 if (!V.hasOneUse())
6253 return false;
6254
6255 SDNode *N = *V.getNode()->use_begin();
6256 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6257 return false;
6258
6259 SDValue EltNo = N->getOperand(1);
6260 if (!isa<ConstantSDNode>(EltNo))
6261 return false;
6262
6263 // If the bit convert changed the number of elements, it is unsafe
6264 // to examine the mask.
6265 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006266 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006267 EVT SrcVT = V.getOperand(0).getValueType();
6268 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6269 return false;
6270 V = V.getOperand(0);
6271 HasShuffleIntoBitcast = true;
6272 }
6273
6274 // Select the input vector, guarding against out of range extract vector.
6275 unsigned NumElems = VT.getVectorNumElements();
6276 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6277 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6278 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6279
6280 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006281 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006282 V = V.getOperand(0);
6283
6284 if (ISD::isNormalLoad(V.getNode())) {
6285 // Is the original load suitable?
6286 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6287
6288 // FIXME: avoid the multi-use bug that is preventing lots of
6289 // of foldings to be detected, this is still wrong of course, but
6290 // give the temporary desired behavior, and if it happens that
6291 // the load has real more uses, during isel it will not fold, and
6292 // will generate poor code.
6293 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6294 return false;
6295
6296 if (!HasShuffleIntoBitcast)
6297 return true;
6298
6299 // If there's a bitcast before the shuffle, check if the load type and
6300 // alignment is valid.
6301 unsigned Align = LN0->getAlignment();
6302 unsigned NewAlign =
6303 TLI.getTargetData()->getABITypeAlignment(
6304 VT.getTypeForEVT(*DAG.getContext()));
6305
6306 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6307 return false;
6308 }
6309
6310 return true;
6311}
6312
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006313static
Evan Cheng835580f2010-10-07 20:50:20 +00006314SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6315 EVT VT = Op.getValueType();
6316
6317 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006318 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6319 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006320 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6321 V1, DAG));
6322}
6323
6324static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006325SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006326 bool HasXMMInt) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006327 SDValue V1 = Op.getOperand(0);
6328 SDValue V2 = Op.getOperand(1);
6329 EVT VT = Op.getValueType();
6330
6331 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6332
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006333 if (HasXMMInt && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006334 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6335
Evan Cheng0899f5c2011-08-31 02:05:24 +00006336 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6337 return DAG.getNode(ISD::BITCAST, dl, VT,
6338 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6339 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6340 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006341}
6342
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006343static
6344SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6345 SDValue V1 = Op.getOperand(0);
6346 SDValue V2 = Op.getOperand(1);
6347 EVT VT = Op.getValueType();
6348
6349 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6350 "unsupported shuffle type");
6351
6352 if (V2.getOpcode() == ISD::UNDEF)
6353 V2 = V1;
6354
6355 // v4i32 or v4f32
6356 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6357}
6358
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006359static inline unsigned getSHUFPOpcode(EVT VT) {
6360 switch(VT.getSimpleVT().SimpleTy) {
6361 case MVT::v8i32: // Use fp unit for int unpack.
6362 case MVT::v8f32:
6363 case MVT::v4i32: // Use fp unit for int unpack.
6364 case MVT::v4f32: return X86ISD::SHUFPS;
6365 case MVT::v4i64: // Use fp unit for int unpack.
6366 case MVT::v4f64:
6367 case MVT::v2i64: // Use fp unit for int unpack.
6368 case MVT::v2f64: return X86ISD::SHUFPD;
6369 default:
6370 llvm_unreachable("Unknown type for shufp*");
6371 }
6372 return 0;
6373}
6374
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006375static
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006376SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasXMMInt) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006377 SDValue V1 = Op.getOperand(0);
6378 SDValue V2 = Op.getOperand(1);
6379 EVT VT = Op.getValueType();
6380 unsigned NumElems = VT.getVectorNumElements();
6381
6382 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6383 // operand of these instructions is only memory, so check if there's a
6384 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6385 // same masks.
6386 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006387
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006388 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006389 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006390 CanFoldLoad = true;
6391
6392 // When V1 is a load, it can be folded later into a store in isel, example:
6393 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6394 // turns into:
6395 // (MOVLPSmr addr:$src1, VR128:$src2)
6396 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006397 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006398 CanFoldLoad = true;
6399
Dan Gohman65fd6562011-11-03 21:49:52 +00006400 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006401 if (CanFoldLoad) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006402 if (HasXMMInt && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006403 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6404
6405 if (NumElems == 4)
Dan Gohman65fd6562011-11-03 21:49:52 +00006406 // If we don't care about the second element, procede to use movss.
6407 if (SVOp->getMaskElt(1) != -1)
6408 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006409 }
6410
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006411 // movl and movlp will both match v2i64, but v2i64 is never matched by
6412 // movl earlier because we make it strict to avoid messing with the movlp load
6413 // folding logic (see the code above getMOVLP call). Match it here then,
6414 // this is horrible, but will stay like this until we move all shuffle
6415 // matching to x86 specific nodes. Note that for the 1st condition all
6416 // types are matched with movsd.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006417 if (HasXMMInt) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006418 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6419 // as to remove this logic from here, as much as possible
6420 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006421 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006422 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006423 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006424
6425 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6426
6427 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006428 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006429 X86::getShuffleSHUFImmediate(SVOp), DAG);
6430}
6431
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006432static inline unsigned getUNPCKLOpcode(EVT VT) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006433 switch(VT.getSimpleVT().SimpleTy) {
6434 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
6435 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006436 case MVT::v4f32: return X86ISD::UNPCKLPS;
6437 case MVT::v2f64: return X86ISD::UNPCKLPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006438 case MVT::v8i32: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006439 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006440 case MVT::v4i64: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006441 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006442 case MVT::v16i8: return X86ISD::PUNPCKLBW;
6443 case MVT::v8i16: return X86ISD::PUNPCKLWD;
6444 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006445 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006446 }
6447 return 0;
6448}
6449
6450static inline unsigned getUNPCKHOpcode(EVT VT) {
6451 switch(VT.getSimpleVT().SimpleTy) {
6452 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
6453 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
6454 case MVT::v4f32: return X86ISD::UNPCKHPS;
6455 case MVT::v2f64: return X86ISD::UNPCKHPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006456 case MVT::v8i32: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006457 case MVT::v8f32: return X86ISD::VUNPCKHPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006458 case MVT::v4i64: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006459 case MVT::v4f64: return X86ISD::VUNPCKHPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006460 case MVT::v16i8: return X86ISD::PUNPCKHBW;
6461 case MVT::v8i16: return X86ISD::PUNPCKHWD;
6462 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006463 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006464 }
6465 return 0;
6466}
6467
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006468static inline unsigned getVPERMILOpcode(EVT VT) {
6469 switch(VT.getSimpleVT().SimpleTy) {
6470 case MVT::v4i32:
6471 case MVT::v4f32: return X86ISD::VPERMILPS;
6472 case MVT::v2i64:
6473 case MVT::v2f64: return X86ISD::VPERMILPD;
6474 case MVT::v8i32:
6475 case MVT::v8f32: return X86ISD::VPERMILPSY;
6476 case MVT::v4i64:
6477 case MVT::v4f64: return X86ISD::VPERMILPDY;
6478 default:
6479 llvm_unreachable("Unknown type for vpermil");
6480 }
6481 return 0;
6482}
6483
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006484/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
6485/// a vbroadcast node. The nodes are suitable whenever we can fold a load coming
6486/// from a 32 or 64 bit scalar. Update Op to the desired load to be folded.
6487static bool isVectorBroadcast(SDValue &Op) {
6488 EVT VT = Op.getValueType();
6489 bool Is256 = VT.getSizeInBits() == 256;
6490
6491 assert((VT.getSizeInBits() == 128 || Is256) &&
6492 "Unsupported type for vbroadcast node");
6493
6494 SDValue V = Op;
6495 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
6496 V = V.getOperand(0);
6497
6498 if (Is256 && !(V.hasOneUse() &&
6499 V.getOpcode() == ISD::INSERT_SUBVECTOR &&
6500 V.getOperand(0).getOpcode() == ISD::UNDEF))
6501 return false;
6502
6503 if (Is256)
6504 V = V.getOperand(1);
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006505
6506 if (!V.hasOneUse())
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006507 return false;
6508
6509 // Check the source scalar_to_vector type. 256-bit broadcasts are
6510 // supported for 32/64-bit sizes, while 128-bit ones are only supported
6511 // for 32-bit scalars.
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006512 if (V.getOpcode() != ISD::SCALAR_TO_VECTOR)
6513 return false;
6514
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006515 unsigned ScalarSize = V.getOperand(0).getValueType().getSizeInBits();
6516 if (ScalarSize != 32 && ScalarSize != 64)
6517 return false;
6518 if (!Is256 && ScalarSize == 64)
6519 return false;
6520
6521 V = V.getOperand(0);
6522 if (!MayFoldLoad(V))
6523 return false;
6524
6525 // Return the load node
6526 Op = V;
6527 return true;
6528}
6529
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006530static
6531SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006532 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006533 const X86Subtarget *Subtarget) {
6534 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6535 EVT VT = Op.getValueType();
6536 DebugLoc dl = Op.getDebugLoc();
6537 SDValue V1 = Op.getOperand(0);
6538 SDValue V2 = Op.getOperand(1);
6539
6540 if (isZeroShuffle(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006541 return getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006542
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006543 // Handle splat operations
6544 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006545 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006546 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006547 // Special case, this is the only place now where it's allowed to return
6548 // a vector_shuffle operation without using a target specific node, because
6549 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6550 // this be moved to DAGCombine instead?
6551 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006552 return Op;
6553
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006554 // Use vbroadcast whenever the splat comes from a foldable load
6555 if (Subtarget->hasAVX() && isVectorBroadcast(V1))
6556 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, V1);
6557
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006558 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006559 if ((Size == 128 && NumElem <= 4) ||
6560 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006561 return SDValue();
6562
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006563 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006564 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006565 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006566
6567 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6568 // do it!
6569 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6570 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6571 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006572 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006573 } else if ((VT == MVT::v4i32 ||
6574 (VT == MVT::v4f32 && Subtarget->hasXMMInt()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006575 // FIXME: Figure out a cleaner way to do this.
6576 // Try to make use of movq to zero out the top part.
6577 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6578 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6579 if (NewOp.getNode()) {
6580 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6581 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6582 DAG, Subtarget, dl);
6583 }
6584 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6585 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6586 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6587 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6588 DAG, Subtarget, dl);
6589 }
6590 }
6591 return SDValue();
6592}
6593
Dan Gohman475871a2008-07-27 21:46:04 +00006594SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006595X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006596 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006597 SDValue V1 = Op.getOperand(0);
6598 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006599 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006600 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006601 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006602 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006603 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
6604 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006605 bool V1IsSplat = false;
6606 bool V2IsSplat = false;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006607 bool HasXMMInt = Subtarget->hasXMMInt();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006608 MachineFunction &MF = DAG.getMachineFunction();
6609 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006610
Dale Johannesen0488fb62010-09-30 23:57:10 +00006611 // Shuffle operations on MMX not supported.
6612 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006613 return Op;
6614
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006615 // Vector shuffle lowering takes 3 steps:
6616 //
6617 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6618 // narrowing and commutation of operands should be handled.
6619 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6620 // shuffle nodes.
6621 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6622 // so the shuffle can be broken into other shuffles and the legalizer can
6623 // try the lowering again.
6624 //
6625 // The general ideia is that no vector_shuffle operation should be left to
6626 // be matched during isel, all of them must be converted to a target specific
6627 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006628
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006629 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6630 // narrowing and commutation of operands should be handled. The actual code
6631 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006632 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006633 if (NewOp.getNode())
6634 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006635
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006636 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6637 // unpckh_undef). Only use pshufd if speed is more important than size.
6638 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006639 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006640 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006641 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006642
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006643 if (X86::isMOVDDUPMask(SVOp) &&
6644 (Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
6645 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006646 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006647
Dale Johannesen0488fb62010-09-30 23:57:10 +00006648 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006649 return getMOVHighToLow(Op, dl, DAG);
6650
6651 // Use to match splats
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006652 if (HasXMMInt && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006653 (VT == MVT::v2f64 || VT == MVT::v2i64))
6654 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
6655
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006656 if (X86::isPSHUFDMask(SVOp)) {
6657 // The actual implementation will match the mask in the if above and then
6658 // during isel it can match several different instructions, not only pshufd
6659 // as its name says, sad but true, emulate the behavior for now...
6660 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6661 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6662
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006663 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6664
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006665 if (HasXMMInt && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006666 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6667
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006668 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6669 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006670 }
Eric Christopherfd179292009-08-27 18:07:15 +00006671
Evan Chengf26ffe92008-05-29 08:22:04 +00006672 // Check if this can be converted into a logical shift.
6673 bool isLeft = false;
6674 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006675 SDValue ShVal;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006676 bool isShift = getSubtarget()->hasXMMInt() &&
6677 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006678 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006679 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006680 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006681 EVT EltVT = VT.getVectorElementType();
6682 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006683 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006684 }
Eric Christopherfd179292009-08-27 18:07:15 +00006685
Nate Begeman9008ca62009-04-27 18:41:29 +00006686 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006687 if (V1IsUndef)
6688 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00006689 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006690 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006691 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006692 if (HasXMMInt && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006693 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6694
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006695 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006696 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6697 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006698 }
Eric Christopherfd179292009-08-27 18:07:15 +00006699
Nate Begeman9008ca62009-04-27 18:41:29 +00006700 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00006701 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006702 return getMOVLowToHigh(Op, dl, DAG, HasXMMInt);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006703
Dale Johannesen0488fb62010-09-30 23:57:10 +00006704 if (X86::isMOVHLPSMask(SVOp))
6705 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006706
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006707 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006708 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006709
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006710 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006711 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006712
Dale Johannesen0488fb62010-09-30 23:57:10 +00006713 if (X86::isMOVLPMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006714 return getMOVLP(Op, dl, DAG, HasXMMInt);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006715
Nate Begeman9008ca62009-04-27 18:41:29 +00006716 if (ShouldXformToMOVHLPS(SVOp) ||
6717 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6718 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006719
Evan Chengf26ffe92008-05-29 08:22:04 +00006720 if (isShift) {
6721 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006722 EVT EltVT = VT.getVectorElementType();
6723 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006724 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006725 }
Eric Christopherfd179292009-08-27 18:07:15 +00006726
Evan Cheng9eca5e82006-10-25 21:49:50 +00006727 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006728 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6729 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006730 V1IsSplat = isSplatVector(V1.getNode());
6731 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006732
Chris Lattner8a594482007-11-25 00:24:49 +00006733 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00006734 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006735 Op = CommuteVectorShuffle(SVOp, DAG);
6736 SVOp = cast<ShuffleVectorSDNode>(Op);
6737 V1 = SVOp->getOperand(0);
6738 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006739 std::swap(V1IsSplat, V2IsSplat);
6740 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006741 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006742 }
6743
Nate Begeman9008ca62009-04-27 18:41:29 +00006744 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6745 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006746 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006747 return V1;
6748 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6749 // the instruction selector will not match, so get a canonical MOVL with
6750 // swapped operands to undo the commute.
6751 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006752 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006753
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006754 if (X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006755 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006756
6757 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006758 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006759
Evan Cheng9bbbb982006-10-25 20:48:19 +00006760 if (V2IsSplat) {
6761 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006762 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006763 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006764 SDValue NewMask = NormalizeMask(SVOp, DAG);
6765 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6766 if (NSVOp != SVOp) {
6767 if (X86::isUNPCKLMask(NSVOp, true)) {
6768 return NewMask;
6769 } else if (X86::isUNPCKHMask(NSVOp, true)) {
6770 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006771 }
6772 }
6773 }
6774
Evan Cheng9eca5e82006-10-25 21:49:50 +00006775 if (Commuted) {
6776 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006777 // FIXME: this seems wrong.
6778 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6779 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006780
6781 if (X86::isUNPCKLMask(NewSVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006782 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006783
6784 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006785 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006786 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006787
Nate Begeman9008ca62009-04-27 18:41:29 +00006788 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006789 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006790 return CommuteVectorShuffle(SVOp, DAG);
6791
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006792 // The checks below are all present in isShuffleMaskLegal, but they are
6793 // inlined here right now to enable us to directly emit target specific
6794 // nodes, and remove one by one until they don't return Op anymore.
6795 SmallVector<int, 16> M;
6796 SVOp->getMask(M);
6797
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006798 if (isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006799 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6800 X86::getShufflePALIGNRImmediate(SVOp),
6801 DAG);
6802
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006803 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6804 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006805 if (VT == MVT::v2f64)
6806 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006807 if (VT == MVT::v2i64)
6808 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6809 }
6810
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006811 if (isPSHUFHWMask(M, VT))
6812 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6813 X86::getShufflePSHUFHWImmediate(SVOp),
6814 DAG);
6815
6816 if (isPSHUFLWMask(M, VT))
6817 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6818 X86::getShufflePSHUFLWImmediate(SVOp),
6819 DAG);
6820
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006821 if (isSHUFPMask(M, VT))
6822 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6823 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006824
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006825 if (X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006826 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006827 if (X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006828 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006829
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006830 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006831 // Generate target specific nodes for 128 or 256-bit shuffles only
6832 // supported in the AVX instruction set.
6833 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006834
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006835 // Handle VMOVDDUPY permutations
6836 if (isMOVDDUPYMask(SVOp, Subtarget))
6837 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6838
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006839 // Handle VPERMILPS* permutations
6840 if (isVPERMILPSMask(M, VT, Subtarget))
6841 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6842 getShuffleVPERMILPSImmediate(SVOp), DAG);
6843
6844 // Handle VPERMILPD* permutations
6845 if (isVPERMILPDMask(M, VT, Subtarget))
6846 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6847 getShuffleVPERMILPDImmediate(SVOp), DAG);
6848
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00006849 // Handle VPERM2F128 permutations
6850 if (isVPERM2F128Mask(M, VT, Subtarget))
6851 return getTargetShuffleNode(X86ISD::VPERM2F128, dl, VT, V1, V2,
6852 getShuffleVPERM2F128Immediate(SVOp), DAG);
6853
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006854 // Handle VSHUFPSY permutations
6855 if (isVSHUFPSYMask(M, VT, Subtarget))
6856 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6857 getShuffleVSHUFPSYImmediate(SVOp), DAG);
6858
6859 // Handle VSHUFPDY permutations
6860 if (isVSHUFPDYMask(M, VT, Subtarget))
6861 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6862 getShuffleVSHUFPDYImmediate(SVOp), DAG);
6863
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006864 //===--------------------------------------------------------------------===//
6865 // Since no target specific shuffle was selected for this generic one,
6866 // lower it into other known shuffles. FIXME: this isn't true yet, but
6867 // this is the plan.
6868 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006869
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006870 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6871 if (VT == MVT::v8i16) {
6872 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6873 if (NewOp.getNode())
6874 return NewOp;
6875 }
6876
6877 if (VT == MVT::v16i8) {
6878 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6879 if (NewOp.getNode())
6880 return NewOp;
6881 }
6882
6883 // Handle all 128-bit wide vectors with 4 elements, and match them with
6884 // several different shuffle types.
6885 if (NumElems == 4 && VT.getSizeInBits() == 128)
6886 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6887
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006888 // Handle general 256-bit shuffles
6889 if (VT.is256BitVector())
6890 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6891
Dan Gohman475871a2008-07-27 21:46:04 +00006892 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006893}
6894
Dan Gohman475871a2008-07-27 21:46:04 +00006895SDValue
6896X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006897 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006898 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006899 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006900
6901 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6902 return SDValue();
6903
Duncan Sands83ec4b62008-06-06 12:08:01 +00006904 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006905 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006906 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006907 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006908 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006909 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006910 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006911 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6912 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6913 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006914 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6915 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006916 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006917 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006918 Op.getOperand(0)),
6919 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006920 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006921 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006922 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006923 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006924 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006925 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006926 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6927 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006928 // result has a single use which is a store or a bitcast to i32. And in
6929 // the case of a store, it's not worth it if the index is a constant 0,
6930 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006931 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006932 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006933 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006934 if ((User->getOpcode() != ISD::STORE ||
6935 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6936 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006937 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006938 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006939 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006940 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006941 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006942 Op.getOperand(0)),
6943 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006944 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006945 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006946 // ExtractPS works with constant index.
6947 if (isa<ConstantSDNode>(Op.getOperand(1)))
6948 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006949 }
Dan Gohman475871a2008-07-27 21:46:04 +00006950 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006951}
6952
6953
Dan Gohman475871a2008-07-27 21:46:04 +00006954SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006955X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6956 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006957 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006958 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006959
David Greene74a579d2011-02-10 16:57:36 +00006960 SDValue Vec = Op.getOperand(0);
6961 EVT VecVT = Vec.getValueType();
6962
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006963 // If this is a 256-bit vector result, first extract the 128-bit vector and
6964 // then extract the element from the 128-bit vector.
6965 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006966 DebugLoc dl = Op.getNode()->getDebugLoc();
6967 unsigned NumElems = VecVT.getVectorNumElements();
6968 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006969 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6970
6971 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006972 bool Upper = IdxVal >= NumElems/2;
6973 Vec = Extract128BitVector(Vec,
6974 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006975
David Greene74a579d2011-02-10 16:57:36 +00006976 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006977 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006978 }
6979
6980 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6981
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006982 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006983 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006984 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006985 return Res;
6986 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006987
Owen Andersone50ed302009-08-10 22:56:29 +00006988 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006989 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006990 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006991 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006992 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006993 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006994 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006995 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6996 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006997 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006998 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006999 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007000 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007001 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007002 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007003 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007004 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007005 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007006 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007007 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007008 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007009 if (Idx == 0)
7010 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007011
Evan Cheng0db9fe62006-04-25 20:13:52 +00007012 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007013 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007014 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007015 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007016 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007017 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007018 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00007019 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007020 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7021 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7022 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007023 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007024 if (Idx == 0)
7025 return Op;
7026
7027 // UNPCKHPD the element to the lowest double word, then movsd.
7028 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7029 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007030 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007031 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007032 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007033 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007034 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007035 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007036 }
7037
Dan Gohman475871a2008-07-27 21:46:04 +00007038 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007039}
7040
Dan Gohman475871a2008-07-27 21:46:04 +00007041SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007042X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7043 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007044 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007045 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007046 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007047
Dan Gohman475871a2008-07-27 21:46:04 +00007048 SDValue N0 = Op.getOperand(0);
7049 SDValue N1 = Op.getOperand(1);
7050 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007051
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007052 if (VT.getSizeInBits() == 256)
7053 return SDValue();
7054
Dan Gohman8a55ce42009-09-23 21:02:20 +00007055 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007056 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007057 unsigned Opc;
7058 if (VT == MVT::v8i16)
7059 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007060 else if (VT == MVT::v16i8)
7061 Opc = X86ISD::PINSRB;
7062 else
7063 Opc = X86ISD::PINSRB;
7064
Nate Begeman14d12ca2008-02-11 04:19:36 +00007065 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7066 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007067 if (N1.getValueType() != MVT::i32)
7068 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7069 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007070 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007071 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007072 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007073 // Bits [7:6] of the constant are the source select. This will always be
7074 // zero here. The DAG Combiner may combine an extract_elt index into these
7075 // bits. For example (insert (extract, 3), 2) could be matched by putting
7076 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007077 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007078 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007079 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007080 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007081 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007082 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007083 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007084 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007085 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007086 // PINSR* works with constant index.
7087 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007088 }
Dan Gohman475871a2008-07-27 21:46:04 +00007089 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007090}
7091
Dan Gohman475871a2008-07-27 21:46:04 +00007092SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007093X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007094 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007095 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007096
David Greene6b381262011-02-09 15:32:06 +00007097 DebugLoc dl = Op.getDebugLoc();
7098 SDValue N0 = Op.getOperand(0);
7099 SDValue N1 = Op.getOperand(1);
7100 SDValue N2 = Op.getOperand(2);
7101
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007102 // If this is a 256-bit vector result, first extract the 128-bit vector,
7103 // insert the element into the extracted half and then place it back.
7104 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007105 if (!isa<ConstantSDNode>(N2))
7106 return SDValue();
7107
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007108 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007109 unsigned NumElems = VT.getVectorNumElements();
7110 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007111 bool Upper = IdxVal >= NumElems/2;
7112 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
7113 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007114
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007115 // Insert the element into the desired half.
7116 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
7117 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00007118
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007119 // Insert the changed part back to the 256-bit vector
7120 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007121 }
7122
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007123 if (Subtarget->hasSSE41() || Subtarget->hasAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007124 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7125
Dan Gohman8a55ce42009-09-23 21:02:20 +00007126 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007127 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007128
Dan Gohman8a55ce42009-09-23 21:02:20 +00007129 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007130 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7131 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007132 if (N1.getValueType() != MVT::i32)
7133 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7134 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007135 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007136 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007137 }
Dan Gohman475871a2008-07-27 21:46:04 +00007138 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007139}
7140
Dan Gohman475871a2008-07-27 21:46:04 +00007141SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007142X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007143 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007144 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007145 EVT OpVT = Op.getValueType();
7146
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007147 // If this is a 256-bit vector result, first insert into a 128-bit
7148 // vector and then insert into the 256-bit vector.
7149 if (OpVT.getSizeInBits() > 128) {
7150 // Insert into a 128-bit vector.
7151 EVT VT128 = EVT::getVectorVT(*Context,
7152 OpVT.getVectorElementType(),
7153 OpVT.getVectorNumElements() / 2);
7154
7155 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7156
7157 // Insert the 128-bit vector.
7158 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7159 DAG.getConstant(0, MVT::i32),
7160 DAG, dl);
7161 }
7162
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007163 if (Op.getValueType() == MVT::v1i64 &&
7164 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007165 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007166
Owen Anderson825b72b2009-08-11 20:47:22 +00007167 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007168 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7169 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007170 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007171 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007172}
7173
David Greene91585092011-01-26 15:38:49 +00007174// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7175// a simple subregister reference or explicit instructions to grab
7176// upper bits of a vector.
7177SDValue
7178X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7179 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007180 DebugLoc dl = Op.getNode()->getDebugLoc();
7181 SDValue Vec = Op.getNode()->getOperand(0);
7182 SDValue Idx = Op.getNode()->getOperand(1);
7183
7184 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7185 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7186 return Extract128BitVector(Vec, Idx, DAG, dl);
7187 }
David Greene91585092011-01-26 15:38:49 +00007188 }
7189 return SDValue();
7190}
7191
David Greenecfe33c42011-01-26 19:13:22 +00007192// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7193// simple superregister reference or explicit instructions to insert
7194// the upper bits of a vector.
7195SDValue
7196X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7197 if (Subtarget->hasAVX()) {
7198 DebugLoc dl = Op.getNode()->getDebugLoc();
7199 SDValue Vec = Op.getNode()->getOperand(0);
7200 SDValue SubVec = Op.getNode()->getOperand(1);
7201 SDValue Idx = Op.getNode()->getOperand(2);
7202
7203 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7204 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007205 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007206 }
7207 }
7208 return SDValue();
7209}
7210
Bill Wendling056292f2008-09-16 21:48:12 +00007211// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7212// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7213// one of the above mentioned nodes. It has to be wrapped because otherwise
7214// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7215// be used to form addressing mode. These wrapped nodes will be selected
7216// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007217SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007218X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007219 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007220
Chris Lattner41621a22009-06-26 19:22:52 +00007221 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7222 // global base reg.
7223 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007224 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007225 CodeModel::Model M = getTargetMachine().getCodeModel();
7226
Chris Lattner4f066492009-07-11 20:29:19 +00007227 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007228 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007229 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007230 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007231 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007232 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007233 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007234
Evan Cheng1606e8e2009-03-13 07:51:59 +00007235 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007236 CP->getAlignment(),
7237 CP->getOffset(), OpFlag);
7238 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007239 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007240 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007241 if (OpFlag) {
7242 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007243 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007244 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007245 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007246 }
7247
7248 return Result;
7249}
7250
Dan Gohmand858e902010-04-17 15:26:15 +00007251SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007252 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007253
Chris Lattner18c59872009-06-27 04:16:01 +00007254 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7255 // global base reg.
7256 unsigned char OpFlag = 0;
7257 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007258 CodeModel::Model M = getTargetMachine().getCodeModel();
7259
Chris Lattner4f066492009-07-11 20:29:19 +00007260 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007261 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007262 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007263 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007264 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007265 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007266 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007267
Chris Lattner18c59872009-06-27 04:16:01 +00007268 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7269 OpFlag);
7270 DebugLoc DL = JT->getDebugLoc();
7271 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007272
Chris Lattner18c59872009-06-27 04:16:01 +00007273 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007274 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007275 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7276 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007277 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007278 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007279
Chris Lattner18c59872009-06-27 04:16:01 +00007280 return Result;
7281}
7282
7283SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007284X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007285 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007286
Chris Lattner18c59872009-06-27 04:16:01 +00007287 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7288 // global base reg.
7289 unsigned char OpFlag = 0;
7290 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007291 CodeModel::Model M = getTargetMachine().getCodeModel();
7292
Chris Lattner4f066492009-07-11 20:29:19 +00007293 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007294 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7295 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7296 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007297 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007298 } else if (Subtarget->isPICStyleGOT()) {
7299 OpFlag = X86II::MO_GOT;
7300 } else if (Subtarget->isPICStyleStubPIC()) {
7301 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7302 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7303 OpFlag = X86II::MO_DARWIN_NONLAZY;
7304 }
Eric Christopherfd179292009-08-27 18:07:15 +00007305
Chris Lattner18c59872009-06-27 04:16:01 +00007306 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007307
Chris Lattner18c59872009-06-27 04:16:01 +00007308 DebugLoc DL = Op.getDebugLoc();
7309 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007310
7311
Chris Lattner18c59872009-06-27 04:16:01 +00007312 // With PIC, the address is actually $g + Offset.
7313 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007314 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007315 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7316 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007317 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007318 Result);
7319 }
Eric Christopherfd179292009-08-27 18:07:15 +00007320
Eli Friedman586272d2011-08-11 01:48:05 +00007321 // For symbols that require a load from a stub to get the address, emit the
7322 // load.
7323 if (isGlobalStubReference(OpFlag))
7324 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007325 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007326
Chris Lattner18c59872009-06-27 04:16:01 +00007327 return Result;
7328}
7329
Dan Gohman475871a2008-07-27 21:46:04 +00007330SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007331X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007332 // Create the TargetBlockAddressAddress node.
7333 unsigned char OpFlags =
7334 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007335 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007336 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007337 DebugLoc dl = Op.getDebugLoc();
7338 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7339 /*isTarget=*/true, OpFlags);
7340
Dan Gohmanf705adb2009-10-30 01:28:02 +00007341 if (Subtarget->isPICStyleRIPRel() &&
7342 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007343 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7344 else
7345 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007346
Dan Gohman29cbade2009-11-20 23:18:13 +00007347 // With PIC, the address is actually $g + Offset.
7348 if (isGlobalRelativeToPICBase(OpFlags)) {
7349 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7350 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7351 Result);
7352 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007353
7354 return Result;
7355}
7356
7357SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007358X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007359 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007360 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007361 // Create the TargetGlobalAddress node, folding in the constant
7362 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007363 unsigned char OpFlags =
7364 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007365 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007366 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007367 if (OpFlags == X86II::MO_NO_FLAG &&
7368 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007369 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007370 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007371 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007372 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007373 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007374 }
Eric Christopherfd179292009-08-27 18:07:15 +00007375
Chris Lattner4f066492009-07-11 20:29:19 +00007376 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007377 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007378 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7379 else
7380 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007381
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007382 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007383 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007384 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7385 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007386 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007387 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007388
Chris Lattner36c25012009-07-10 07:34:39 +00007389 // For globals that require a load from a stub to get the address, emit the
7390 // load.
7391 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007392 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007393 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007394
Dan Gohman6520e202008-10-18 02:06:02 +00007395 // If there was a non-zero offset that we didn't fold, create an explicit
7396 // addition for it.
7397 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007398 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007399 DAG.getConstant(Offset, getPointerTy()));
7400
Evan Cheng0db9fe62006-04-25 20:13:52 +00007401 return Result;
7402}
7403
Evan Chengda43bcf2008-09-24 00:05:32 +00007404SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007405X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007406 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007407 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007408 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007409}
7410
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007411static SDValue
7412GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007413 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007414 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007415 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007416 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007417 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007418 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007419 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007420 GA->getOffset(),
7421 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007422 if (InFlag) {
7423 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007424 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007425 } else {
7426 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007427 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007428 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007429
7430 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007431 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007432
Rafael Espindola15f1b662009-04-24 12:59:40 +00007433 SDValue Flag = Chain.getValue(1);
7434 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007435}
7436
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007437// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007438static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007439LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007440 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007441 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007442 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7443 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007444 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007445 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007446 InFlag = Chain.getValue(1);
7447
Chris Lattnerb903bed2009-06-26 21:20:29 +00007448 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007449}
7450
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007451// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007452static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007453LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007454 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007455 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7456 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007457}
7458
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007459// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7460// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007461static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007462 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007463 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007464 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007465
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007466 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7467 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7468 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007469
Michael J. Spencerec38de22010-10-10 22:04:20 +00007470 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007471 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007472 MachinePointerInfo(Ptr),
7473 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007474
Chris Lattnerb903bed2009-06-26 21:20:29 +00007475 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007476 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7477 // initialexec.
7478 unsigned WrapperKind = X86ISD::Wrapper;
7479 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007480 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007481 } else if (is64Bit) {
7482 assert(model == TLSModel::InitialExec);
7483 OperandFlags = X86II::MO_GOTTPOFF;
7484 WrapperKind = X86ISD::WrapperRIP;
7485 } else {
7486 assert(model == TLSModel::InitialExec);
7487 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007488 }
Eric Christopherfd179292009-08-27 18:07:15 +00007489
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007490 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7491 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007492 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007493 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007494 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007495 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007496
Rafael Espindola9a580232009-02-27 13:37:18 +00007497 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007498 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007499 MachinePointerInfo::getGOT(), false, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007500
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007501 // The address of the thread local variable is the add of the thread
7502 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007503 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007504}
7505
Dan Gohman475871a2008-07-27 21:46:04 +00007506SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007507X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007508
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007509 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007510 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007511
Eric Christopher30ef0e52010-06-03 04:07:48 +00007512 if (Subtarget->isTargetELF()) {
7513 // TODO: implement the "local dynamic" model
7514 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007515
Eric Christopher30ef0e52010-06-03 04:07:48 +00007516 // If GV is an alias then use the aliasee for determining
7517 // thread-localness.
7518 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7519 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007520
7521 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007522 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007523
Eric Christopher30ef0e52010-06-03 04:07:48 +00007524 switch (model) {
7525 case TLSModel::GeneralDynamic:
7526 case TLSModel::LocalDynamic: // not implemented
7527 if (Subtarget->is64Bit())
7528 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7529 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007530
Eric Christopher30ef0e52010-06-03 04:07:48 +00007531 case TLSModel::InitialExec:
7532 case TLSModel::LocalExec:
7533 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7534 Subtarget->is64Bit());
7535 }
7536 } else if (Subtarget->isTargetDarwin()) {
7537 // Darwin only has one model of TLS. Lower to that.
7538 unsigned char OpFlag = 0;
7539 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7540 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007541
Eric Christopher30ef0e52010-06-03 04:07:48 +00007542 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7543 // global base reg.
7544 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7545 !Subtarget->is64Bit();
7546 if (PIC32)
7547 OpFlag = X86II::MO_TLVP_PIC_BASE;
7548 else
7549 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007550 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007551 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007552 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007553 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007554 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007555
Eric Christopher30ef0e52010-06-03 04:07:48 +00007556 // With PIC32, the address is actually $g + Offset.
7557 if (PIC32)
7558 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7559 DAG.getNode(X86ISD::GlobalBaseReg,
7560 DebugLoc(), getPointerTy()),
7561 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007562
Eric Christopher30ef0e52010-06-03 04:07:48 +00007563 // Lowering the machine isd will make sure everything is in the right
7564 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007565 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007566 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007567 SDValue Args[] = { Chain, Offset };
7568 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007569
Eric Christopher30ef0e52010-06-03 04:07:48 +00007570 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7571 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7572 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007573
Eric Christopher30ef0e52010-06-03 04:07:48 +00007574 // And our return value (tls address) is in the standard call return value
7575 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007576 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007577 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7578 Chain.getValue(1));
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007579 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007580
Eric Christopher30ef0e52010-06-03 04:07:48 +00007581 assert(false &&
7582 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007583
Torok Edwinc23197a2009-07-14 16:55:14 +00007584 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007585 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007586}
7587
Evan Cheng0db9fe62006-04-25 20:13:52 +00007588
Nadav Rotem43012222011-05-11 08:12:09 +00007589/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007590/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007591SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007592 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007593 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007594 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007595 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007596 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007597 SDValue ShOpLo = Op.getOperand(0);
7598 SDValue ShOpHi = Op.getOperand(1);
7599 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007600 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007601 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007602 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007603
Dan Gohman475871a2008-07-27 21:46:04 +00007604 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007605 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007606 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7607 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007608 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007609 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7610 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007611 }
Evan Chenge3413162006-01-09 18:33:28 +00007612
Owen Anderson825b72b2009-08-11 20:47:22 +00007613 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7614 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007615 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007616 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007617
Dan Gohman475871a2008-07-27 21:46:04 +00007618 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007619 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007620 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7621 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007622
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007623 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007624 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7625 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007626 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007627 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7628 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007629 }
7630
Dan Gohman475871a2008-07-27 21:46:04 +00007631 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007632 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007633}
Evan Chenga3195e82006-01-12 22:54:21 +00007634
Dan Gohmand858e902010-04-17 15:26:15 +00007635SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7636 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007637 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007638
Dale Johannesen0488fb62010-09-30 23:57:10 +00007639 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007640 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007641
Owen Anderson825b72b2009-08-11 20:47:22 +00007642 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007643 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007644
Eli Friedman36df4992009-05-27 00:47:34 +00007645 // These are really Legal; return the operand so the caller accepts it as
7646 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007647 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007648 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007649 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007650 Subtarget->is64Bit()) {
7651 return Op;
7652 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007653
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007654 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007655 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007656 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007657 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007658 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007659 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007660 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007661 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007662 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007663 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7664}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007665
Owen Andersone50ed302009-08-10 22:56:29 +00007666SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007667 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007668 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007669 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007670 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007671 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007672 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007673 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007674 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007675 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007676 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007677
Chris Lattner492a43e2010-09-22 01:28:21 +00007678 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007679
Stuart Hastings84be9582011-06-02 15:57:11 +00007680 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7681 MachineMemOperand *MMO;
7682 if (FI) {
7683 int SSFI = FI->getIndex();
7684 MMO =
7685 DAG.getMachineFunction()
7686 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7687 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7688 } else {
7689 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7690 StackSlot = StackSlot.getOperand(1);
7691 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007692 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007693 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7694 X86ISD::FILD, DL,
7695 Tys, Ops, array_lengthof(Ops),
7696 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007697
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007698 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007699 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007700 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007701
7702 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7703 // shouldn't be necessary except that RFP cannot be live across
7704 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007705 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007706 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7707 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007708 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007709 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007710 SDValue Ops[] = {
7711 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7712 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007713 MachineMemOperand *MMO =
7714 DAG.getMachineFunction()
7715 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007716 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007717
Chris Lattner492a43e2010-09-22 01:28:21 +00007718 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7719 Ops, array_lengthof(Ops),
7720 Op.getValueType(), MMO);
7721 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007722 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007723 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007724 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007725
Evan Cheng0db9fe62006-04-25 20:13:52 +00007726 return Result;
7727}
7728
Bill Wendling8b8a6362009-01-17 03:56:04 +00007729// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007730SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7731 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007732 // This algorithm is not obvious. Here it is in C code, more or less:
7733 /*
7734 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7735 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7736 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007737
Bill Wendling8b8a6362009-01-17 03:56:04 +00007738 // Copy ints to xmm registers.
7739 __m128i xh = _mm_cvtsi32_si128( hi );
7740 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007741
Bill Wendling8b8a6362009-01-17 03:56:04 +00007742 // Combine into low half of a single xmm register.
7743 __m128i x = _mm_unpacklo_epi32( xh, xl );
7744 __m128d d;
7745 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007746
Bill Wendling8b8a6362009-01-17 03:56:04 +00007747 // Merge in appropriate exponents to give the integer bits the right
7748 // magnitude.
7749 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007750
Bill Wendling8b8a6362009-01-17 03:56:04 +00007751 // Subtract away the biases to deal with the IEEE-754 double precision
7752 // implicit 1.
7753 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007754
Bill Wendling8b8a6362009-01-17 03:56:04 +00007755 // All conversions up to here are exact. The correctly rounded result is
7756 // calculated using the current rounding mode using the following
7757 // horizontal add.
7758 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7759 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7760 // store doesn't really need to be here (except
7761 // maybe to zero the other double)
7762 return sd;
7763 }
7764 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007765
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007766 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007767 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007768
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007769 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007770 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007771 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7772 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7773 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7774 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007775 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007776 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007777
Bill Wendling8b8a6362009-01-17 03:56:04 +00007778 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007779 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007780 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007781 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007782 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007783 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007784 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007785
Owen Anderson825b72b2009-08-11 20:47:22 +00007786 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7787 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007788 Op.getOperand(0),
7789 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007790 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7791 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007792 Op.getOperand(0),
7793 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007794 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7795 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007796 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007797 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007798 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007799 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007800 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007801 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007802 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007803 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007804
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007805 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007806 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007807 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7808 DAG.getUNDEF(MVT::v2f64), ShufMask);
7809 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7810 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007811 DAG.getIntPtrConstant(0));
7812}
7813
Bill Wendling8b8a6362009-01-17 03:56:04 +00007814// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007815SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7816 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007817 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007818 // FP constant to bias correct the final result.
7819 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007820 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007821
7822 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007823 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007824 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007825
Eli Friedmanf3704762011-08-29 21:15:46 +00007826 // Zero out the upper parts of the register.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00007827 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasXMMInt(),
7828 DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007829
Owen Anderson825b72b2009-08-11 20:47:22 +00007830 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007831 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007832 DAG.getIntPtrConstant(0));
7833
7834 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007835 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007836 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007837 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007838 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007839 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007840 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007841 MVT::v2f64, Bias)));
7842 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007843 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007844 DAG.getIntPtrConstant(0));
7845
7846 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007847 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007848
7849 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007850 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007851
Owen Anderson825b72b2009-08-11 20:47:22 +00007852 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007853 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007854 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007855 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007856 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007857 }
7858
7859 // Handle final rounding.
7860 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007861}
7862
Dan Gohmand858e902010-04-17 15:26:15 +00007863SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7864 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007865 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007866 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007867
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007868 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007869 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7870 // the optimization here.
7871 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007872 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007873
Owen Andersone50ed302009-08-10 22:56:29 +00007874 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007875 EVT DstVT = Op.getValueType();
7876 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007877 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007878 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007879 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007880
7881 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007882 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007883 if (SrcVT == MVT::i32) {
7884 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7885 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7886 getPointerTy(), StackSlot, WordOff);
7887 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007888 StackSlot, MachinePointerInfo(),
7889 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007890 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007891 OffsetSlot, MachinePointerInfo(),
7892 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007893 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7894 return Fild;
7895 }
7896
7897 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7898 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007899 StackSlot, MachinePointerInfo(),
7900 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007901 // For i64 source, we need to add the appropriate power of 2 if the input
7902 // was negative. This is the same as the optimization in
7903 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7904 // we must be careful to do the computation in x87 extended precision, not
7905 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007906 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7907 MachineMemOperand *MMO =
7908 DAG.getMachineFunction()
7909 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7910 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007911
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007912 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7913 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007914 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7915 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007916
7917 APInt FF(32, 0x5F800000ULL);
7918
7919 // Check whether the sign bit is set.
7920 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7921 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7922 ISD::SETLT);
7923
7924 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7925 SDValue FudgePtr = DAG.getConstantPool(
7926 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7927 getPointerTy());
7928
7929 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7930 SDValue Zero = DAG.getIntPtrConstant(0);
7931 SDValue Four = DAG.getIntPtrConstant(4);
7932 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7933 Zero, Four);
7934 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7935
7936 // Load the value out, extending it from f32 to f80.
7937 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007938 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007939 FudgePtr, MachinePointerInfo::getConstantPool(),
7940 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007941 // Extend everything to 80 bits to force it to be done on x87.
7942 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7943 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007944}
7945
Dan Gohman475871a2008-07-27 21:46:04 +00007946std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007947FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007948 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007949
Owen Andersone50ed302009-08-10 22:56:29 +00007950 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007951
7952 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007953 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7954 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007955 }
7956
Owen Anderson825b72b2009-08-11 20:47:22 +00007957 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7958 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007959 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007960
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007961 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007962 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007963 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007964 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007965 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007966 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007967 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007968 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007969
Evan Cheng87c89352007-10-15 20:11:21 +00007970 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7971 // stack slot.
7972 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007973 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007974 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007975 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007976
Michael J. Spencerec38de22010-10-10 22:04:20 +00007977
7978
Evan Cheng0db9fe62006-04-25 20:13:52 +00007979 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007980 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007981 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007982 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7983 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7984 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007985 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007986
Dan Gohman475871a2008-07-27 21:46:04 +00007987 SDValue Chain = DAG.getEntryNode();
7988 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007989 EVT TheVT = Op.getOperand(0).getValueType();
7990 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007991 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007992 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007993 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007994 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007995 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007996 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007997 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007998 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007999
Chris Lattner492a43e2010-09-22 01:28:21 +00008000 MachineMemOperand *MMO =
8001 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8002 MachineMemOperand::MOLoad, MemSize, MemSize);
8003 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8004 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008005 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008006 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008007 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8008 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008009
Chris Lattner07290932010-09-22 01:05:16 +00008010 MachineMemOperand *MMO =
8011 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8012 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008013
Evan Cheng0db9fe62006-04-25 20:13:52 +00008014 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00008015 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00008016 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8017 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00008018
Chris Lattner27a6c732007-11-24 07:07:01 +00008019 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008020}
8021
Dan Gohmand858e902010-04-17 15:26:15 +00008022SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8023 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00008024 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008025 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008026
Eli Friedman948e95a2009-05-23 09:59:16 +00008027 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00008028 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008029 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8030 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008031
Chris Lattner27a6c732007-11-24 07:07:01 +00008032 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008033 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008034 FIST, StackSlot, MachinePointerInfo(),
8035 false, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00008036}
8037
Dan Gohmand858e902010-04-17 15:26:15 +00008038SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8039 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00008040 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
8041 SDValue FIST = Vals.first, StackSlot = Vals.second;
8042 assert(FIST.getNode() && "Unexpected failure");
8043
8044 // Load the result.
8045 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008046 FIST, StackSlot, MachinePointerInfo(),
8047 false, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00008048}
8049
Dan Gohmand858e902010-04-17 15:26:15 +00008050SDValue X86TargetLowering::LowerFABS(SDValue Op,
8051 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008052 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008053 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008054 EVT VT = Op.getValueType();
8055 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008056 if (VT.isVector())
8057 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008058 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008059 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008060 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00008061 CV.push_back(C);
8062 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008063 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008064 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00008065 CV.push_back(C);
8066 CV.push_back(C);
8067 CV.push_back(C);
8068 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008069 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008070 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008071 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008072 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008073 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008074 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008075 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008076}
8077
Dan Gohmand858e902010-04-17 15:26:15 +00008078SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008079 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008080 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008081 EVT VT = Op.getValueType();
8082 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00008083 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008084 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008085 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008086 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008087 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00008088 CV.push_back(C);
8089 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008090 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008091 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00008092 CV.push_back(C);
8093 CV.push_back(C);
8094 CV.push_back(C);
8095 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008096 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008097 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008098 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008099 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008100 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008101 false, false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008102 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008103 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008104 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008105 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008106 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008107 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008108 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008109 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00008110 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008111}
8112
Dan Gohmand858e902010-04-17 15:26:15 +00008113SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008114 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008115 SDValue Op0 = Op.getOperand(0);
8116 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008117 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008118 EVT VT = Op.getValueType();
8119 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008120
8121 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008122 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008123 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008124 SrcVT = VT;
8125 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008126 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008127 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008128 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008129 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008130 }
8131
8132 // At this point the operands and the result should have the same
8133 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008134
Evan Cheng68c47cb2007-01-05 07:55:56 +00008135 // First get the sign bit of second operand.
8136 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008137 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008138 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8139 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008140 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008141 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8142 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8143 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8144 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008145 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008146 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008147 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008148 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008149 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008150 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008151 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008152
8153 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008154 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008155 // Op0 is MVT::f32, Op1 is MVT::f64.
8156 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8157 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8158 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008159 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008160 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008161 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008162 }
8163
Evan Cheng73d6cf12007-01-05 21:37:56 +00008164 // Clear first operand sign bit.
8165 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008166 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008167 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8168 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008169 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008170 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8171 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8172 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8173 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008174 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008175 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008176 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008177 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008178 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008179 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008180 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008181
8182 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008183 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008184}
8185
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008186SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8187 SDValue N0 = Op.getOperand(0);
8188 DebugLoc dl = Op.getDebugLoc();
8189 EVT VT = Op.getValueType();
8190
8191 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8192 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8193 DAG.getConstant(1, VT));
8194 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8195}
8196
Dan Gohman076aee32009-03-04 19:44:21 +00008197/// Emit nodes that will be selected as "test Op0,Op0", or something
8198/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008199SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008200 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008201 DebugLoc dl = Op.getDebugLoc();
8202
Dan Gohman31125812009-03-07 01:58:32 +00008203 // CF and OF aren't always set the way we want. Determine which
8204 // of these we need.
8205 bool NeedCF = false;
8206 bool NeedOF = false;
8207 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008208 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008209 case X86::COND_A: case X86::COND_AE:
8210 case X86::COND_B: case X86::COND_BE:
8211 NeedCF = true;
8212 break;
8213 case X86::COND_G: case X86::COND_GE:
8214 case X86::COND_L: case X86::COND_LE:
8215 case X86::COND_O: case X86::COND_NO:
8216 NeedOF = true;
8217 break;
Dan Gohman31125812009-03-07 01:58:32 +00008218 }
8219
Dan Gohman076aee32009-03-04 19:44:21 +00008220 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008221 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8222 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008223 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8224 // Emit a CMP with 0, which is the TEST pattern.
8225 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8226 DAG.getConstant(0, Op.getValueType()));
8227
8228 unsigned Opcode = 0;
8229 unsigned NumOperands = 0;
8230 switch (Op.getNode()->getOpcode()) {
8231 case ISD::ADD:
8232 // Due to an isel shortcoming, be conservative if this add is likely to be
8233 // selected as part of a load-modify-store instruction. When the root node
8234 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8235 // uses of other nodes in the match, such as the ADD in this case. This
8236 // leads to the ADD being left around and reselected, with the result being
8237 // two adds in the output. Alas, even if none our users are stores, that
8238 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8239 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8240 // climbing the DAG back to the root, and it doesn't seem to be worth the
8241 // effort.
8242 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00008243 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008244 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
8245 goto default_case;
8246
8247 if (ConstantSDNode *C =
8248 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8249 // An add of one will be selected as an INC.
8250 if (C->getAPIntValue() == 1) {
8251 Opcode = X86ISD::INC;
8252 NumOperands = 1;
8253 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008254 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008255
8256 // An add of negative one (subtract of one) will be selected as a DEC.
8257 if (C->getAPIntValue().isAllOnesValue()) {
8258 Opcode = X86ISD::DEC;
8259 NumOperands = 1;
8260 break;
8261 }
Dan Gohman076aee32009-03-04 19:44:21 +00008262 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008263
8264 // Otherwise use a regular EFLAGS-setting add.
8265 Opcode = X86ISD::ADD;
8266 NumOperands = 2;
8267 break;
8268 case ISD::AND: {
8269 // If the primary and result isn't used, don't bother using X86ISD::AND,
8270 // because a TEST instruction will be better.
8271 bool NonFlagUse = false;
8272 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8273 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8274 SDNode *User = *UI;
8275 unsigned UOpNo = UI.getOperandNo();
8276 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8277 // Look pass truncate.
8278 UOpNo = User->use_begin().getOperandNo();
8279 User = *User->use_begin();
8280 }
8281
8282 if (User->getOpcode() != ISD::BRCOND &&
8283 User->getOpcode() != ISD::SETCC &&
8284 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8285 NonFlagUse = true;
8286 break;
8287 }
Dan Gohman076aee32009-03-04 19:44:21 +00008288 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008289
8290 if (!NonFlagUse)
8291 break;
8292 }
8293 // FALL THROUGH
8294 case ISD::SUB:
8295 case ISD::OR:
8296 case ISD::XOR:
8297 // Due to the ISEL shortcoming noted above, be conservative if this op is
8298 // likely to be selected as part of a load-modify-store instruction.
8299 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8300 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8301 if (UI->getOpcode() == ISD::STORE)
8302 goto default_case;
8303
8304 // Otherwise use a regular EFLAGS-setting instruction.
8305 switch (Op.getNode()->getOpcode()) {
8306 default: llvm_unreachable("unexpected operator!");
8307 case ISD::SUB: Opcode = X86ISD::SUB; break;
8308 case ISD::OR: Opcode = X86ISD::OR; break;
8309 case ISD::XOR: Opcode = X86ISD::XOR; break;
8310 case ISD::AND: Opcode = X86ISD::AND; break;
8311 }
8312
8313 NumOperands = 2;
8314 break;
8315 case X86ISD::ADD:
8316 case X86ISD::SUB:
8317 case X86ISD::INC:
8318 case X86ISD::DEC:
8319 case X86ISD::OR:
8320 case X86ISD::XOR:
8321 case X86ISD::AND:
8322 return SDValue(Op.getNode(), 1);
8323 default:
8324 default_case:
8325 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008326 }
8327
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008328 if (Opcode == 0)
8329 // Emit a CMP with 0, which is the TEST pattern.
8330 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8331 DAG.getConstant(0, Op.getValueType()));
8332
8333 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8334 SmallVector<SDValue, 4> Ops;
8335 for (unsigned i = 0; i != NumOperands; ++i)
8336 Ops.push_back(Op.getOperand(i));
8337
8338 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8339 DAG.ReplaceAllUsesWith(Op, New);
8340 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008341}
8342
8343/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8344/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008345SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008346 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008347 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8348 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008349 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008350
8351 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008352 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008353}
8354
Evan Chengd40d03e2010-01-06 19:38:29 +00008355/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8356/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008357SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8358 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008359 SDValue Op0 = And.getOperand(0);
8360 SDValue Op1 = And.getOperand(1);
8361 if (Op0.getOpcode() == ISD::TRUNCATE)
8362 Op0 = Op0.getOperand(0);
8363 if (Op1.getOpcode() == ISD::TRUNCATE)
8364 Op1 = Op1.getOperand(0);
8365
Evan Chengd40d03e2010-01-06 19:38:29 +00008366 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008367 if (Op1.getOpcode() == ISD::SHL)
8368 std::swap(Op0, Op1);
8369 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008370 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8371 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008372 // If we looked past a truncate, check that it's only truncating away
8373 // known zeros.
8374 unsigned BitWidth = Op0.getValueSizeInBits();
8375 unsigned AndBitWidth = And.getValueSizeInBits();
8376 if (BitWidth > AndBitWidth) {
8377 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8378 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8379 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8380 return SDValue();
8381 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008382 LHS = Op1;
8383 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008384 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008385 } else if (Op1.getOpcode() == ISD::Constant) {
8386 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
8387 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00008388 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
8389 LHS = AndLHS.getOperand(0);
8390 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008391 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008392 }
Evan Cheng0488db92007-09-25 01:57:46 +00008393
Evan Chengd40d03e2010-01-06 19:38:29 +00008394 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008395 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008396 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008397 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008398 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008399 // Also promote i16 to i32 for performance / code size reason.
8400 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008401 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008402 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008403
Evan Chengd40d03e2010-01-06 19:38:29 +00008404 // If the operand types disagree, extend the shift amount to match. Since
8405 // BT ignores high bits (like shifts) we can use anyextend.
8406 if (LHS.getValueType() != RHS.getValueType())
8407 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008408
Evan Chengd40d03e2010-01-06 19:38:29 +00008409 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8410 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8411 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8412 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008413 }
8414
Evan Cheng54de3ea2010-01-05 06:52:31 +00008415 return SDValue();
8416}
8417
Dan Gohmand858e902010-04-17 15:26:15 +00008418SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008419
8420 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8421
Evan Cheng54de3ea2010-01-05 06:52:31 +00008422 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8423 SDValue Op0 = Op.getOperand(0);
8424 SDValue Op1 = Op.getOperand(1);
8425 DebugLoc dl = Op.getDebugLoc();
8426 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8427
8428 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008429 // Lower (X & (1 << N)) == 0 to BT(X, N).
8430 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8431 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008432 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008433 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008434 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008435 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8436 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8437 if (NewSetCC.getNode())
8438 return NewSetCC;
8439 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008440
Chris Lattner481eebc2010-12-19 21:23:48 +00008441 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8442 // these.
8443 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008444 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008445 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8446 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008447
Chris Lattner481eebc2010-12-19 21:23:48 +00008448 // If the input is a setcc, then reuse the input setcc or use a new one with
8449 // the inverted condition.
8450 if (Op0.getOpcode() == X86ISD::SETCC) {
8451 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8452 bool Invert = (CC == ISD::SETNE) ^
8453 cast<ConstantSDNode>(Op1)->isNullValue();
8454 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008455
Evan Cheng2c755ba2010-02-27 07:36:59 +00008456 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008457 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8458 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8459 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008460 }
8461
Evan Chenge5b51ac2010-04-17 06:13:15 +00008462 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008463 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008464 if (X86CC == X86::COND_INVALID)
8465 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008466
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008467 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008468 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008469 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008470}
8471
Craig Topper89af15e2011-09-18 08:03:58 +00008472// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008473// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008474static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008475 EVT VT = Op.getValueType();
8476
Duncan Sands28b77e92011-09-06 19:07:46 +00008477 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008478 "Unsupported value type for operation");
8479
8480 int NumElems = VT.getVectorNumElements();
8481 DebugLoc dl = Op.getDebugLoc();
8482 SDValue CC = Op.getOperand(2);
8483 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8484 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8485
8486 // Extract the LHS vectors
8487 SDValue LHS = Op.getOperand(0);
8488 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8489 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8490
8491 // Extract the RHS vectors
8492 SDValue RHS = Op.getOperand(1);
8493 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8494 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8495
8496 // Issue the operation on the smaller types and concatenate the result back
8497 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8498 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8499 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8500 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8501 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8502}
8503
8504
Dan Gohmand858e902010-04-17 15:26:15 +00008505SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008506 SDValue Cond;
8507 SDValue Op0 = Op.getOperand(0);
8508 SDValue Op1 = Op.getOperand(1);
8509 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008510 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008511 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8512 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008513 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008514
8515 if (isFP) {
8516 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008517 EVT EltVT = Op0.getValueType().getVectorElementType();
8518 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8519
8520 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008521 bool Swap = false;
8522
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008523 // SSE Condition code mapping:
8524 // 0 - EQ
8525 // 1 - LT
8526 // 2 - LE
8527 // 3 - UNORD
8528 // 4 - NEQ
8529 // 5 - NLT
8530 // 6 - NLE
8531 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008532 switch (SetCCOpcode) {
8533 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008534 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008535 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008536 case ISD::SETOGT:
8537 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008538 case ISD::SETLT:
8539 case ISD::SETOLT: SSECC = 1; break;
8540 case ISD::SETOGE:
8541 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008542 case ISD::SETLE:
8543 case ISD::SETOLE: SSECC = 2; break;
8544 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008545 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008546 case ISD::SETNE: SSECC = 4; break;
8547 case ISD::SETULE: Swap = true;
8548 case ISD::SETUGE: SSECC = 5; break;
8549 case ISD::SETULT: Swap = true;
8550 case ISD::SETUGT: SSECC = 6; break;
8551 case ISD::SETO: SSECC = 7; break;
8552 }
8553 if (Swap)
8554 std::swap(Op0, Op1);
8555
Nate Begemanfb8ead02008-07-25 19:05:58 +00008556 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008557 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008558 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008559 SDValue UNORD, EQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008560 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8561 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008562 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008563 }
8564 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008565 SDValue ORD, NEQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008566 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8567 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008568 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008569 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008570 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008571 }
8572 // Handle all other FP comparisons here.
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008573 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008574 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008575
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008576 // Break 256-bit integer vector compare into smaller ones.
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008577 if (!isFP && VT.getSizeInBits() == 256)
Craig Topper89af15e2011-09-18 08:03:58 +00008578 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008579
Nate Begeman30a0de92008-07-17 16:51:19 +00008580 // We are handling one of the integer comparisons here. Since SSE only has
8581 // GT and EQ comparisons for integer, swapping operands and multiple
8582 // operations may be required for some comparisons.
8583 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8584 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008585
Owen Anderson825b72b2009-08-11 20:47:22 +00008586 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008587 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008588 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008589 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008590 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8591 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008592 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008593
Nate Begeman30a0de92008-07-17 16:51:19 +00008594 switch (SetCCOpcode) {
8595 default: break;
8596 case ISD::SETNE: Invert = true;
8597 case ISD::SETEQ: Opc = EQOpc; break;
8598 case ISD::SETLT: Swap = true;
8599 case ISD::SETGT: Opc = GTOpc; break;
8600 case ISD::SETGE: Swap = true;
8601 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8602 case ISD::SETULT: Swap = true;
8603 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8604 case ISD::SETUGE: Swap = true;
8605 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8606 }
8607 if (Swap)
8608 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008609
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008610 // Check that the operation in question is available (most are plain SSE2,
8611 // but PCMPGTQ and PCMPEQQ have different requirements).
8612 if (Opc == X86ISD::PCMPGTQ && !Subtarget->hasSSE42() && !Subtarget->hasAVX())
8613 return SDValue();
8614 if (Opc == X86ISD::PCMPEQQ && !Subtarget->hasSSE41() && !Subtarget->hasAVX())
8615 return SDValue();
8616
Nate Begeman30a0de92008-07-17 16:51:19 +00008617 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8618 // bits of the inputs before performing those operations.
8619 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008620 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008621 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8622 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008623 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008624 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8625 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008626 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8627 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008628 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008629
Dale Johannesenace16102009-02-03 19:33:06 +00008630 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008631
8632 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008633 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008634 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008635
Nate Begeman30a0de92008-07-17 16:51:19 +00008636 return Result;
8637}
Evan Cheng0488db92007-09-25 01:57:46 +00008638
Evan Cheng370e5342008-12-03 08:38:43 +00008639// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008640static bool isX86LogicalCmp(SDValue Op) {
8641 unsigned Opc = Op.getNode()->getOpcode();
8642 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8643 return true;
8644 if (Op.getResNo() == 1 &&
8645 (Opc == X86ISD::ADD ||
8646 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008647 Opc == X86ISD::ADC ||
8648 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008649 Opc == X86ISD::SMUL ||
8650 Opc == X86ISD::UMUL ||
8651 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008652 Opc == X86ISD::DEC ||
8653 Opc == X86ISD::OR ||
8654 Opc == X86ISD::XOR ||
8655 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008656 return true;
8657
Chris Lattner9637d5b2010-12-05 07:49:54 +00008658 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8659 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008660
Dan Gohman076aee32009-03-04 19:44:21 +00008661 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008662}
8663
Chris Lattnera2b56002010-12-05 01:23:24 +00008664static bool isZero(SDValue V) {
8665 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8666 return C && C->isNullValue();
8667}
8668
Chris Lattner96908b12010-12-05 02:00:51 +00008669static bool isAllOnes(SDValue V) {
8670 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8671 return C && C->isAllOnesValue();
8672}
8673
Dan Gohmand858e902010-04-17 15:26:15 +00008674SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008675 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008676 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008677 SDValue Op1 = Op.getOperand(1);
8678 SDValue Op2 = Op.getOperand(2);
8679 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008680 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008681
Dan Gohman1a492952009-10-20 16:22:37 +00008682 if (Cond.getOpcode() == ISD::SETCC) {
8683 SDValue NewCond = LowerSETCC(Cond, DAG);
8684 if (NewCond.getNode())
8685 Cond = NewCond;
8686 }
Evan Cheng734503b2006-09-11 02:19:56 +00008687
Chris Lattnera2b56002010-12-05 01:23:24 +00008688 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008689 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008690 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008691 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008692 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008693 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8694 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008695 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008696
Chris Lattnera2b56002010-12-05 01:23:24 +00008697 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008698
8699 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008700 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8701 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008702
8703 SDValue CmpOp0 = Cmp.getOperand(0);
8704 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8705 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008706
Chris Lattner96908b12010-12-05 02:00:51 +00008707 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008708 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8709 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008710
Chris Lattner96908b12010-12-05 02:00:51 +00008711 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8712 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008713
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008714 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008715 if (N2C == 0 || !N2C->isNullValue())
8716 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8717 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008718 }
8719 }
8720
Chris Lattnera2b56002010-12-05 01:23:24 +00008721 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008722 if (Cond.getOpcode() == ISD::AND &&
8723 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8724 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008725 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008726 Cond = Cond.getOperand(0);
8727 }
8728
Evan Cheng3f41d662007-10-08 22:16:29 +00008729 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8730 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008731 unsigned CondOpcode = Cond.getOpcode();
8732 if (CondOpcode == X86ISD::SETCC ||
8733 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008734 CC = Cond.getOperand(0);
8735
Dan Gohman475871a2008-07-27 21:46:04 +00008736 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008737 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008738 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008739
Evan Cheng3f41d662007-10-08 22:16:29 +00008740 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008741 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008742 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008743 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008744
Chris Lattnerd1980a52009-03-12 06:52:53 +00008745 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8746 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008747 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008748 addTest = false;
8749 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008750 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8751 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8752 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8753 Cond.getOperand(0).getValueType() != MVT::i8)) {
8754 SDValue LHS = Cond.getOperand(0);
8755 SDValue RHS = Cond.getOperand(1);
8756 unsigned X86Opcode;
8757 unsigned X86Cond;
8758 SDVTList VTs;
8759 switch (CondOpcode) {
8760 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8761 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8762 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8763 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8764 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8765 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8766 default: llvm_unreachable("unexpected overflowing operator");
8767 }
8768 if (CondOpcode == ISD::UMULO)
8769 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8770 MVT::i32);
8771 else
8772 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8773
8774 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
8775
8776 if (CondOpcode == ISD::UMULO)
8777 Cond = X86Op.getValue(2);
8778 else
8779 Cond = X86Op.getValue(1);
8780
8781 CC = DAG.getConstant(X86Cond, MVT::i8);
8782 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00008783 }
8784
8785 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008786 // Look pass the truncate.
8787 if (Cond.getOpcode() == ISD::TRUNCATE)
8788 Cond = Cond.getOperand(0);
8789
8790 // We know the result of AND is compared against zero. Try to match
8791 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008792 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008793 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008794 if (NewSetCC.getNode()) {
8795 CC = NewSetCC.getOperand(0);
8796 Cond = NewSetCC.getOperand(1);
8797 addTest = false;
8798 }
8799 }
8800 }
8801
8802 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008803 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008804 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008805 }
8806
Benjamin Kramere915ff32010-12-22 23:09:28 +00008807 // a < b ? -1 : 0 -> RES = ~setcc_carry
8808 // a < b ? 0 : -1 -> RES = setcc_carry
8809 // a >= b ? -1 : 0 -> RES = setcc_carry
8810 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8811 if (Cond.getOpcode() == X86ISD::CMP) {
8812 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8813
8814 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8815 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8816 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8817 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8818 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8819 return DAG.getNOT(DL, Res, Res.getValueType());
8820 return Res;
8821 }
8822 }
8823
Evan Cheng0488db92007-09-25 01:57:46 +00008824 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8825 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008826 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008827 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008828 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008829}
8830
Evan Cheng370e5342008-12-03 08:38:43 +00008831// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8832// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8833// from the AND / OR.
8834static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8835 Opc = Op.getOpcode();
8836 if (Opc != ISD::OR && Opc != ISD::AND)
8837 return false;
8838 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8839 Op.getOperand(0).hasOneUse() &&
8840 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8841 Op.getOperand(1).hasOneUse());
8842}
8843
Evan Cheng961d6d42009-02-02 08:19:07 +00008844// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8845// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008846static bool isXor1OfSetCC(SDValue Op) {
8847 if (Op.getOpcode() != ISD::XOR)
8848 return false;
8849 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8850 if (N1C && N1C->getAPIntValue() == 1) {
8851 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8852 Op.getOperand(0).hasOneUse();
8853 }
8854 return false;
8855}
8856
Dan Gohmand858e902010-04-17 15:26:15 +00008857SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008858 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008859 SDValue Chain = Op.getOperand(0);
8860 SDValue Cond = Op.getOperand(1);
8861 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008862 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008863 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00008864 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00008865
Dan Gohman1a492952009-10-20 16:22:37 +00008866 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00008867 // Check for setcc([su]{add,sub,mul}o == 0).
8868 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
8869 isa<ConstantSDNode>(Cond.getOperand(1)) &&
8870 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
8871 Cond.getOperand(0).getResNo() == 1 &&
8872 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
8873 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
8874 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
8875 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
8876 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
8877 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
8878 Inverted = true;
8879 Cond = Cond.getOperand(0);
8880 } else {
8881 SDValue NewCond = LowerSETCC(Cond, DAG);
8882 if (NewCond.getNode())
8883 Cond = NewCond;
8884 }
Dan Gohman1a492952009-10-20 16:22:37 +00008885 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008886#if 0
8887 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008888 else if (Cond.getOpcode() == X86ISD::ADD ||
8889 Cond.getOpcode() == X86ISD::SUB ||
8890 Cond.getOpcode() == X86ISD::SMUL ||
8891 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008892 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008893#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008894
Evan Chengad9c0a32009-12-15 00:53:42 +00008895 // Look pass (and (setcc_carry (cmp ...)), 1).
8896 if (Cond.getOpcode() == ISD::AND &&
8897 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8898 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008899 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008900 Cond = Cond.getOperand(0);
8901 }
8902
Evan Cheng3f41d662007-10-08 22:16:29 +00008903 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8904 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008905 unsigned CondOpcode = Cond.getOpcode();
8906 if (CondOpcode == X86ISD::SETCC ||
8907 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008908 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008909
Dan Gohman475871a2008-07-27 21:46:04 +00008910 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008911 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008912 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008913 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008914 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008915 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008916 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008917 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008918 default: break;
8919 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008920 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008921 // These can only come from an arithmetic instruction with overflow,
8922 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008923 Cond = Cond.getNode()->getOperand(1);
8924 addTest = false;
8925 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008926 }
Evan Cheng0488db92007-09-25 01:57:46 +00008927 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008928 }
8929 CondOpcode = Cond.getOpcode();
8930 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8931 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8932 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8933 Cond.getOperand(0).getValueType() != MVT::i8)) {
8934 SDValue LHS = Cond.getOperand(0);
8935 SDValue RHS = Cond.getOperand(1);
8936 unsigned X86Opcode;
8937 unsigned X86Cond;
8938 SDVTList VTs;
8939 switch (CondOpcode) {
8940 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8941 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8942 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8943 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8944 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8945 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8946 default: llvm_unreachable("unexpected overflowing operator");
8947 }
8948 if (Inverted)
8949 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
8950 if (CondOpcode == ISD::UMULO)
8951 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8952 MVT::i32);
8953 else
8954 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8955
8956 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
8957
8958 if (CondOpcode == ISD::UMULO)
8959 Cond = X86Op.getValue(2);
8960 else
8961 Cond = X86Op.getValue(1);
8962
8963 CC = DAG.getConstant(X86Cond, MVT::i8);
8964 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00008965 } else {
8966 unsigned CondOpc;
8967 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8968 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008969 if (CondOpc == ISD::OR) {
8970 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8971 // two branches instead of an explicit OR instruction with a
8972 // separate test.
8973 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008974 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008975 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008976 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008977 Chain, Dest, CC, Cmp);
8978 CC = Cond.getOperand(1).getOperand(0);
8979 Cond = Cmp;
8980 addTest = false;
8981 }
8982 } else { // ISD::AND
8983 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8984 // two branches instead of an explicit AND instruction with a
8985 // separate test. However, we only do this if this block doesn't
8986 // have a fall-through edge, because this requires an explicit
8987 // jmp when the condition is false.
8988 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008989 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008990 Op.getNode()->hasOneUse()) {
8991 X86::CondCode CCode =
8992 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8993 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008994 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008995 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008996 // Look for an unconditional branch following this conditional branch.
8997 // We need this because we need to reverse the successors in order
8998 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008999 if (User->getOpcode() == ISD::BR) {
9000 SDValue FalseBB = User->getOperand(1);
9001 SDNode *NewBR =
9002 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009003 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009004 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009005 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009006
Dale Johannesene4d209d2009-02-03 20:21:25 +00009007 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009008 Chain, Dest, CC, Cmp);
9009 X86::CondCode CCode =
9010 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9011 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009012 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009013 Cond = Cmp;
9014 addTest = false;
9015 }
9016 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009017 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009018 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9019 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9020 // It should be transformed during dag combiner except when the condition
9021 // is set by a arithmetics with overflow node.
9022 X86::CondCode CCode =
9023 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9024 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009025 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009026 Cond = Cond.getOperand(0).getOperand(1);
9027 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009028 } else if (Cond.getOpcode() == ISD::SETCC &&
9029 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9030 // For FCMP_OEQ, we can emit
9031 // two branches instead of an explicit AND instruction with a
9032 // separate test. However, we only do this if this block doesn't
9033 // have a fall-through edge, because this requires an explicit
9034 // jmp when the condition is false.
9035 if (Op.getNode()->hasOneUse()) {
9036 SDNode *User = *Op.getNode()->use_begin();
9037 // Look for an unconditional branch following this conditional branch.
9038 // We need this because we need to reverse the successors in order
9039 // to implement FCMP_OEQ.
9040 if (User->getOpcode() == ISD::BR) {
9041 SDValue FalseBB = User->getOperand(1);
9042 SDNode *NewBR =
9043 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9044 assert(NewBR == User);
9045 (void)NewBR;
9046 Dest = FalseBB;
9047
9048 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9049 Cond.getOperand(0), Cond.getOperand(1));
9050 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9051 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9052 Chain, Dest, CC, Cmp);
9053 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9054 Cond = Cmp;
9055 addTest = false;
9056 }
9057 }
9058 } else if (Cond.getOpcode() == ISD::SETCC &&
9059 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9060 // For FCMP_UNE, we can emit
9061 // two branches instead of an explicit AND instruction with a
9062 // separate test. However, we only do this if this block doesn't
9063 // have a fall-through edge, because this requires an explicit
9064 // jmp when the condition is false.
9065 if (Op.getNode()->hasOneUse()) {
9066 SDNode *User = *Op.getNode()->use_begin();
9067 // Look for an unconditional branch following this conditional branch.
9068 // We need this because we need to reverse the successors in order
9069 // to implement FCMP_UNE.
9070 if (User->getOpcode() == ISD::BR) {
9071 SDValue FalseBB = User->getOperand(1);
9072 SDNode *NewBR =
9073 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9074 assert(NewBR == User);
9075 (void)NewBR;
9076
9077 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9078 Cond.getOperand(0), Cond.getOperand(1));
9079 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9080 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9081 Chain, Dest, CC, Cmp);
9082 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9083 Cond = Cmp;
9084 addTest = false;
9085 Dest = FalseBB;
9086 }
9087 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009088 }
Evan Cheng0488db92007-09-25 01:57:46 +00009089 }
9090
9091 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009092 // Look pass the truncate.
9093 if (Cond.getOpcode() == ISD::TRUNCATE)
9094 Cond = Cond.getOperand(0);
9095
9096 // We know the result of AND is compared against zero. Try to match
9097 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009098 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009099 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9100 if (NewSetCC.getNode()) {
9101 CC = NewSetCC.getOperand(0);
9102 Cond = NewSetCC.getOperand(1);
9103 addTest = false;
9104 }
9105 }
9106 }
9107
9108 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009109 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009110 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009111 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00009112 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009113 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009114}
9115
Anton Korobeynikove060b532007-04-17 19:34:00 +00009116
9117// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9118// Calls to _alloca is needed to probe the stack when allocating more than 4k
9119// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9120// that the guard pages used by the OS virtual memory manager are allocated in
9121// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009122SDValue
9123X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009124 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009125 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
9126 EnableSegmentedStacks) &&
9127 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009128 "are being used");
9129 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009130 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009131
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009132 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009133 SDValue Chain = Op.getOperand(0);
9134 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009135 // FIXME: Ensure alignment here
9136
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009137 bool Is64Bit = Subtarget->is64Bit();
9138 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009139
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009140 if (EnableSegmentedStacks) {
9141 MachineFunction &MF = DAG.getMachineFunction();
9142 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009143
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009144 if (Is64Bit) {
9145 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009146 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009147 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009148
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009149 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
9150 I != E; I++)
9151 if (I->hasNestAttr())
9152 report_fatal_error("Cannot use segmented stacks with functions that "
9153 "have nested arguments.");
9154 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009155
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009156 const TargetRegisterClass *AddrRegClass =
9157 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9158 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9159 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9160 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9161 DAG.getRegister(Vreg, SPTy));
9162 SDValue Ops1[2] = { Value, Chain };
9163 return DAG.getMergeValues(Ops1, 2, dl);
9164 } else {
9165 SDValue Flag;
9166 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009167
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009168 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9169 Flag = Chain.getValue(1);
9170 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009171
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009172 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9173 Flag = Chain.getValue(1);
9174
9175 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9176
9177 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9178 return DAG.getMergeValues(Ops1, 2, dl);
9179 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009180}
9181
Dan Gohmand858e902010-04-17 15:26:15 +00009182SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009183 MachineFunction &MF = DAG.getMachineFunction();
9184 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9185
Dan Gohman69de1932008-02-06 22:27:42 +00009186 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009187 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009188
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009189 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009190 // vastart just stores the address of the VarArgsFrameIndex slot into the
9191 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009192 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9193 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009194 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9195 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009196 }
9197
9198 // __va_list_tag:
9199 // gp_offset (0 - 6 * 8)
9200 // fp_offset (48 - 48 + 8 * 16)
9201 // overflow_arg_area (point to parameters coming in memory).
9202 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009203 SmallVector<SDValue, 8> MemOps;
9204 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009205 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009206 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009207 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9208 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009209 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009210 MemOps.push_back(Store);
9211
9212 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009213 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009214 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009215 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009216 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9217 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009218 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009219 MemOps.push_back(Store);
9220
9221 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009222 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009223 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009224 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9225 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009226 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9227 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009228 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009229 MemOps.push_back(Store);
9230
9231 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009232 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009233 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009234 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9235 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009236 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9237 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009238 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009239 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009240 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009241}
9242
Dan Gohmand858e902010-04-17 15:26:15 +00009243SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009244 assert(Subtarget->is64Bit() &&
9245 "LowerVAARG only handles 64-bit va_arg!");
9246 assert((Subtarget->isTargetLinux() ||
9247 Subtarget->isTargetDarwin()) &&
9248 "Unhandled target in LowerVAARG");
9249 assert(Op.getNode()->getNumOperands() == 4);
9250 SDValue Chain = Op.getOperand(0);
9251 SDValue SrcPtr = Op.getOperand(1);
9252 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9253 unsigned Align = Op.getConstantOperandVal(3);
9254 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009255
Dan Gohman320afb82010-10-12 18:00:49 +00009256 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009257 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009258 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9259 uint8_t ArgMode;
9260
9261 // Decide which area this value should be read from.
9262 // TODO: Implement the AMD64 ABI in its entirety. This simple
9263 // selection mechanism works only for the basic types.
9264 if (ArgVT == MVT::f80) {
9265 llvm_unreachable("va_arg for f80 not yet implemented");
9266 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9267 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9268 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9269 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9270 } else {
9271 llvm_unreachable("Unhandled argument type in LowerVAARG");
9272 }
9273
9274 if (ArgMode == 2) {
9275 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00009276 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009277 !(DAG.getMachineFunction()
9278 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009279 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009280 }
9281
9282 // Insert VAARG_64 node into the DAG
9283 // VAARG_64 returns two values: Variable Argument Address, Chain
9284 SmallVector<SDValue, 11> InstOps;
9285 InstOps.push_back(Chain);
9286 InstOps.push_back(SrcPtr);
9287 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9288 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9289 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9290 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9291 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9292 VTs, &InstOps[0], InstOps.size(),
9293 MVT::i64,
9294 MachinePointerInfo(SV),
9295 /*Align=*/0,
9296 /*Volatile=*/false,
9297 /*ReadMem=*/true,
9298 /*WriteMem=*/true);
9299 Chain = VAARG.getValue(1);
9300
9301 // Load the next argument and return it
9302 return DAG.getLoad(ArgVT, dl,
9303 Chain,
9304 VAARG,
9305 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009306 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009307}
9308
Dan Gohmand858e902010-04-17 15:26:15 +00009309SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009310 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009311 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009312 SDValue Chain = Op.getOperand(0);
9313 SDValue DstPtr = Op.getOperand(1);
9314 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009315 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9316 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009317 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009318
Chris Lattnere72f2022010-09-21 05:40:29 +00009319 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009320 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009321 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009322 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009323}
9324
Dan Gohman475871a2008-07-27 21:46:04 +00009325SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009326X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009327 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009328 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009329 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009330 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009331 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009332 case Intrinsic::x86_sse_comieq_ss:
9333 case Intrinsic::x86_sse_comilt_ss:
9334 case Intrinsic::x86_sse_comile_ss:
9335 case Intrinsic::x86_sse_comigt_ss:
9336 case Intrinsic::x86_sse_comige_ss:
9337 case Intrinsic::x86_sse_comineq_ss:
9338 case Intrinsic::x86_sse_ucomieq_ss:
9339 case Intrinsic::x86_sse_ucomilt_ss:
9340 case Intrinsic::x86_sse_ucomile_ss:
9341 case Intrinsic::x86_sse_ucomigt_ss:
9342 case Intrinsic::x86_sse_ucomige_ss:
9343 case Intrinsic::x86_sse_ucomineq_ss:
9344 case Intrinsic::x86_sse2_comieq_sd:
9345 case Intrinsic::x86_sse2_comilt_sd:
9346 case Intrinsic::x86_sse2_comile_sd:
9347 case Intrinsic::x86_sse2_comigt_sd:
9348 case Intrinsic::x86_sse2_comige_sd:
9349 case Intrinsic::x86_sse2_comineq_sd:
9350 case Intrinsic::x86_sse2_ucomieq_sd:
9351 case Intrinsic::x86_sse2_ucomilt_sd:
9352 case Intrinsic::x86_sse2_ucomile_sd:
9353 case Intrinsic::x86_sse2_ucomigt_sd:
9354 case Intrinsic::x86_sse2_ucomige_sd:
9355 case Intrinsic::x86_sse2_ucomineq_sd: {
9356 unsigned Opc = 0;
9357 ISD::CondCode CC = ISD::SETCC_INVALID;
9358 switch (IntNo) {
9359 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009360 case Intrinsic::x86_sse_comieq_ss:
9361 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009362 Opc = X86ISD::COMI;
9363 CC = ISD::SETEQ;
9364 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009365 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009366 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009367 Opc = X86ISD::COMI;
9368 CC = ISD::SETLT;
9369 break;
9370 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009371 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009372 Opc = X86ISD::COMI;
9373 CC = ISD::SETLE;
9374 break;
9375 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009376 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009377 Opc = X86ISD::COMI;
9378 CC = ISD::SETGT;
9379 break;
9380 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009381 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009382 Opc = X86ISD::COMI;
9383 CC = ISD::SETGE;
9384 break;
9385 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009386 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009387 Opc = X86ISD::COMI;
9388 CC = ISD::SETNE;
9389 break;
9390 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009391 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009392 Opc = X86ISD::UCOMI;
9393 CC = ISD::SETEQ;
9394 break;
9395 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009396 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009397 Opc = X86ISD::UCOMI;
9398 CC = ISD::SETLT;
9399 break;
9400 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009401 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009402 Opc = X86ISD::UCOMI;
9403 CC = ISD::SETLE;
9404 break;
9405 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009406 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009407 Opc = X86ISD::UCOMI;
9408 CC = ISD::SETGT;
9409 break;
9410 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009411 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009412 Opc = X86ISD::UCOMI;
9413 CC = ISD::SETGE;
9414 break;
9415 case Intrinsic::x86_sse_ucomineq_ss:
9416 case Intrinsic::x86_sse2_ucomineq_sd:
9417 Opc = X86ISD::UCOMI;
9418 CC = ISD::SETNE;
9419 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009420 }
Evan Cheng734503b2006-09-11 02:19:56 +00009421
Dan Gohman475871a2008-07-27 21:46:04 +00009422 SDValue LHS = Op.getOperand(1);
9423 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009424 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009425 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009426 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9427 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9428 DAG.getConstant(X86CC, MVT::i8), Cond);
9429 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009430 }
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009431 // Arithmetic intrinsics.
9432 case Intrinsic::x86_sse3_hadd_ps:
9433 case Intrinsic::x86_sse3_hadd_pd:
9434 case Intrinsic::x86_avx_hadd_ps_256:
9435 case Intrinsic::x86_avx_hadd_pd_256:
9436 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9437 Op.getOperand(1), Op.getOperand(2));
9438 case Intrinsic::x86_sse3_hsub_ps:
9439 case Intrinsic::x86_sse3_hsub_pd:
9440 case Intrinsic::x86_avx_hsub_ps_256:
9441 case Intrinsic::x86_avx_hsub_pd_256:
9442 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9443 Op.getOperand(1), Op.getOperand(2));
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009444 // ptest and testp intrinsics. The intrinsic these come from are designed to
9445 // return an integer value, not just an instruction so lower it to the ptest
9446 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009447 case Intrinsic::x86_sse41_ptestz:
9448 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009449 case Intrinsic::x86_sse41_ptestnzc:
9450 case Intrinsic::x86_avx_ptestz_256:
9451 case Intrinsic::x86_avx_ptestc_256:
9452 case Intrinsic::x86_avx_ptestnzc_256:
9453 case Intrinsic::x86_avx_vtestz_ps:
9454 case Intrinsic::x86_avx_vtestc_ps:
9455 case Intrinsic::x86_avx_vtestnzc_ps:
9456 case Intrinsic::x86_avx_vtestz_pd:
9457 case Intrinsic::x86_avx_vtestc_pd:
9458 case Intrinsic::x86_avx_vtestnzc_pd:
9459 case Intrinsic::x86_avx_vtestz_ps_256:
9460 case Intrinsic::x86_avx_vtestc_ps_256:
9461 case Intrinsic::x86_avx_vtestnzc_ps_256:
9462 case Intrinsic::x86_avx_vtestz_pd_256:
9463 case Intrinsic::x86_avx_vtestc_pd_256:
9464 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9465 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009466 unsigned X86CC = 0;
9467 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009468 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009469 case Intrinsic::x86_avx_vtestz_ps:
9470 case Intrinsic::x86_avx_vtestz_pd:
9471 case Intrinsic::x86_avx_vtestz_ps_256:
9472 case Intrinsic::x86_avx_vtestz_pd_256:
9473 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009474 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009475 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009476 // ZF = 1
9477 X86CC = X86::COND_E;
9478 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009479 case Intrinsic::x86_avx_vtestc_ps:
9480 case Intrinsic::x86_avx_vtestc_pd:
9481 case Intrinsic::x86_avx_vtestc_ps_256:
9482 case Intrinsic::x86_avx_vtestc_pd_256:
9483 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009484 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009485 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009486 // CF = 1
9487 X86CC = X86::COND_B;
9488 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009489 case Intrinsic::x86_avx_vtestnzc_ps:
9490 case Intrinsic::x86_avx_vtestnzc_pd:
9491 case Intrinsic::x86_avx_vtestnzc_ps_256:
9492 case Intrinsic::x86_avx_vtestnzc_pd_256:
9493 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009494 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009495 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009496 // ZF and CF = 0
9497 X86CC = X86::COND_A;
9498 break;
9499 }
Eric Christopherfd179292009-08-27 18:07:15 +00009500
Eric Christopher71c67532009-07-29 00:28:05 +00009501 SDValue LHS = Op.getOperand(1);
9502 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009503 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9504 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009505 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9506 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9507 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009508 }
Evan Cheng5759f972008-05-04 09:15:50 +00009509
9510 // Fix vector shift instructions where the last operand is a non-immediate
9511 // i32 value.
9512 case Intrinsic::x86_sse2_pslli_w:
9513 case Intrinsic::x86_sse2_pslli_d:
9514 case Intrinsic::x86_sse2_pslli_q:
9515 case Intrinsic::x86_sse2_psrli_w:
9516 case Intrinsic::x86_sse2_psrli_d:
9517 case Intrinsic::x86_sse2_psrli_q:
9518 case Intrinsic::x86_sse2_psrai_w:
9519 case Intrinsic::x86_sse2_psrai_d:
9520 case Intrinsic::x86_mmx_pslli_w:
9521 case Intrinsic::x86_mmx_pslli_d:
9522 case Intrinsic::x86_mmx_pslli_q:
9523 case Intrinsic::x86_mmx_psrli_w:
9524 case Intrinsic::x86_mmx_psrli_d:
9525 case Intrinsic::x86_mmx_psrli_q:
9526 case Intrinsic::x86_mmx_psrai_w:
9527 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009528 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009529 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009530 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009531
9532 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009533 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009534 switch (IntNo) {
9535 case Intrinsic::x86_sse2_pslli_w:
9536 NewIntNo = Intrinsic::x86_sse2_psll_w;
9537 break;
9538 case Intrinsic::x86_sse2_pslli_d:
9539 NewIntNo = Intrinsic::x86_sse2_psll_d;
9540 break;
9541 case Intrinsic::x86_sse2_pslli_q:
9542 NewIntNo = Intrinsic::x86_sse2_psll_q;
9543 break;
9544 case Intrinsic::x86_sse2_psrli_w:
9545 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9546 break;
9547 case Intrinsic::x86_sse2_psrli_d:
9548 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9549 break;
9550 case Intrinsic::x86_sse2_psrli_q:
9551 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9552 break;
9553 case Intrinsic::x86_sse2_psrai_w:
9554 NewIntNo = Intrinsic::x86_sse2_psra_w;
9555 break;
9556 case Intrinsic::x86_sse2_psrai_d:
9557 NewIntNo = Intrinsic::x86_sse2_psra_d;
9558 break;
9559 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009560 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009561 switch (IntNo) {
9562 case Intrinsic::x86_mmx_pslli_w:
9563 NewIntNo = Intrinsic::x86_mmx_psll_w;
9564 break;
9565 case Intrinsic::x86_mmx_pslli_d:
9566 NewIntNo = Intrinsic::x86_mmx_psll_d;
9567 break;
9568 case Intrinsic::x86_mmx_pslli_q:
9569 NewIntNo = Intrinsic::x86_mmx_psll_q;
9570 break;
9571 case Intrinsic::x86_mmx_psrli_w:
9572 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9573 break;
9574 case Intrinsic::x86_mmx_psrli_d:
9575 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9576 break;
9577 case Intrinsic::x86_mmx_psrli_q:
9578 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9579 break;
9580 case Intrinsic::x86_mmx_psrai_w:
9581 NewIntNo = Intrinsic::x86_mmx_psra_w;
9582 break;
9583 case Intrinsic::x86_mmx_psrai_d:
9584 NewIntNo = Intrinsic::x86_mmx_psra_d;
9585 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009586 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009587 }
9588 break;
9589 }
9590 }
Mon P Wangefa42202009-09-03 19:56:25 +00009591
9592 // The vector shift intrinsics with scalars uses 32b shift amounts but
9593 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9594 // to be zero.
9595 SDValue ShOps[4];
9596 ShOps[0] = ShAmt;
9597 ShOps[1] = DAG.getConstant(0, MVT::i32);
9598 if (ShAmtVT == MVT::v4i32) {
9599 ShOps[2] = DAG.getUNDEF(MVT::i32);
9600 ShOps[3] = DAG.getUNDEF(MVT::i32);
9601 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9602 } else {
9603 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009604// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009605 }
9606
Owen Andersone50ed302009-08-10 22:56:29 +00009607 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009608 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009609 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009610 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009611 Op.getOperand(1), ShAmt);
9612 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009613 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009614}
Evan Cheng72261582005-12-20 06:22:03 +00009615
Dan Gohmand858e902010-04-17 15:26:15 +00009616SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9617 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009618 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9619 MFI->setReturnAddressIsTaken(true);
9620
Bill Wendling64e87322009-01-16 19:25:27 +00009621 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009622 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009623
9624 if (Depth > 0) {
9625 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9626 SDValue Offset =
9627 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009628 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009629 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009630 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009631 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009632 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009633 }
9634
9635 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009636 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009637 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009638 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009639}
9640
Dan Gohmand858e902010-04-17 15:26:15 +00009641SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009642 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9643 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009644
Owen Andersone50ed302009-08-10 22:56:29 +00009645 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009646 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009647 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9648 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009649 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009650 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009651 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9652 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009653 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009654 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009655}
9656
Dan Gohman475871a2008-07-27 21:46:04 +00009657SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009658 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009659 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009660}
9661
Dan Gohmand858e902010-04-17 15:26:15 +00009662SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009663 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009664 SDValue Chain = Op.getOperand(0);
9665 SDValue Offset = Op.getOperand(1);
9666 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009667 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009668
Dan Gohmand8816272010-08-11 18:14:00 +00009669 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9670 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9671 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009672 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009673
Dan Gohmand8816272010-08-11 18:14:00 +00009674 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9675 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009676 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009677 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9678 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009679 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009680 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009681
Dale Johannesene4d209d2009-02-03 20:21:25 +00009682 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009683 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009684 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009685}
9686
Duncan Sands4a544a72011-09-06 13:37:06 +00009687SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9688 SelectionDAG &DAG) const {
9689 return Op.getOperand(0);
9690}
9691
9692SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9693 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009694 SDValue Root = Op.getOperand(0);
9695 SDValue Trmp = Op.getOperand(1); // trampoline
9696 SDValue FPtr = Op.getOperand(2); // nested function
9697 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009698 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009699
Dan Gohman69de1932008-02-06 22:27:42 +00009700 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009701
9702 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009703 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009704
9705 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009706 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9707 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009708
Evan Cheng0e6a0522011-07-18 20:57:22 +00009709 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9710 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009711
9712 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9713
9714 // Load the pointer to the nested function into R11.
9715 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009716 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009717 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009718 Addr, MachinePointerInfo(TrmpAddr),
9719 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009720
Owen Anderson825b72b2009-08-11 20:47:22 +00009721 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9722 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009723 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9724 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009725 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009726
9727 // Load the 'nest' parameter value into R10.
9728 // R10 is specified in X86CallingConv.td
9729 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009730 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9731 DAG.getConstant(10, MVT::i64));
9732 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009733 Addr, MachinePointerInfo(TrmpAddr, 10),
9734 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009735
Owen Anderson825b72b2009-08-11 20:47:22 +00009736 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9737 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009738 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9739 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009740 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009741
9742 // Jump to the nested function.
9743 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009744 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9745 DAG.getConstant(20, MVT::i64));
9746 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009747 Addr, MachinePointerInfo(TrmpAddr, 20),
9748 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009749
9750 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009751 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9752 DAG.getConstant(22, MVT::i64));
9753 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009754 MachinePointerInfo(TrmpAddr, 22),
9755 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009756
Duncan Sands4a544a72011-09-06 13:37:06 +00009757 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009758 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009759 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009760 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009761 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009762 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009763
9764 switch (CC) {
9765 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009766 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009767 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009768 case CallingConv::X86_StdCall: {
9769 // Pass 'nest' parameter in ECX.
9770 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009771 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009772
9773 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009774 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009775 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009776
Chris Lattner58d74912008-03-12 17:45:29 +00009777 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009778 unsigned InRegCount = 0;
9779 unsigned Idx = 1;
9780
9781 for (FunctionType::param_iterator I = FTy->param_begin(),
9782 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009783 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009784 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009785 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009786
9787 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009788 report_fatal_error("Nest register in use - reduce number of inreg"
9789 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009790 }
9791 }
9792 break;
9793 }
9794 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009795 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009796 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009797 // Pass 'nest' parameter in EAX.
9798 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009799 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009800 break;
9801 }
9802
Dan Gohman475871a2008-07-27 21:46:04 +00009803 SDValue OutChains[4];
9804 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009805
Owen Anderson825b72b2009-08-11 20:47:22 +00009806 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9807 DAG.getConstant(10, MVT::i32));
9808 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009809
Chris Lattnera62fe662010-02-05 19:20:30 +00009810 // This is storing the opcode for MOV32ri.
9811 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009812 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009813 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009814 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009815 Trmp, MachinePointerInfo(TrmpAddr),
9816 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009817
Owen Anderson825b72b2009-08-11 20:47:22 +00009818 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9819 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009820 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9821 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009822 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009823
Chris Lattnera62fe662010-02-05 19:20:30 +00009824 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009825 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9826 DAG.getConstant(5, MVT::i32));
9827 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009828 MachinePointerInfo(TrmpAddr, 5),
9829 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009830
Owen Anderson825b72b2009-08-11 20:47:22 +00009831 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9832 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009833 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9834 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009835 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009836
Duncan Sands4a544a72011-09-06 13:37:06 +00009837 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009838 }
9839}
9840
Dan Gohmand858e902010-04-17 15:26:15 +00009841SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9842 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009843 /*
9844 The rounding mode is in bits 11:10 of FPSR, and has the following
9845 settings:
9846 00 Round to nearest
9847 01 Round to -inf
9848 10 Round to +inf
9849 11 Round to 0
9850
9851 FLT_ROUNDS, on the other hand, expects the following:
9852 -1 Undefined
9853 0 Round to 0
9854 1 Round to nearest
9855 2 Round to +inf
9856 3 Round to -inf
9857
9858 To perform the conversion, we do:
9859 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9860 */
9861
9862 MachineFunction &MF = DAG.getMachineFunction();
9863 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009864 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009865 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009866 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009867 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009868
9869 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009870 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009871 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009872
Michael J. Spencerec38de22010-10-10 22:04:20 +00009873
Chris Lattner2156b792010-09-22 01:11:26 +00009874 MachineMemOperand *MMO =
9875 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9876 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009877
Chris Lattner2156b792010-09-22 01:11:26 +00009878 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9879 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9880 DAG.getVTList(MVT::Other),
9881 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009882
9883 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009884 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +00009885 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009886
9887 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009888 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009889 DAG.getNode(ISD::SRL, DL, MVT::i16,
9890 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009891 CWD, DAG.getConstant(0x800, MVT::i16)),
9892 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009893 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009894 DAG.getNode(ISD::SRL, DL, MVT::i16,
9895 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009896 CWD, DAG.getConstant(0x400, MVT::i16)),
9897 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009898
Dan Gohman475871a2008-07-27 21:46:04 +00009899 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009900 DAG.getNode(ISD::AND, DL, MVT::i16,
9901 DAG.getNode(ISD::ADD, DL, MVT::i16,
9902 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009903 DAG.getConstant(1, MVT::i16)),
9904 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009905
9906
Duncan Sands83ec4b62008-06-06 12:08:01 +00009907 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009908 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009909}
9910
Dan Gohmand858e902010-04-17 15:26:15 +00009911SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009912 EVT VT = Op.getValueType();
9913 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009914 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009915 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009916
9917 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009918 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009919 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009920 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009921 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009922 }
Evan Cheng18efe262007-12-14 02:13:44 +00009923
Evan Cheng152804e2007-12-14 08:30:15 +00009924 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009925 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009926 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009927
9928 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009929 SDValue Ops[] = {
9930 Op,
9931 DAG.getConstant(NumBits+NumBits-1, OpVT),
9932 DAG.getConstant(X86::COND_E, MVT::i8),
9933 Op.getValue(1)
9934 };
9935 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009936
9937 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009938 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009939
Owen Anderson825b72b2009-08-11 20:47:22 +00009940 if (VT == MVT::i8)
9941 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009942 return Op;
9943}
9944
Dan Gohmand858e902010-04-17 15:26:15 +00009945SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009946 EVT VT = Op.getValueType();
9947 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009948 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009949 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009950
9951 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009952 if (VT == MVT::i8) {
9953 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009954 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009955 }
Evan Cheng152804e2007-12-14 08:30:15 +00009956
9957 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009958 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009959 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009960
9961 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009962 SDValue Ops[] = {
9963 Op,
9964 DAG.getConstant(NumBits, OpVT),
9965 DAG.getConstant(X86::COND_E, MVT::i8),
9966 Op.getValue(1)
9967 };
9968 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009969
Owen Anderson825b72b2009-08-11 20:47:22 +00009970 if (VT == MVT::i8)
9971 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009972 return Op;
9973}
9974
Craig Topper13894fa2011-08-24 06:14:18 +00009975// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9976// ones, and then concatenate the result back.
9977static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009978 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009979
9980 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9981 "Unsupported value type for operation");
9982
9983 int NumElems = VT.getVectorNumElements();
9984 DebugLoc dl = Op.getDebugLoc();
9985 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9986 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9987
9988 // Extract the LHS vectors
9989 SDValue LHS = Op.getOperand(0);
9990 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9991 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9992
9993 // Extract the RHS vectors
9994 SDValue RHS = Op.getOperand(1);
9995 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9996 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9997
9998 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9999 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10000
10001 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10002 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10003 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10004}
10005
10006SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
10007 assert(Op.getValueType().getSizeInBits() == 256 &&
10008 Op.getValueType().isInteger() &&
10009 "Only handle AVX 256-bit vector integer operation");
10010 return Lower256IntArith(Op, DAG);
10011}
10012
10013SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
10014 assert(Op.getValueType().getSizeInBits() == 256 &&
10015 Op.getValueType().isInteger() &&
10016 "Only handle AVX 256-bit vector integer operation");
10017 return Lower256IntArith(Op, DAG);
10018}
10019
10020SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
10021 EVT VT = Op.getValueType();
10022
10023 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topperaaa643c2011-11-09 07:28:55 +000010024 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010025 return Lower256IntArith(Op, DAG);
10026
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010027 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010028
Craig Topperaaa643c2011-11-09 07:28:55 +000010029 SDValue A = Op.getOperand(0);
10030 SDValue B = Op.getOperand(1);
10031
10032 if (VT == MVT::v4i64) {
10033 assert(Subtarget->hasAVX2() && "Lowering v4i64 multiply requires AVX2");
10034
10035 // ulong2 Ahi = __builtin_ia32_psrlqi256( a, 32);
10036 // ulong2 Bhi = __builtin_ia32_psrlqi256( b, 32);
10037 // ulong2 AloBlo = __builtin_ia32_pmuludq256( a, b );
10038 // ulong2 AloBhi = __builtin_ia32_pmuludq256( a, Bhi );
10039 // ulong2 AhiBlo = __builtin_ia32_pmuludq256( Ahi, b );
10040 //
10041 // AloBhi = __builtin_ia32_psllqi256( AloBhi, 32 );
10042 // AhiBlo = __builtin_ia32_psllqi256( AhiBlo, 32 );
10043 // return AloBlo + AloBhi + AhiBlo;
10044
10045 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10046 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
10047 A, DAG.getConstant(32, MVT::i32));
10048 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10049 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
10050 B, DAG.getConstant(32, MVT::i32));
10051 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10052 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10053 A, B);
10054 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10055 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10056 A, Bhi);
10057 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10058 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10059 Ahi, B);
10060 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10061 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
10062 AloBhi, DAG.getConstant(32, MVT::i32));
10063 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10064 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
10065 AhiBlo, DAG.getConstant(32, MVT::i32));
10066 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10067 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
10068 return Res;
10069 }
10070
10071 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
10072
Mon P Wangaf9b9522008-12-18 21:42:19 +000010073 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
10074 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
10075 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
10076 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
10077 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
10078 //
10079 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
10080 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
10081 // return AloBlo + AloBhi + AhiBlo;
10082
Dale Johannesene4d209d2009-02-03 20:21:25 +000010083 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010084 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10085 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010086 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010087 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10088 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010089 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010090 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010091 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010092 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010093 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010094 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010095 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010096 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010097 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010098 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010099 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10100 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010101 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010102 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10103 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010104 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10105 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010106 return Res;
10107}
10108
Nadav Rotem43012222011-05-11 08:12:09 +000010109SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10110
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010111 EVT VT = Op.getValueType();
10112 DebugLoc dl = Op.getDebugLoc();
10113 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010114 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010115 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010116
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010117 if (!Subtarget->hasXMMInt())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010118 return SDValue();
10119
10120 // Decompose 256-bit shifts into smaller 128-bit shifts.
10121 if (VT.getSizeInBits() == 256) {
10122 int NumElems = VT.getVectorNumElements();
10123 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10124 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10125
10126 // Extract the two vectors
10127 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
10128 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
10129 DAG, dl);
10130
10131 // Recreate the shift amount vectors
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +000010132 SDValue Amt1, Amt2;
10133 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10134 // Constant shift amount
10135 SmallVector<SDValue, 4> Amt1Csts;
10136 SmallVector<SDValue, 4> Amt2Csts;
10137 for (int i = 0; i < NumElems/2; ++i)
10138 Amt1Csts.push_back(Amt->getOperand(i));
10139 for (int i = NumElems/2; i < NumElems; ++i)
10140 Amt2Csts.push_back(Amt->getOperand(i));
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010141
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +000010142 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10143 &Amt1Csts[0], NumElems/2);
10144 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10145 &Amt2Csts[0], NumElems/2);
10146 } else {
10147 // Variable shift amount
10148 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
10149 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
10150 DAG, dl);
10151 }
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010152
10153 // Issue new vector shifts for the smaller types
10154 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10155 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10156
10157 // Concatenate the result back
10158 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10159 }
Nate Begeman51409212010-07-28 00:21:48 +000010160
Nadav Rotem43012222011-05-11 08:12:09 +000010161 // Optimize shl/srl/sra with constant shift amount.
10162 if (isSplatVector(Amt.getNode())) {
10163 SDValue SclrAmt = Amt->getOperand(0);
10164 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10165 uint64_t ShiftAmt = C->getZExtValue();
10166
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010167 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SHL) {
10168 // Make a large shift.
10169 SDValue SHL =
10170 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10171 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10172 R, DAG.getConstant(ShiftAmt, MVT::i32));
10173 // Zero out the rightmost bits.
10174 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10175 MVT::i8));
10176 return DAG.getNode(ISD::AND, dl, VT, SHL,
10177 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10178 }
10179
Nadav Rotem43012222011-05-11 08:12:09 +000010180 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
10181 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10182 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10183 R, DAG.getConstant(ShiftAmt, MVT::i32));
10184
10185 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
10186 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10187 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10188 R, DAG.getConstant(ShiftAmt, MVT::i32));
10189
10190 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
10191 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10192 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10193 R, DAG.getConstant(ShiftAmt, MVT::i32));
10194
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010195 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRL) {
10196 // Make a large shift.
10197 SDValue SRL =
10198 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10199 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10200 R, DAG.getConstant(ShiftAmt, MVT::i32));
10201 // Zero out the leftmost bits.
10202 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10203 MVT::i8));
10204 return DAG.getNode(ISD::AND, dl, VT, SRL,
10205 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10206 }
10207
Nadav Rotem43012222011-05-11 08:12:09 +000010208 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
10209 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10210 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10211 R, DAG.getConstant(ShiftAmt, MVT::i32));
10212
10213 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
10214 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10215 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
10216 R, DAG.getConstant(ShiftAmt, MVT::i32));
10217
10218 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
10219 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10220 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10221 R, DAG.getConstant(ShiftAmt, MVT::i32));
10222
10223 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
10224 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10225 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
10226 R, DAG.getConstant(ShiftAmt, MVT::i32));
10227
10228 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
10229 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10230 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
10231 R, DAG.getConstant(ShiftAmt, MVT::i32));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010232
10233 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRA) {
10234 if (ShiftAmt == 7) {
10235 // R s>> 7 === R s< 0
10236 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10237 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10238 }
10239
10240 // R s>> a === ((R u>> a) ^ m) - m
10241 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10242 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10243 MVT::i8));
10244 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10245 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10246 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10247 return Res;
10248 }
Nadav Rotem43012222011-05-11 08:12:09 +000010249 }
10250 }
10251
10252 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010253 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010254 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10255 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10256 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
10257
10258 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010259
Nate Begeman51409212010-07-28 00:21:48 +000010260 std::vector<Constant*> CV(4, CI);
10261 Constant *C = ConstantVector::get(CV);
10262 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10263 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010264 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010265 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010266
10267 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010268 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010269 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10270 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10271 }
Nadav Rotem43012222011-05-11 08:12:09 +000010272 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010273 // a = a << 5;
10274 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10275 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10276 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
10277
10278 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
10279 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
10280
10281 std::vector<Constant*> CVM1(16, CM1);
10282 std::vector<Constant*> CVM2(16, CM2);
10283 Constant *C = ConstantVector::get(CVM1);
10284 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10285 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010286 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010287 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010288
10289 // r = pblendv(r, psllw(r & (char16)15, 4), a);
10290 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10291 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10292 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10293 DAG.getConstant(4, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010294 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010295 // a += a
10296 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010297
Nate Begeman51409212010-07-28 00:21:48 +000010298 C = ConstantVector::get(CVM2);
10299 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10300 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010301 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010302 false, false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010303
Nate Begeman51409212010-07-28 00:21:48 +000010304 // r = pblendv(r, psllw(r & (char16)63, 2), a);
10305 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10306 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10307 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10308 DAG.getConstant(2, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010309 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010310 // a += a
10311 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010312
Nate Begeman51409212010-07-28 00:21:48 +000010313 // return pblendv(r, r+r, a);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010314 R = DAG.getNode(ISD::VSELECT, dl, VT, Op,
10315 R, DAG.getNode(ISD::ADD, dl, VT, R, R));
Nate Begeman51409212010-07-28 00:21:48 +000010316 return R;
10317 }
10318 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010319}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010320
Dan Gohmand858e902010-04-17 15:26:15 +000010321SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010322 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10323 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010324 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10325 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010326 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010327 SDValue LHS = N->getOperand(0);
10328 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010329 unsigned BaseOp = 0;
10330 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010331 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010332 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010333 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010334 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010335 // A subtract of one will be selected as a INC. Note that INC doesn't
10336 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010337 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10338 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010339 BaseOp = X86ISD::INC;
10340 Cond = X86::COND_O;
10341 break;
10342 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010343 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010344 Cond = X86::COND_O;
10345 break;
10346 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010347 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010348 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010349 break;
10350 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010351 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10352 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010353 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10354 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010355 BaseOp = X86ISD::DEC;
10356 Cond = X86::COND_O;
10357 break;
10358 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010359 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010360 Cond = X86::COND_O;
10361 break;
10362 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010363 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010364 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010365 break;
10366 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010367 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010368 Cond = X86::COND_O;
10369 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010370 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10371 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10372 MVT::i32);
10373 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010374
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010375 SDValue SetCC =
10376 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10377 DAG.getConstant(X86::COND_O, MVT::i32),
10378 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010379
Dan Gohman6e5fda22011-07-22 18:45:15 +000010380 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010381 }
Bill Wendling74c37652008-12-09 22:08:41 +000010382 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010383
Bill Wendling61edeb52008-12-02 01:06:39 +000010384 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010385 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010386 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010387
Bill Wendling61edeb52008-12-02 01:06:39 +000010388 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010389 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10390 DAG.getConstant(Cond, MVT::i32),
10391 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010392
Dan Gohman6e5fda22011-07-22 18:45:15 +000010393 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010394}
10395
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010396SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10397 DebugLoc dl = Op.getDebugLoc();
10398 SDNode* Node = Op.getNode();
10399 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
10400 EVT VT = Node->getValueType(0);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010401 if (Subtarget->hasXMMInt() && VT.isVector()) {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010402 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10403 ExtraVT.getScalarType().getSizeInBits();
10404 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10405
10406 unsigned SHLIntrinsicsID = 0;
10407 unsigned SRAIntrinsicsID = 0;
10408 switch (VT.getSimpleVT().SimpleTy) {
10409 default:
10410 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010411 case MVT::v4i32: {
10412 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10413 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10414 break;
10415 }
10416 case MVT::v8i16: {
10417 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10418 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10419 break;
10420 }
10421 }
10422
10423 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10424 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
10425 Node->getOperand(0), ShAmt);
10426
Nadav Rotema7934dd2011-10-10 19:31:45 +000010427 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10428 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10429 Tmp1, ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010430 }
10431
10432 return SDValue();
10433}
10434
10435
Eric Christopher9a9d2752010-07-22 02:48:34 +000010436SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10437 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010438
Eric Christopher77ed1352011-07-08 00:04:56 +000010439 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10440 // There isn't any reason to disable it if the target processor supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010441 if (!Subtarget->hasXMMInt() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010442 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010443 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010444 SDValue Ops[] = {
10445 DAG.getRegister(X86::ESP, MVT::i32), // Base
10446 DAG.getTargetConstant(1, MVT::i8), // Scale
10447 DAG.getRegister(0, MVT::i32), // Index
10448 DAG.getTargetConstant(0, MVT::i32), // Disp
10449 DAG.getRegister(0, MVT::i32), // Segment.
10450 Zero,
10451 Chain
10452 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010453 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010454 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10455 array_lengthof(Ops));
10456 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010457 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010458
Eric Christopher9a9d2752010-07-22 02:48:34 +000010459 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010460 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010461 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010462
Chris Lattner132929a2010-08-14 17:26:09 +000010463 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10464 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10465 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10466 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010467
Chris Lattner132929a2010-08-14 17:26:09 +000010468 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10469 if (!Op1 && !Op2 && !Op3 && Op4)
10470 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010471
Chris Lattner132929a2010-08-14 17:26:09 +000010472 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10473 if (Op1 && !Op2 && !Op3 && !Op4)
10474 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010475
10476 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010477 // (MFENCE)>;
10478 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010479}
10480
Eli Friedman14648462011-07-27 22:21:52 +000010481SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10482 SelectionDAG &DAG) const {
10483 DebugLoc dl = Op.getDebugLoc();
10484 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10485 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10486 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10487 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10488
10489 // The only fence that needs an instruction is a sequentially-consistent
10490 // cross-thread fence.
10491 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10492 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10493 // no-sse2). There isn't any reason to disable it if the target processor
10494 // supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010495 if (Subtarget->hasXMMInt() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010496 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10497
10498 SDValue Chain = Op.getOperand(0);
10499 SDValue Zero = DAG.getConstant(0, MVT::i32);
10500 SDValue Ops[] = {
10501 DAG.getRegister(X86::ESP, MVT::i32), // Base
10502 DAG.getTargetConstant(1, MVT::i8), // Scale
10503 DAG.getRegister(0, MVT::i32), // Index
10504 DAG.getTargetConstant(0, MVT::i32), // Disp
10505 DAG.getRegister(0, MVT::i32), // Segment.
10506 Zero,
10507 Chain
10508 };
10509 SDNode *Res =
10510 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10511 array_lengthof(Ops));
10512 return SDValue(Res, 0);
10513 }
10514
10515 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10516 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10517}
10518
10519
Dan Gohmand858e902010-04-17 15:26:15 +000010520SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010521 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010522 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010523 unsigned Reg = 0;
10524 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010525 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010526 default:
10527 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010528 case MVT::i8: Reg = X86::AL; size = 1; break;
10529 case MVT::i16: Reg = X86::AX; size = 2; break;
10530 case MVT::i32: Reg = X86::EAX; size = 4; break;
10531 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010532 assert(Subtarget->is64Bit() && "Node not type legal!");
10533 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010534 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010535 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010536 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010537 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010538 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010539 Op.getOperand(1),
10540 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010541 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010542 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010543 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010544 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10545 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10546 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010547 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010548 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010549 return cpOut;
10550}
10551
Duncan Sands1607f052008-12-01 11:39:25 +000010552SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010553 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010554 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010555 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010556 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010557 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010558 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010559 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10560 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010561 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010562 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10563 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010564 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010565 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010566 rdx.getValue(1)
10567 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010568 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010569}
10570
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010571SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010572 SelectionDAG &DAG) const {
10573 EVT SrcVT = Op.getOperand(0).getValueType();
10574 EVT DstVT = Op.getValueType();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010575 assert(Subtarget->is64Bit() && !Subtarget->hasXMMInt() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010576 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010577 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010578 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010579 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010580 // i64 <=> MMX conversions are Legal.
10581 if (SrcVT==MVT::i64 && DstVT.isVector())
10582 return Op;
10583 if (DstVT==MVT::i64 && SrcVT.isVector())
10584 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010585 // MMX <=> MMX conversions are Legal.
10586 if (SrcVT.isVector() && DstVT.isVector())
10587 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010588 // All other conversions need to be expanded.
10589 return SDValue();
10590}
Chris Lattner5b856542010-12-20 00:59:46 +000010591
Dan Gohmand858e902010-04-17 15:26:15 +000010592SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010593 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010594 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010595 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010596 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010597 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010598 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010599 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010600 Node->getOperand(0),
10601 Node->getOperand(1), negOp,
10602 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010603 cast<AtomicSDNode>(Node)->getAlignment(),
10604 cast<AtomicSDNode>(Node)->getOrdering(),
10605 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010606}
10607
Eli Friedman327236c2011-08-24 20:50:09 +000010608static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10609 SDNode *Node = Op.getNode();
10610 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010611 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010612
10613 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010614 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10615 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10616 // (The only way to get a 16-byte store is cmpxchg16b)
10617 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10618 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10619 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010620 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10621 cast<AtomicSDNode>(Node)->getMemoryVT(),
10622 Node->getOperand(0),
10623 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010624 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010625 cast<AtomicSDNode>(Node)->getOrdering(),
10626 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010627 return Swap.getValue(1);
10628 }
10629 // Other atomic stores have a simple pattern.
10630 return Op;
10631}
10632
Chris Lattner5b856542010-12-20 00:59:46 +000010633static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10634 EVT VT = Op.getNode()->getValueType(0);
10635
10636 // Let legalize expand this if it isn't a legal type yet.
10637 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10638 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010639
Chris Lattner5b856542010-12-20 00:59:46 +000010640 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010641
Chris Lattner5b856542010-12-20 00:59:46 +000010642 unsigned Opc;
10643 bool ExtraOp = false;
10644 switch (Op.getOpcode()) {
10645 default: assert(0 && "Invalid code");
10646 case ISD::ADDC: Opc = X86ISD::ADD; break;
10647 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10648 case ISD::SUBC: Opc = X86ISD::SUB; break;
10649 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10650 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010651
Chris Lattner5b856542010-12-20 00:59:46 +000010652 if (!ExtraOp)
10653 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10654 Op.getOperand(1));
10655 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10656 Op.getOperand(1), Op.getOperand(2));
10657}
10658
Evan Cheng0db9fe62006-04-25 20:13:52 +000010659/// LowerOperation - Provide custom lowering hooks for some operations.
10660///
Dan Gohmand858e902010-04-17 15:26:15 +000010661SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010662 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010663 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010664 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010665 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010666 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010667 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10668 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010669 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010670 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010671 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010672 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10673 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10674 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010675 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010676 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010677 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10678 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10679 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010680 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010681 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010682 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010683 case ISD::SHL_PARTS:
10684 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010685 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010686 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010687 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010688 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010689 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010690 case ISD::FABS: return LowerFABS(Op, DAG);
10691 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010692 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010693 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010694 case ISD::SETCC: return LowerSETCC(Op, DAG);
10695 case ISD::SELECT: return LowerSELECT(Op, DAG);
10696 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010697 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010698 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010699 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010700 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010701 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010702 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10703 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010704 case ISD::FRAME_TO_ARGS_OFFSET:
10705 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010706 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010707 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010708 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10709 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010710 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010711 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10712 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010713 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010714 case ISD::SRA:
10715 case ISD::SRL:
10716 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010717 case ISD::SADDO:
10718 case ISD::UADDO:
10719 case ISD::SSUBO:
10720 case ISD::USUBO:
10721 case ISD::SMULO:
10722 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010723 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010724 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010725 case ISD::ADDC:
10726 case ISD::ADDE:
10727 case ISD::SUBC:
10728 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010729 case ISD::ADD: return LowerADD(Op, DAG);
10730 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010731 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010732}
10733
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010734static void ReplaceATOMIC_LOAD(SDNode *Node,
10735 SmallVectorImpl<SDValue> &Results,
10736 SelectionDAG &DAG) {
10737 DebugLoc dl = Node->getDebugLoc();
10738 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10739
10740 // Convert wide load -> cmpxchg8b/cmpxchg16b
10741 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10742 // (The only way to get a 16-byte load is cmpxchg16b)
10743 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010744 SDValue Zero = DAG.getConstant(0, VT);
10745 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010746 Node->getOperand(0),
10747 Node->getOperand(1), Zero, Zero,
10748 cast<AtomicSDNode>(Node)->getMemOperand(),
10749 cast<AtomicSDNode>(Node)->getOrdering(),
10750 cast<AtomicSDNode>(Node)->getSynchScope());
10751 Results.push_back(Swap.getValue(0));
10752 Results.push_back(Swap.getValue(1));
10753}
10754
Duncan Sands1607f052008-12-01 11:39:25 +000010755void X86TargetLowering::
10756ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010757 SelectionDAG &DAG, unsigned NewOp) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010758 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000010759 assert (Node->getValueType(0) == MVT::i64 &&
10760 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010761
10762 SDValue Chain = Node->getOperand(0);
10763 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010764 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010765 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010766 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010767 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010768 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010769 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010770 SDValue Result =
10771 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10772 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010773 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010774 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010775 Results.push_back(Result.getValue(2));
10776}
10777
Duncan Sands126d9072008-07-04 11:47:58 +000010778/// ReplaceNodeResults - Replace a node with an illegal result type
10779/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010780void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10781 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010782 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010783 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010784 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010785 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010786 assert(false && "Do not know how to custom type legalize this operation!");
10787 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010788 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010789 case ISD::ADDC:
10790 case ISD::ADDE:
10791 case ISD::SUBC:
10792 case ISD::SUBE:
10793 // We don't want to expand or promote these.
10794 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010795 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010796 std::pair<SDValue,SDValue> Vals =
10797 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010798 SDValue FIST = Vals.first, StackSlot = Vals.second;
10799 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010800 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010801 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010802 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010803 MachinePointerInfo(),
10804 false, false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010805 }
10806 return;
10807 }
10808 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010809 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010810 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010811 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010812 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010813 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010814 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010815 eax.getValue(2));
10816 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10817 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010818 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010819 Results.push_back(edx.getValue(1));
10820 return;
10821 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010822 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010823 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010824 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010825 bool Regs64bit = T == MVT::i128;
10826 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010827 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010828 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10829 DAG.getConstant(0, HalfT));
10830 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10831 DAG.getConstant(1, HalfT));
10832 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10833 Regs64bit ? X86::RAX : X86::EAX,
10834 cpInL, SDValue());
10835 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10836 Regs64bit ? X86::RDX : X86::EDX,
10837 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010838 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010839 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10840 DAG.getConstant(0, HalfT));
10841 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10842 DAG.getConstant(1, HalfT));
10843 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10844 Regs64bit ? X86::RBX : X86::EBX,
10845 swapInL, cpInH.getValue(1));
10846 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10847 Regs64bit ? X86::RCX : X86::ECX,
10848 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010849 SDValue Ops[] = { swapInH.getValue(0),
10850 N->getOperand(1),
10851 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010852 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010853 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010854 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10855 X86ISD::LCMPXCHG8_DAG;
10856 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010857 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010858 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10859 Regs64bit ? X86::RAX : X86::EAX,
10860 HalfT, Result.getValue(1));
10861 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10862 Regs64bit ? X86::RDX : X86::EDX,
10863 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010864 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010865 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010866 Results.push_back(cpOutH.getValue(1));
10867 return;
10868 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010869 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010870 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10871 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010872 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010873 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10874 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010875 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010876 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10877 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010878 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010879 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10880 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010881 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010882 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10883 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010884 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010885 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10886 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010887 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010888 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10889 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010890 case ISD::ATOMIC_LOAD:
10891 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010892 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010893}
10894
Evan Cheng72261582005-12-20 06:22:03 +000010895const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10896 switch (Opcode) {
10897 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010898 case X86ISD::BSF: return "X86ISD::BSF";
10899 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010900 case X86ISD::SHLD: return "X86ISD::SHLD";
10901 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010902 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010903 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010904 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010905 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010906 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010907 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010908 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10909 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10910 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010911 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010912 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010913 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010914 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010915 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010916 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010917 case X86ISD::COMI: return "X86ISD::COMI";
10918 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010919 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010920 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010921 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10922 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010923 case X86ISD::CMOV: return "X86ISD::CMOV";
10924 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010925 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010926 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10927 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010928 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010929 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010930 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010931 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010932 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010933 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10934 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010935 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010936 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010937 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +000010938 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
10939 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
10940 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Evan Cheng8ca29322006-11-10 21:43:37 +000010941 case X86ISD::FMAX: return "X86ISD::FMAX";
10942 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000010943 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
10944 case X86ISD::FRCP: return "X86ISD::FRCP";
Duncan Sands17470be2011-09-22 20:15:48 +000010945 case X86ISD::FHADD: return "X86ISD::FHADD";
10946 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010947 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000010948 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010949 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000010950 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010951 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000010952 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
10953 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010954 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
10955 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
10956 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
10957 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
10958 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
10959 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000010960 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
10961 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000010962 case X86ISD::VSHL: return "X86ISD::VSHL";
10963 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000010964 case X86ISD::CMPPD: return "X86ISD::CMPPD";
10965 case X86ISD::CMPPS: return "X86ISD::CMPPS";
10966 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
10967 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
10968 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
10969 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
10970 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
10971 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
10972 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
10973 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010974 case X86ISD::ADD: return "X86ISD::ADD";
10975 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000010976 case X86ISD::ADC: return "X86ISD::ADC";
10977 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000010978 case X86ISD::SMUL: return "X86ISD::SMUL";
10979 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000010980 case X86ISD::INC: return "X86ISD::INC";
10981 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000010982 case X86ISD::OR: return "X86ISD::OR";
10983 case X86ISD::XOR: return "X86ISD::XOR";
10984 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000010985 case X86ISD::ANDN: return "X86ISD::ANDN";
Evan Cheng73f24c92009-03-30 21:36:47 +000010986 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000010987 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010988 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010989 case X86ISD::PALIGN: return "X86ISD::PALIGN";
10990 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
10991 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
10992 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
10993 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
10994 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
10995 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
10996 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
10997 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010998 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000010999 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011000 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011001 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11002 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011003 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11004 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11005 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
11006 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
11007 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
11008 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11009 case X86ISD::MOVSS: return "X86ISD::MOVSS";
11010 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
11011 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +000011012 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011013 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
11014 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
11015 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
11016 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
11017 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
11018 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
11019 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
11020 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
11021 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
11022 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011023 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000011024 case X86ISD::VPERMILPS: return "X86ISD::VPERMILPS";
11025 case X86ISD::VPERMILPSY: return "X86ISD::VPERMILPSY";
11026 case X86ISD::VPERMILPD: return "X86ISD::VPERMILPD";
11027 case X86ISD::VPERMILPDY: return "X86ISD::VPERMILPDY";
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000011028 case X86ISD::VPERM2F128: return "X86ISD::VPERM2F128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011029 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011030 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011031 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011032 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011033 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000011034 }
11035}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011036
Chris Lattnerc9addb72007-03-30 23:15:24 +000011037// isLegalAddressingMode - Return true if the addressing mode represented
11038// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011039bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011040 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011041 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011042 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011043 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011044
Chris Lattnerc9addb72007-03-30 23:15:24 +000011045 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011046 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011047 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011048
Chris Lattnerc9addb72007-03-30 23:15:24 +000011049 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011050 unsigned GVFlags =
11051 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011052
Chris Lattnerdfed4132009-07-10 07:38:24 +000011053 // If a reference to this global requires an extra load, we can't fold it.
11054 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011055 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011056
Chris Lattnerdfed4132009-07-10 07:38:24 +000011057 // If BaseGV requires a register for the PIC base, we cannot also have a
11058 // BaseReg specified.
11059 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011060 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011061
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011062 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011063 if ((M != CodeModel::Small || R != Reloc::Static) &&
11064 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011065 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011066 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011067
Chris Lattnerc9addb72007-03-30 23:15:24 +000011068 switch (AM.Scale) {
11069 case 0:
11070 case 1:
11071 case 2:
11072 case 4:
11073 case 8:
11074 // These scales always work.
11075 break;
11076 case 3:
11077 case 5:
11078 case 9:
11079 // These scales are formed with basereg+scalereg. Only accept if there is
11080 // no basereg yet.
11081 if (AM.HasBaseReg)
11082 return false;
11083 break;
11084 default: // Other stuff never works.
11085 return false;
11086 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011087
Chris Lattnerc9addb72007-03-30 23:15:24 +000011088 return true;
11089}
11090
11091
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011092bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011093 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011094 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011095 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11096 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011097 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011098 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011099 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011100}
11101
Owen Andersone50ed302009-08-10 22:56:29 +000011102bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011103 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011104 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011105 unsigned NumBits1 = VT1.getSizeInBits();
11106 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011107 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011108 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011109 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011110}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011111
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011112bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011113 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011114 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011115}
11116
Owen Andersone50ed302009-08-10 22:56:29 +000011117bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011118 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011119 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011120}
11121
Owen Andersone50ed302009-08-10 22:56:29 +000011122bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011123 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011124 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011125}
11126
Evan Cheng60c07e12006-07-05 22:17:51 +000011127/// isShuffleMaskLegal - Targets can use this to indicate that they only
11128/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11129/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11130/// are assumed to be legal.
11131bool
Eric Christopherfd179292009-08-27 18:07:15 +000011132X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011133 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011134 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011135 if (VT.getSizeInBits() == 64)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000011136 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX());
Nate Begeman9008ca62009-04-27 18:41:29 +000011137
Nate Begemana09008b2009-10-19 02:17:23 +000011138 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011139 return (VT.getVectorNumElements() == 2 ||
11140 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11141 isMOVLMask(M, VT) ||
11142 isSHUFPMask(M, VT) ||
11143 isPSHUFDMask(M, VT) ||
11144 isPSHUFHWMask(M, VT) ||
11145 isPSHUFLWMask(M, VT) ||
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000011146 isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011147 isUNPCKLMask(M, VT) ||
11148 isUNPCKHMask(M, VT) ||
11149 isUNPCKL_v_undef_Mask(M, VT) ||
11150 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011151}
11152
Dan Gohman7d8143f2008-04-09 20:09:42 +000011153bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011154X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011155 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011156 unsigned NumElts = VT.getVectorNumElements();
11157 // FIXME: This collection of masks seems suspect.
11158 if (NumElts == 2)
11159 return true;
11160 if (NumElts == 4 && VT.getSizeInBits() == 128) {
11161 return (isMOVLMask(Mask, VT) ||
11162 isCommutedMOVLMask(Mask, VT, true) ||
11163 isSHUFPMask(Mask, VT) ||
11164 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011165 }
11166 return false;
11167}
11168
11169//===----------------------------------------------------------------------===//
11170// X86 Scheduler Hooks
11171//===----------------------------------------------------------------------===//
11172
Mon P Wang63307c32008-05-05 19:05:59 +000011173// private utility function
11174MachineBasicBlock *
11175X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11176 MachineBasicBlock *MBB,
11177 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011178 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011179 unsigned LoadOpc,
11180 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011181 unsigned notOpc,
11182 unsigned EAXreg,
11183 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011184 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011185 // For the atomic bitwise operator, we generate
11186 // thisMBB:
11187 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011188 // ld t1 = [bitinstr.addr]
11189 // op t2 = t1, [bitinstr.val]
11190 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011191 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11192 // bz newMBB
11193 // fallthrough -->nextMBB
11194 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11195 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011196 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011197 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011198
Mon P Wang63307c32008-05-05 19:05:59 +000011199 /// First build the CFG
11200 MachineFunction *F = MBB->getParent();
11201 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011202 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11203 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11204 F->insert(MBBIter, newMBB);
11205 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011206
Dan Gohman14152b42010-07-06 20:24:04 +000011207 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11208 nextMBB->splice(nextMBB->begin(), thisMBB,
11209 llvm::next(MachineBasicBlock::iterator(bInstr)),
11210 thisMBB->end());
11211 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011212
Mon P Wang63307c32008-05-05 19:05:59 +000011213 // Update thisMBB to fall through to newMBB
11214 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011215
Mon P Wang63307c32008-05-05 19:05:59 +000011216 // newMBB jumps to itself and fall through to nextMBB
11217 newMBB->addSuccessor(nextMBB);
11218 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011219
Mon P Wang63307c32008-05-05 19:05:59 +000011220 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011221 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011222 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011223 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011224 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011225 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011226 int numArgs = bInstr->getNumOperands() - 1;
11227 for (int i=0; i < numArgs; ++i)
11228 argOpers[i] = &bInstr->getOperand(i+1);
11229
11230 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011231 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011232 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011233
Dale Johannesen140be2d2008-08-19 18:47:28 +000011234 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011235 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011236 for (int i=0; i <= lastAddrIndx; ++i)
11237 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011238
Dale Johannesen140be2d2008-08-19 18:47:28 +000011239 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011240 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011241 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011242 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011243 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011244 tt = t1;
11245
Dale Johannesen140be2d2008-08-19 18:47:28 +000011246 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011247 assert((argOpers[valArgIndx]->isReg() ||
11248 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011249 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011250 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011251 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011252 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011253 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011254 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000011255 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011256
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011257 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000011258 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011259
Dale Johannesene4d209d2009-02-03 20:21:25 +000011260 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011261 for (int i=0; i <= lastAddrIndx; ++i)
11262 (*MIB).addOperand(*argOpers[i]);
11263 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000011264 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011265 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11266 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011267
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011268 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011269 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011270
Mon P Wang63307c32008-05-05 19:05:59 +000011271 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011272 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011273
Dan Gohman14152b42010-07-06 20:24:04 +000011274 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011275 return nextMBB;
11276}
11277
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011278// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011279MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011280X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11281 MachineBasicBlock *MBB,
11282 unsigned regOpcL,
11283 unsigned regOpcH,
11284 unsigned immOpcL,
11285 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011286 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011287 // For the atomic bitwise operator, we generate
11288 // thisMBB (instructions are in pairs, except cmpxchg8b)
11289 // ld t1,t2 = [bitinstr.addr]
11290 // newMBB:
11291 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11292 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011293 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011294 // mov ECX, EBX <- t5, t6
11295 // mov EAX, EDX <- t1, t2
11296 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11297 // mov t3, t4 <- EAX, EDX
11298 // bz newMBB
11299 // result in out1, out2
11300 // fallthrough -->nextMBB
11301
11302 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11303 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011304 const unsigned NotOpc = X86::NOT32r;
11305 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11306 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11307 MachineFunction::iterator MBBIter = MBB;
11308 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011309
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011310 /// First build the CFG
11311 MachineFunction *F = MBB->getParent();
11312 MachineBasicBlock *thisMBB = MBB;
11313 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11314 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11315 F->insert(MBBIter, newMBB);
11316 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011317
Dan Gohman14152b42010-07-06 20:24:04 +000011318 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11319 nextMBB->splice(nextMBB->begin(), thisMBB,
11320 llvm::next(MachineBasicBlock::iterator(bInstr)),
11321 thisMBB->end());
11322 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011323
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011324 // Update thisMBB to fall through to newMBB
11325 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011326
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011327 // newMBB jumps to itself and fall through to nextMBB
11328 newMBB->addSuccessor(nextMBB);
11329 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011330
Dale Johannesene4d209d2009-02-03 20:21:25 +000011331 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011332 // Insert instructions into newMBB based on incoming instruction
11333 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011334 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011335 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011336 MachineOperand& dest1Oper = bInstr->getOperand(0);
11337 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011338 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11339 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011340 argOpers[i] = &bInstr->getOperand(i+2);
11341
Dan Gohman71ea4e52010-05-14 21:01:44 +000011342 // We use some of the operands multiple times, so conservatively just
11343 // clear any kill flags that might be present.
11344 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11345 argOpers[i]->setIsKill(false);
11346 }
11347
Evan Chengad5b52f2010-01-08 19:14:57 +000011348 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011349 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011350
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011351 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011352 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011353 for (int i=0; i <= lastAddrIndx; ++i)
11354 (*MIB).addOperand(*argOpers[i]);
11355 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011356 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011357 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011358 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011359 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011360 MachineOperand newOp3 = *(argOpers[3]);
11361 if (newOp3.isImm())
11362 newOp3.setImm(newOp3.getImm()+4);
11363 else
11364 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011365 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011366 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011367
11368 // t3/4 are defined later, at the bottom of the loop
11369 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11370 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011371 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011372 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011373 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011374 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11375
Evan Cheng306b4ca2010-01-08 23:41:50 +000011376 // The subsequent operations should be using the destination registers of
11377 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011378 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011379 t1 = F->getRegInfo().createVirtualRegister(RC);
11380 t2 = F->getRegInfo().createVirtualRegister(RC);
11381 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11382 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011383 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011384 t1 = dest1Oper.getReg();
11385 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011386 }
11387
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011388 int valArgIndx = lastAddrIndx + 1;
11389 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011390 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011391 "invalid operand");
11392 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11393 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011394 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011395 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011396 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011397 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011398 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011399 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011400 (*MIB).addOperand(*argOpers[valArgIndx]);
11401 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011402 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011403 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011404 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011405 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011406 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011407 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011408 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011409 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011410 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011411 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011412
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011413 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011414 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011415 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011416 MIB.addReg(t2);
11417
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011418 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011419 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011420 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011421 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011422
Dale Johannesene4d209d2009-02-03 20:21:25 +000011423 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011424 for (int i=0; i <= lastAddrIndx; ++i)
11425 (*MIB).addOperand(*argOpers[i]);
11426
11427 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011428 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11429 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011430
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011431 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011432 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011433 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011434 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011435
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011436 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011437 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011438
Dan Gohman14152b42010-07-06 20:24:04 +000011439 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011440 return nextMBB;
11441}
11442
11443// private utility function
11444MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011445X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11446 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011447 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011448 // For the atomic min/max operator, we generate
11449 // thisMBB:
11450 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011451 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011452 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011453 // cmp t1, t2
11454 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011455 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011456 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11457 // bz newMBB
11458 // fallthrough -->nextMBB
11459 //
11460 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11461 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011462 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011463 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011464
Mon P Wang63307c32008-05-05 19:05:59 +000011465 /// First build the CFG
11466 MachineFunction *F = MBB->getParent();
11467 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011468 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11469 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11470 F->insert(MBBIter, newMBB);
11471 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011472
Dan Gohman14152b42010-07-06 20:24:04 +000011473 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11474 nextMBB->splice(nextMBB->begin(), thisMBB,
11475 llvm::next(MachineBasicBlock::iterator(mInstr)),
11476 thisMBB->end());
11477 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011478
Mon P Wang63307c32008-05-05 19:05:59 +000011479 // Update thisMBB to fall through to newMBB
11480 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011481
Mon P Wang63307c32008-05-05 19:05:59 +000011482 // newMBB jumps to newMBB and fall through to nextMBB
11483 newMBB->addSuccessor(nextMBB);
11484 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011485
Dale Johannesene4d209d2009-02-03 20:21:25 +000011486 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011487 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011488 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011489 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011490 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011491 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011492 int numArgs = mInstr->getNumOperands() - 1;
11493 for (int i=0; i < numArgs; ++i)
11494 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011495
Mon P Wang63307c32008-05-05 19:05:59 +000011496 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011497 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011498 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011499
Mon P Wangab3e7472008-05-05 22:56:23 +000011500 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011501 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011502 for (int i=0; i <= lastAddrIndx; ++i)
11503 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011504
Mon P Wang63307c32008-05-05 19:05:59 +000011505 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011506 assert((argOpers[valArgIndx]->isReg() ||
11507 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011508 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011509
11510 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011511 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011512 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011513 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011514 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011515 (*MIB).addOperand(*argOpers[valArgIndx]);
11516
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011517 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011518 MIB.addReg(t1);
11519
Dale Johannesene4d209d2009-02-03 20:21:25 +000011520 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011521 MIB.addReg(t1);
11522 MIB.addReg(t2);
11523
11524 // Generate movc
11525 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011526 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011527 MIB.addReg(t2);
11528 MIB.addReg(t1);
11529
11530 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011531 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011532 for (int i=0; i <= lastAddrIndx; ++i)
11533 (*MIB).addOperand(*argOpers[i]);
11534 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011535 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011536 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11537 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011538
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011539 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011540 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011541
Mon P Wang63307c32008-05-05 19:05:59 +000011542 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011543 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011544
Dan Gohman14152b42010-07-06 20:24:04 +000011545 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011546 return nextMBB;
11547}
11548
Eric Christopherf83a5de2009-08-27 18:08:16 +000011549// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011550// or XMM0_V32I8 in AVX all of this code can be replaced with that
11551// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011552MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011553X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011554 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011555 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
11556 "Target must have SSE4.2 or AVX features enabled");
11557
Eric Christopherb120ab42009-08-18 22:50:32 +000011558 DebugLoc dl = MI->getDebugLoc();
11559 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011560 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011561 if (!Subtarget->hasAVX()) {
11562 if (memArg)
11563 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11564 else
11565 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11566 } else {
11567 if (memArg)
11568 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11569 else
11570 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11571 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011572
Eric Christopher41c902f2010-11-30 08:20:21 +000011573 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011574 for (unsigned i = 0; i < numArgs; ++i) {
11575 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011576 if (!(Op.isReg() && Op.isImplicit()))
11577 MIB.addOperand(Op);
11578 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011579 BuildMI(*BB, MI, dl,
11580 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11581 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011582 .addReg(X86::XMM0);
11583
Dan Gohman14152b42010-07-06 20:24:04 +000011584 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011585 return BB;
11586}
11587
11588MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011589X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011590 DebugLoc dl = MI->getDebugLoc();
11591 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011592
Eric Christopher228232b2010-11-30 07:20:12 +000011593 // Address into RAX/EAX, other two args into ECX, EDX.
11594 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11595 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11596 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11597 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011598 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011599
Eric Christopher228232b2010-11-30 07:20:12 +000011600 unsigned ValOps = X86::AddrNumOperands;
11601 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11602 .addReg(MI->getOperand(ValOps).getReg());
11603 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11604 .addReg(MI->getOperand(ValOps+1).getReg());
11605
11606 // The instruction doesn't actually take any operands though.
11607 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011608
Eric Christopher228232b2010-11-30 07:20:12 +000011609 MI->eraseFromParent(); // The pseudo is gone now.
11610 return BB;
11611}
11612
11613MachineBasicBlock *
11614X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011615 DebugLoc dl = MI->getDebugLoc();
11616 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011617
Eric Christopher228232b2010-11-30 07:20:12 +000011618 // First arg in ECX, the second in EAX.
11619 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11620 .addReg(MI->getOperand(0).getReg());
11621 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11622 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011623
Eric Christopher228232b2010-11-30 07:20:12 +000011624 // The instruction doesn't actually take any operands though.
11625 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011626
Eric Christopher228232b2010-11-30 07:20:12 +000011627 MI->eraseFromParent(); // The pseudo is gone now.
11628 return BB;
11629}
11630
11631MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011632X86TargetLowering::EmitVAARG64WithCustomInserter(
11633 MachineInstr *MI,
11634 MachineBasicBlock *MBB) const {
11635 // Emit va_arg instruction on X86-64.
11636
11637 // Operands to this pseudo-instruction:
11638 // 0 ) Output : destination address (reg)
11639 // 1-5) Input : va_list address (addr, i64mem)
11640 // 6 ) ArgSize : Size (in bytes) of vararg type
11641 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11642 // 8 ) Align : Alignment of type
11643 // 9 ) EFLAGS (implicit-def)
11644
11645 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11646 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11647
11648 unsigned DestReg = MI->getOperand(0).getReg();
11649 MachineOperand &Base = MI->getOperand(1);
11650 MachineOperand &Scale = MI->getOperand(2);
11651 MachineOperand &Index = MI->getOperand(3);
11652 MachineOperand &Disp = MI->getOperand(4);
11653 MachineOperand &Segment = MI->getOperand(5);
11654 unsigned ArgSize = MI->getOperand(6).getImm();
11655 unsigned ArgMode = MI->getOperand(7).getImm();
11656 unsigned Align = MI->getOperand(8).getImm();
11657
11658 // Memory Reference
11659 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11660 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11661 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11662
11663 // Machine Information
11664 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11665 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11666 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11667 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11668 DebugLoc DL = MI->getDebugLoc();
11669
11670 // struct va_list {
11671 // i32 gp_offset
11672 // i32 fp_offset
11673 // i64 overflow_area (address)
11674 // i64 reg_save_area (address)
11675 // }
11676 // sizeof(va_list) = 24
11677 // alignment(va_list) = 8
11678
11679 unsigned TotalNumIntRegs = 6;
11680 unsigned TotalNumXMMRegs = 8;
11681 bool UseGPOffset = (ArgMode == 1);
11682 bool UseFPOffset = (ArgMode == 2);
11683 unsigned MaxOffset = TotalNumIntRegs * 8 +
11684 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11685
11686 /* Align ArgSize to a multiple of 8 */
11687 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11688 bool NeedsAlign = (Align > 8);
11689
11690 MachineBasicBlock *thisMBB = MBB;
11691 MachineBasicBlock *overflowMBB;
11692 MachineBasicBlock *offsetMBB;
11693 MachineBasicBlock *endMBB;
11694
11695 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11696 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11697 unsigned OffsetReg = 0;
11698
11699 if (!UseGPOffset && !UseFPOffset) {
11700 // If we only pull from the overflow region, we don't create a branch.
11701 // We don't need to alter control flow.
11702 OffsetDestReg = 0; // unused
11703 OverflowDestReg = DestReg;
11704
11705 offsetMBB = NULL;
11706 overflowMBB = thisMBB;
11707 endMBB = thisMBB;
11708 } else {
11709 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11710 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11711 // If not, pull from overflow_area. (branch to overflowMBB)
11712 //
11713 // thisMBB
11714 // | .
11715 // | .
11716 // offsetMBB overflowMBB
11717 // | .
11718 // | .
11719 // endMBB
11720
11721 // Registers for the PHI in endMBB
11722 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11723 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11724
11725 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11726 MachineFunction *MF = MBB->getParent();
11727 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11728 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11729 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11730
11731 MachineFunction::iterator MBBIter = MBB;
11732 ++MBBIter;
11733
11734 // Insert the new basic blocks
11735 MF->insert(MBBIter, offsetMBB);
11736 MF->insert(MBBIter, overflowMBB);
11737 MF->insert(MBBIter, endMBB);
11738
11739 // Transfer the remainder of MBB and its successor edges to endMBB.
11740 endMBB->splice(endMBB->begin(), thisMBB,
11741 llvm::next(MachineBasicBlock::iterator(MI)),
11742 thisMBB->end());
11743 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11744
11745 // Make offsetMBB and overflowMBB successors of thisMBB
11746 thisMBB->addSuccessor(offsetMBB);
11747 thisMBB->addSuccessor(overflowMBB);
11748
11749 // endMBB is a successor of both offsetMBB and overflowMBB
11750 offsetMBB->addSuccessor(endMBB);
11751 overflowMBB->addSuccessor(endMBB);
11752
11753 // Load the offset value into a register
11754 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11755 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11756 .addOperand(Base)
11757 .addOperand(Scale)
11758 .addOperand(Index)
11759 .addDisp(Disp, UseFPOffset ? 4 : 0)
11760 .addOperand(Segment)
11761 .setMemRefs(MMOBegin, MMOEnd);
11762
11763 // Check if there is enough room left to pull this argument.
11764 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11765 .addReg(OffsetReg)
11766 .addImm(MaxOffset + 8 - ArgSizeA8);
11767
11768 // Branch to "overflowMBB" if offset >= max
11769 // Fall through to "offsetMBB" otherwise
11770 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11771 .addMBB(overflowMBB);
11772 }
11773
11774 // In offsetMBB, emit code to use the reg_save_area.
11775 if (offsetMBB) {
11776 assert(OffsetReg != 0);
11777
11778 // Read the reg_save_area address.
11779 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11780 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11781 .addOperand(Base)
11782 .addOperand(Scale)
11783 .addOperand(Index)
11784 .addDisp(Disp, 16)
11785 .addOperand(Segment)
11786 .setMemRefs(MMOBegin, MMOEnd);
11787
11788 // Zero-extend the offset
11789 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11790 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11791 .addImm(0)
11792 .addReg(OffsetReg)
11793 .addImm(X86::sub_32bit);
11794
11795 // Add the offset to the reg_save_area to get the final address.
11796 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11797 .addReg(OffsetReg64)
11798 .addReg(RegSaveReg);
11799
11800 // Compute the offset for the next argument
11801 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11802 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11803 .addReg(OffsetReg)
11804 .addImm(UseFPOffset ? 16 : 8);
11805
11806 // Store it back into the va_list.
11807 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11808 .addOperand(Base)
11809 .addOperand(Scale)
11810 .addOperand(Index)
11811 .addDisp(Disp, UseFPOffset ? 4 : 0)
11812 .addOperand(Segment)
11813 .addReg(NextOffsetReg)
11814 .setMemRefs(MMOBegin, MMOEnd);
11815
11816 // Jump to endMBB
11817 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11818 .addMBB(endMBB);
11819 }
11820
11821 //
11822 // Emit code to use overflow area
11823 //
11824
11825 // Load the overflow_area address into a register.
11826 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11827 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11828 .addOperand(Base)
11829 .addOperand(Scale)
11830 .addOperand(Index)
11831 .addDisp(Disp, 8)
11832 .addOperand(Segment)
11833 .setMemRefs(MMOBegin, MMOEnd);
11834
11835 // If we need to align it, do so. Otherwise, just copy the address
11836 // to OverflowDestReg.
11837 if (NeedsAlign) {
11838 // Align the overflow address
11839 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11840 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11841
11842 // aligned_addr = (addr + (align-1)) & ~(align-1)
11843 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11844 .addReg(OverflowAddrReg)
11845 .addImm(Align-1);
11846
11847 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11848 .addReg(TmpReg)
11849 .addImm(~(uint64_t)(Align-1));
11850 } else {
11851 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11852 .addReg(OverflowAddrReg);
11853 }
11854
11855 // Compute the next overflow address after this argument.
11856 // (the overflow address should be kept 8-byte aligned)
11857 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11858 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11859 .addReg(OverflowDestReg)
11860 .addImm(ArgSizeA8);
11861
11862 // Store the new overflow address.
11863 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11864 .addOperand(Base)
11865 .addOperand(Scale)
11866 .addOperand(Index)
11867 .addDisp(Disp, 8)
11868 .addOperand(Segment)
11869 .addReg(NextAddrReg)
11870 .setMemRefs(MMOBegin, MMOEnd);
11871
11872 // If we branched, emit the PHI to the front of endMBB.
11873 if (offsetMBB) {
11874 BuildMI(*endMBB, endMBB->begin(), DL,
11875 TII->get(X86::PHI), DestReg)
11876 .addReg(OffsetDestReg).addMBB(offsetMBB)
11877 .addReg(OverflowDestReg).addMBB(overflowMBB);
11878 }
11879
11880 // Erase the pseudo instruction
11881 MI->eraseFromParent();
11882
11883 return endMBB;
11884}
11885
11886MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011887X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11888 MachineInstr *MI,
11889 MachineBasicBlock *MBB) const {
11890 // Emit code to save XMM registers to the stack. The ABI says that the
11891 // number of registers to save is given in %al, so it's theoretically
11892 // possible to do an indirect jump trick to avoid saving all of them,
11893 // however this code takes a simpler approach and just executes all
11894 // of the stores if %al is non-zero. It's less code, and it's probably
11895 // easier on the hardware branch predictor, and stores aren't all that
11896 // expensive anyway.
11897
11898 // Create the new basic blocks. One block contains all the XMM stores,
11899 // and one block is the final destination regardless of whether any
11900 // stores were performed.
11901 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11902 MachineFunction *F = MBB->getParent();
11903 MachineFunction::iterator MBBIter = MBB;
11904 ++MBBIter;
11905 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11906 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11907 F->insert(MBBIter, XMMSaveMBB);
11908 F->insert(MBBIter, EndMBB);
11909
Dan Gohman14152b42010-07-06 20:24:04 +000011910 // Transfer the remainder of MBB and its successor edges to EndMBB.
11911 EndMBB->splice(EndMBB->begin(), MBB,
11912 llvm::next(MachineBasicBlock::iterator(MI)),
11913 MBB->end());
11914 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11915
Dan Gohmand6708ea2009-08-15 01:38:56 +000011916 // The original block will now fall through to the XMM save block.
11917 MBB->addSuccessor(XMMSaveMBB);
11918 // The XMMSaveMBB will fall through to the end block.
11919 XMMSaveMBB->addSuccessor(EndMBB);
11920
11921 // Now add the instructions.
11922 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11923 DebugLoc DL = MI->getDebugLoc();
11924
11925 unsigned CountReg = MI->getOperand(0).getReg();
11926 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11927 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11928
11929 if (!Subtarget->isTargetWin64()) {
11930 // If %al is 0, branch around the XMM save block.
11931 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011932 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011933 MBB->addSuccessor(EndMBB);
11934 }
11935
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011936 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000011937 // In the XMM save block, save all the XMM argument registers.
11938 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11939 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011940 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011941 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011942 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011943 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011944 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011945 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000011946 .addFrameIndex(RegSaveFrameIndex)
11947 .addImm(/*Scale=*/1)
11948 .addReg(/*IndexReg=*/0)
11949 .addImm(/*Disp=*/Offset)
11950 .addReg(/*Segment=*/0)
11951 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011952 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011953 }
11954
Dan Gohman14152b42010-07-06 20:24:04 +000011955 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011956
11957 return EndMBB;
11958}
Mon P Wang63307c32008-05-05 19:05:59 +000011959
Evan Cheng60c07e12006-07-05 22:17:51 +000011960MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000011961X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011962 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000011963 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11964 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000011965
Chris Lattner52600972009-09-02 05:57:00 +000011966 // To "insert" a SELECT_CC instruction, we actually have to insert the
11967 // diamond control-flow pattern. The incoming instruction knows the
11968 // destination vreg to set, the condition code register to branch on, the
11969 // true/false values to select between, and a branch opcode to use.
11970 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11971 MachineFunction::iterator It = BB;
11972 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000011973
Chris Lattner52600972009-09-02 05:57:00 +000011974 // thisMBB:
11975 // ...
11976 // TrueVal = ...
11977 // cmpTY ccX, r1, r2
11978 // bCC copy1MBB
11979 // fallthrough --> copy0MBB
11980 MachineBasicBlock *thisMBB = BB;
11981 MachineFunction *F = BB->getParent();
11982 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11983 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000011984 F->insert(It, copy0MBB);
11985 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000011986
Bill Wendling730c07e2010-06-25 20:48:10 +000011987 // If the EFLAGS register isn't dead in the terminator, then claim that it's
11988 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000011989 if (!MI->killsRegister(X86::EFLAGS)) {
11990 copy0MBB->addLiveIn(X86::EFLAGS);
11991 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000011992 }
11993
Dan Gohman14152b42010-07-06 20:24:04 +000011994 // Transfer the remainder of BB and its successor edges to sinkMBB.
11995 sinkMBB->splice(sinkMBB->begin(), BB,
11996 llvm::next(MachineBasicBlock::iterator(MI)),
11997 BB->end());
11998 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11999
12000 // Add the true and fallthrough blocks as its successors.
12001 BB->addSuccessor(copy0MBB);
12002 BB->addSuccessor(sinkMBB);
12003
12004 // Create the conditional branch instruction.
12005 unsigned Opc =
12006 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12007 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12008
Chris Lattner52600972009-09-02 05:57:00 +000012009 // copy0MBB:
12010 // %FalseValue = ...
12011 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012012 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012013
Chris Lattner52600972009-09-02 05:57:00 +000012014 // sinkMBB:
12015 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12016 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012017 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12018 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012019 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12020 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12021
Dan Gohman14152b42010-07-06 20:24:04 +000012022 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012023 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012024}
12025
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012026MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012027X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12028 bool Is64Bit) const {
12029 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12030 DebugLoc DL = MI->getDebugLoc();
12031 MachineFunction *MF = BB->getParent();
12032 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12033
12034 assert(EnableSegmentedStacks);
12035
12036 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12037 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12038
12039 // BB:
12040 // ... [Till the alloca]
12041 // If stacklet is not large enough, jump to mallocMBB
12042 //
12043 // bumpMBB:
12044 // Allocate by subtracting from RSP
12045 // Jump to continueMBB
12046 //
12047 // mallocMBB:
12048 // Allocate by call to runtime
12049 //
12050 // continueMBB:
12051 // ...
12052 // [rest of original BB]
12053 //
12054
12055 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12056 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12057 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12058
12059 MachineRegisterInfo &MRI = MF->getRegInfo();
12060 const TargetRegisterClass *AddrRegClass =
12061 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12062
12063 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12064 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12065 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012066 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012067 sizeVReg = MI->getOperand(1).getReg(),
12068 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12069
12070 MachineFunction::iterator MBBIter = BB;
12071 ++MBBIter;
12072
12073 MF->insert(MBBIter, bumpMBB);
12074 MF->insert(MBBIter, mallocMBB);
12075 MF->insert(MBBIter, continueMBB);
12076
12077 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12078 (MachineBasicBlock::iterator(MI)), BB->end());
12079 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12080
12081 // Add code to the main basic block to check if the stack limit has been hit,
12082 // and if so, jump to mallocMBB otherwise to bumpMBB.
12083 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012084 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012085 .addReg(tmpSPVReg).addReg(sizeVReg);
12086 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
12087 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012088 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012089 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12090
12091 // bumpMBB simply decreases the stack pointer, since we know the current
12092 // stacklet has enough space.
12093 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012094 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012095 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012096 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012097 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12098
12099 // Calls into a routine in libgcc to allocate more space from the heap.
12100 if (Is64Bit) {
12101 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12102 .addReg(sizeVReg);
12103 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
12104 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
12105 } else {
12106 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12107 .addImm(12);
12108 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12109 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
12110 .addExternalSymbol("__morestack_allocate_stack_space");
12111 }
12112
12113 if (!Is64Bit)
12114 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12115 .addImm(16);
12116
12117 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12118 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12119 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12120
12121 // Set up the CFG correctly.
12122 BB->addSuccessor(bumpMBB);
12123 BB->addSuccessor(mallocMBB);
12124 mallocMBB->addSuccessor(continueMBB);
12125 bumpMBB->addSuccessor(continueMBB);
12126
12127 // Take care of the PHI nodes.
12128 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12129 MI->getOperand(0).getReg())
12130 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12131 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12132
12133 // Delete the original pseudo instruction.
12134 MI->eraseFromParent();
12135
12136 // And we're done.
12137 return continueMBB;
12138}
12139
12140MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012141X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012142 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012143 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12144 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012145
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012146 assert(!Subtarget->isTargetEnvMacho());
12147
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012148 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12149 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012150
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012151 if (Subtarget->isTargetWin64()) {
12152 if (Subtarget->isTargetCygMing()) {
12153 // ___chkstk(Mingw64):
12154 // Clobbers R10, R11, RAX and EFLAGS.
12155 // Updates RSP.
12156 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12157 .addExternalSymbol("___chkstk")
12158 .addReg(X86::RAX, RegState::Implicit)
12159 .addReg(X86::RSP, RegState::Implicit)
12160 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12161 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12162 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12163 } else {
12164 // __chkstk(MSVCRT): does not update stack pointer.
12165 // Clobbers R10, R11 and EFLAGS.
12166 // FIXME: RAX(allocated size) might be reused and not killed.
12167 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12168 .addExternalSymbol("__chkstk")
12169 .addReg(X86::RAX, RegState::Implicit)
12170 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12171 // RAX has the offset to subtracted from RSP.
12172 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12173 .addReg(X86::RSP)
12174 .addReg(X86::RAX);
12175 }
12176 } else {
12177 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012178 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12179
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012180 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12181 .addExternalSymbol(StackProbeSymbol)
12182 .addReg(X86::EAX, RegState::Implicit)
12183 .addReg(X86::ESP, RegState::Implicit)
12184 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12185 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12186 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12187 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012188
Dan Gohman14152b42010-07-06 20:24:04 +000012189 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012190 return BB;
12191}
Chris Lattner52600972009-09-02 05:57:00 +000012192
12193MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012194X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12195 MachineBasicBlock *BB) const {
12196 // This is pretty easy. We're taking the value that we received from
12197 // our load from the relocation, sticking it in either RDI (x86-64)
12198 // or EAX and doing an indirect call. The return value will then
12199 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012200 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012201 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012202 DebugLoc DL = MI->getDebugLoc();
12203 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012204
12205 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012206 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012207
Eric Christopher30ef0e52010-06-03 04:07:48 +000012208 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012209 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12210 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012211 .addReg(X86::RIP)
12212 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012213 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012214 MI->getOperand(3).getTargetFlags())
12215 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012216 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012217 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000012218 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012219 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12220 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012221 .addReg(0)
12222 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012223 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012224 MI->getOperand(3).getTargetFlags())
12225 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012226 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012227 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012228 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012229 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12230 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012231 .addReg(TII->getGlobalBaseReg(F))
12232 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012233 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012234 MI->getOperand(3).getTargetFlags())
12235 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012236 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012237 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012238 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012239
Dan Gohman14152b42010-07-06 20:24:04 +000012240 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012241 return BB;
12242}
12243
12244MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012245X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012246 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012247 switch (MI->getOpcode()) {
Richard Trieu23946fc2011-09-21 03:09:09 +000012248 default: assert(0 && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012249 case X86::TAILJMPd64:
12250 case X86::TAILJMPr64:
12251 case X86::TAILJMPm64:
Richard Trieu23946fc2011-09-21 03:09:09 +000012252 assert(0 && "TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012253 case X86::TCRETURNdi64:
12254 case X86::TCRETURNri64:
12255 case X86::TCRETURNmi64:
12256 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
12257 // On AMD64, additional defs should be added before register allocation.
12258 if (!Subtarget->isTargetWin64()) {
12259 MI->addRegisterDefined(X86::RSI);
12260 MI->addRegisterDefined(X86::RDI);
12261 MI->addRegisterDefined(X86::XMM6);
12262 MI->addRegisterDefined(X86::XMM7);
12263 MI->addRegisterDefined(X86::XMM8);
12264 MI->addRegisterDefined(X86::XMM9);
12265 MI->addRegisterDefined(X86::XMM10);
12266 MI->addRegisterDefined(X86::XMM11);
12267 MI->addRegisterDefined(X86::XMM12);
12268 MI->addRegisterDefined(X86::XMM13);
12269 MI->addRegisterDefined(X86::XMM14);
12270 MI->addRegisterDefined(X86::XMM15);
12271 }
12272 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012273 case X86::WIN_ALLOCA:
12274 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012275 case X86::SEG_ALLOCA_32:
12276 return EmitLoweredSegAlloca(MI, BB, false);
12277 case X86::SEG_ALLOCA_64:
12278 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012279 case X86::TLSCall_32:
12280 case X86::TLSCall_64:
12281 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012282 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012283 case X86::CMOV_FR32:
12284 case X86::CMOV_FR64:
12285 case X86::CMOV_V4F32:
12286 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012287 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012288 case X86::CMOV_V8F32:
12289 case X86::CMOV_V4F64:
12290 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012291 case X86::CMOV_GR16:
12292 case X86::CMOV_GR32:
12293 case X86::CMOV_RFP32:
12294 case X86::CMOV_RFP64:
12295 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012296 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012297
Dale Johannesen849f2142007-07-03 00:53:03 +000012298 case X86::FP32_TO_INT16_IN_MEM:
12299 case X86::FP32_TO_INT32_IN_MEM:
12300 case X86::FP32_TO_INT64_IN_MEM:
12301 case X86::FP64_TO_INT16_IN_MEM:
12302 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012303 case X86::FP64_TO_INT64_IN_MEM:
12304 case X86::FP80_TO_INT16_IN_MEM:
12305 case X86::FP80_TO_INT32_IN_MEM:
12306 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012307 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12308 DebugLoc DL = MI->getDebugLoc();
12309
Evan Cheng60c07e12006-07-05 22:17:51 +000012310 // Change the floating point control register to use "round towards zero"
12311 // mode when truncating to an integer value.
12312 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012313 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012314 addFrameReference(BuildMI(*BB, MI, DL,
12315 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012316
12317 // Load the old value of the high byte of the control word...
12318 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012319 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012320 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012321 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012322
12323 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012324 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012325 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012326
12327 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012328 addFrameReference(BuildMI(*BB, MI, DL,
12329 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012330
12331 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012332 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012333 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012334
12335 // Get the X86 opcode to use.
12336 unsigned Opc;
12337 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012338 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012339 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12340 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12341 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12342 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12343 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12344 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012345 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12346 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12347 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012348 }
12349
12350 X86AddressMode AM;
12351 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012352 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012353 AM.BaseType = X86AddressMode::RegBase;
12354 AM.Base.Reg = Op.getReg();
12355 } else {
12356 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012357 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012358 }
12359 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012360 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012361 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012362 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012363 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012364 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012365 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012366 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012367 AM.GV = Op.getGlobal();
12368 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012369 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012370 }
Dan Gohman14152b42010-07-06 20:24:04 +000012371 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012372 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012373
12374 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012375 addFrameReference(BuildMI(*BB, MI, DL,
12376 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012377
Dan Gohman14152b42010-07-06 20:24:04 +000012378 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012379 return BB;
12380 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012381 // String/text processing lowering.
12382 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012383 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012384 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12385 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012386 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012387 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12388 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012389 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012390 return EmitPCMP(MI, BB, 5, false /* in mem */);
12391 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012392 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012393 return EmitPCMP(MI, BB, 5, true /* in mem */);
12394
Eric Christopher228232b2010-11-30 07:20:12 +000012395 // Thread synchronization.
12396 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012397 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012398 case X86::MWAIT:
12399 return EmitMwait(MI, BB);
12400
Eric Christopherb120ab42009-08-18 22:50:32 +000012401 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012402 case X86::ATOMAND32:
12403 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012404 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012405 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012406 X86::NOT32r, X86::EAX,
12407 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012408 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012409 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12410 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012411 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012412 X86::NOT32r, X86::EAX,
12413 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012414 case X86::ATOMXOR32:
12415 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012416 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012417 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012418 X86::NOT32r, X86::EAX,
12419 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012420 case X86::ATOMNAND32:
12421 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012422 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012423 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012424 X86::NOT32r, X86::EAX,
12425 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012426 case X86::ATOMMIN32:
12427 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12428 case X86::ATOMMAX32:
12429 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12430 case X86::ATOMUMIN32:
12431 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12432 case X86::ATOMUMAX32:
12433 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012434
12435 case X86::ATOMAND16:
12436 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12437 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012438 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012439 X86::NOT16r, X86::AX,
12440 X86::GR16RegisterClass);
12441 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012442 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012443 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012444 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012445 X86::NOT16r, X86::AX,
12446 X86::GR16RegisterClass);
12447 case X86::ATOMXOR16:
12448 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12449 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012450 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012451 X86::NOT16r, X86::AX,
12452 X86::GR16RegisterClass);
12453 case X86::ATOMNAND16:
12454 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12455 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012456 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012457 X86::NOT16r, X86::AX,
12458 X86::GR16RegisterClass, true);
12459 case X86::ATOMMIN16:
12460 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12461 case X86::ATOMMAX16:
12462 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12463 case X86::ATOMUMIN16:
12464 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12465 case X86::ATOMUMAX16:
12466 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12467
12468 case X86::ATOMAND8:
12469 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12470 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012471 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012472 X86::NOT8r, X86::AL,
12473 X86::GR8RegisterClass);
12474 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012475 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012476 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012477 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012478 X86::NOT8r, X86::AL,
12479 X86::GR8RegisterClass);
12480 case X86::ATOMXOR8:
12481 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12482 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012483 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012484 X86::NOT8r, X86::AL,
12485 X86::GR8RegisterClass);
12486 case X86::ATOMNAND8:
12487 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12488 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012489 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012490 X86::NOT8r, X86::AL,
12491 X86::GR8RegisterClass, true);
12492 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012493 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012494 case X86::ATOMAND64:
12495 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012496 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012497 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012498 X86::NOT64r, X86::RAX,
12499 X86::GR64RegisterClass);
12500 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012501 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12502 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012503 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012504 X86::NOT64r, X86::RAX,
12505 X86::GR64RegisterClass);
12506 case X86::ATOMXOR64:
12507 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012508 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012509 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012510 X86::NOT64r, X86::RAX,
12511 X86::GR64RegisterClass);
12512 case X86::ATOMNAND64:
12513 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12514 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012515 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012516 X86::NOT64r, X86::RAX,
12517 X86::GR64RegisterClass, true);
12518 case X86::ATOMMIN64:
12519 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12520 case X86::ATOMMAX64:
12521 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12522 case X86::ATOMUMIN64:
12523 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12524 case X86::ATOMUMAX64:
12525 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012526
12527 // This group does 64-bit operations on a 32-bit host.
12528 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012529 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012530 X86::AND32rr, X86::AND32rr,
12531 X86::AND32ri, X86::AND32ri,
12532 false);
12533 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012534 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012535 X86::OR32rr, X86::OR32rr,
12536 X86::OR32ri, X86::OR32ri,
12537 false);
12538 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012539 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012540 X86::XOR32rr, X86::XOR32rr,
12541 X86::XOR32ri, X86::XOR32ri,
12542 false);
12543 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012544 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012545 X86::AND32rr, X86::AND32rr,
12546 X86::AND32ri, X86::AND32ri,
12547 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012548 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012549 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012550 X86::ADD32rr, X86::ADC32rr,
12551 X86::ADD32ri, X86::ADC32ri,
12552 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012553 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012554 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012555 X86::SUB32rr, X86::SBB32rr,
12556 X86::SUB32ri, X86::SBB32ri,
12557 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012558 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012559 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012560 X86::MOV32rr, X86::MOV32rr,
12561 X86::MOV32ri, X86::MOV32ri,
12562 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012563 case X86::VASTART_SAVE_XMM_REGS:
12564 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012565
12566 case X86::VAARG_64:
12567 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012568 }
12569}
12570
12571//===----------------------------------------------------------------------===//
12572// X86 Optimization Hooks
12573//===----------------------------------------------------------------------===//
12574
Dan Gohman475871a2008-07-27 21:46:04 +000012575void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012576 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012577 APInt &KnownZero,
12578 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012579 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012580 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012581 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012582 assert((Opc >= ISD::BUILTIN_OP_END ||
12583 Opc == ISD::INTRINSIC_WO_CHAIN ||
12584 Opc == ISD::INTRINSIC_W_CHAIN ||
12585 Opc == ISD::INTRINSIC_VOID) &&
12586 "Should use MaskedValueIsZero if you don't know whether Op"
12587 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012588
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012589 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012590 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012591 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012592 case X86ISD::ADD:
12593 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012594 case X86ISD::ADC:
12595 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012596 case X86ISD::SMUL:
12597 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012598 case X86ISD::INC:
12599 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012600 case X86ISD::OR:
12601 case X86ISD::XOR:
12602 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012603 // These nodes' second result is a boolean.
12604 if (Op.getResNo() == 0)
12605 break;
12606 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012607 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012608 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12609 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012610 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012611 case ISD::INTRINSIC_WO_CHAIN: {
12612 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
12613 unsigned NumLoBits = 0;
12614 switch (IntId) {
12615 default: break;
12616 case Intrinsic::x86_sse_movmsk_ps:
12617 case Intrinsic::x86_avx_movmsk_ps_256:
12618 case Intrinsic::x86_sse2_movmsk_pd:
12619 case Intrinsic::x86_avx_movmsk_pd_256:
12620 case Intrinsic::x86_mmx_pmovmskb:
12621 case Intrinsic::x86_sse2_pmovmskb_128: {
12622 // High bits of movmskp{s|d}, pmovmskb are known zero.
12623 switch (IntId) {
12624 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
12625 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
12626 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
12627 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
12628 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
12629 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
12630 }
12631 KnownZero = APInt::getHighBitsSet(Mask.getBitWidth(),
12632 Mask.getBitWidth() - NumLoBits);
12633 break;
12634 }
12635 }
12636 break;
12637 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012638 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012639}
Chris Lattner259e97c2006-01-31 19:43:35 +000012640
Owen Andersonbc146b02010-09-21 20:42:50 +000012641unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12642 unsigned Depth) const {
12643 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12644 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12645 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012646
Owen Andersonbc146b02010-09-21 20:42:50 +000012647 // Fallback case.
12648 return 1;
12649}
12650
Evan Cheng206ee9d2006-07-07 08:33:52 +000012651/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012652/// node is a GlobalAddress + offset.
12653bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012654 const GlobalValue* &GA,
12655 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012656 if (N->getOpcode() == X86ISD::Wrapper) {
12657 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012658 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012659 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012660 return true;
12661 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012662 }
Evan Chengad4196b2008-05-12 19:56:52 +000012663 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012664}
12665
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012666/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12667/// same as extracting the high 128-bit part of 256-bit vector and then
12668/// inserting the result into the low part of a new 256-bit vector
12669static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12670 EVT VT = SVOp->getValueType(0);
12671 int NumElems = VT.getVectorNumElements();
12672
12673 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12674 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12675 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12676 SVOp->getMaskElt(j) >= 0)
12677 return false;
12678
12679 return true;
12680}
12681
12682/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12683/// same as extracting the low 128-bit part of 256-bit vector and then
12684/// inserting the result into the high part of a new 256-bit vector
12685static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12686 EVT VT = SVOp->getValueType(0);
12687 int NumElems = VT.getVectorNumElements();
12688
12689 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12690 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12691 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12692 SVOp->getMaskElt(j) >= 0)
12693 return false;
12694
12695 return true;
12696}
12697
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012698/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12699static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12700 TargetLowering::DAGCombinerInfo &DCI) {
12701 DebugLoc dl = N->getDebugLoc();
12702 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12703 SDValue V1 = SVOp->getOperand(0);
12704 SDValue V2 = SVOp->getOperand(1);
12705 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012706 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012707
12708 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12709 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12710 //
12711 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012712 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012713 // V UNDEF BUILD_VECTOR UNDEF
12714 // \ / \ /
12715 // CONCAT_VECTOR CONCAT_VECTOR
12716 // \ /
12717 // \ /
12718 // RESULT: V + zero extended
12719 //
12720 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12721 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12722 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12723 return SDValue();
12724
12725 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12726 return SDValue();
12727
12728 // To match the shuffle mask, the first half of the mask should
12729 // be exactly the first vector, and all the rest a splat with the
12730 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012731 for (int i = 0; i < NumElems/2; ++i)
12732 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12733 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12734 return SDValue();
12735
12736 // Emit a zeroed vector and insert the desired subvector on its
12737 // first half.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000012738 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012739 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12740 DAG.getConstant(0, MVT::i32), DAG, dl);
12741 return DCI.CombineTo(N, InsV);
12742 }
12743
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012744 //===--------------------------------------------------------------------===//
12745 // Combine some shuffles into subvector extracts and inserts:
12746 //
12747
12748 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12749 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12750 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12751 DAG, dl);
12752 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12753 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12754 return DCI.CombineTo(N, InsV);
12755 }
12756
12757 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12758 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12759 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12760 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12761 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12762 return DCI.CombineTo(N, InsV);
12763 }
12764
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012765 return SDValue();
12766}
12767
12768/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012769static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012770 TargetLowering::DAGCombinerInfo &DCI,
12771 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012772 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012773 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012774
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012775 // Don't create instructions with illegal types after legalize types has run.
12776 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12777 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12778 return SDValue();
12779
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012780 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12781 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12782 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012783 return PerformShuffleCombine256(N, DAG, DCI);
12784
12785 // Only handle 128 wide vector from here on.
12786 if (VT.getSizeInBits() != 128)
12787 return SDValue();
12788
12789 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12790 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12791 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012792 SmallVector<SDValue, 16> Elts;
12793 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012794 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012795
Nate Begemanfdea31a2010-03-24 20:49:50 +000012796 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012797}
Evan Chengd880b972008-05-09 21:53:03 +000012798
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012799/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12800/// generation and convert it from being a bunch of shuffles and extracts
12801/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012802static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12803 const TargetLowering &TLI) {
12804 SDValue InputVector = N->getOperand(0);
12805
12806 // Only operate on vectors of 4 elements, where the alternative shuffling
12807 // gets to be more expensive.
12808 if (InputVector.getValueType() != MVT::v4i32)
12809 return SDValue();
12810
12811 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12812 // single use which is a sign-extend or zero-extend, and all elements are
12813 // used.
12814 SmallVector<SDNode *, 4> Uses;
12815 unsigned ExtractedElements = 0;
12816 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12817 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12818 if (UI.getUse().getResNo() != InputVector.getResNo())
12819 return SDValue();
12820
12821 SDNode *Extract = *UI;
12822 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12823 return SDValue();
12824
12825 if (Extract->getValueType(0) != MVT::i32)
12826 return SDValue();
12827 if (!Extract->hasOneUse())
12828 return SDValue();
12829 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12830 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12831 return SDValue();
12832 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12833 return SDValue();
12834
12835 // Record which element was extracted.
12836 ExtractedElements |=
12837 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12838
12839 Uses.push_back(Extract);
12840 }
12841
12842 // If not all the elements were used, this may not be worthwhile.
12843 if (ExtractedElements != 15)
12844 return SDValue();
12845
12846 // Ok, we've now decided to do the transformation.
12847 DebugLoc dl = InputVector.getDebugLoc();
12848
12849 // Store the value to a temporary stack slot.
12850 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012851 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12852 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012853
12854 // Replace each use (extract) with a load of the appropriate element.
12855 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12856 UE = Uses.end(); UI != UE; ++UI) {
12857 SDNode *Extract = *UI;
12858
Nadav Rotem86694292011-05-17 08:31:57 +000012859 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012860 SDValue Idx = Extract->getOperand(1);
12861 unsigned EltSize =
12862 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12863 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12864 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12865
Nadav Rotem86694292011-05-17 08:31:57 +000012866 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012867 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012868
12869 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012870 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012871 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000012872 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012873
12874 // Replace the exact with the load.
12875 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12876 }
12877
12878 // The replacement was made in place; don't return anything.
12879 return SDValue();
12880}
12881
Duncan Sands6bcd2192011-09-17 16:49:39 +000012882/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
12883/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012884static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012885 const X86Subtarget *Subtarget) {
12886 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012887 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012888 // Get the LHS/RHS of the select.
12889 SDValue LHS = N->getOperand(1);
12890 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000012891 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000012892
Dan Gohman670e5392009-09-21 18:03:22 +000012893 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012894 // instructions match the semantics of the common C idiom x<y?x:y but not
12895 // x<=y?x:y, because of how they handle negative zero (which can be
12896 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000012897 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
12898 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
12899 (Subtarget->hasXMMInt() ||
12900 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012901 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012902
Chris Lattner47b4ce82009-03-11 05:48:52 +000012903 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000012904 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000012905 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12906 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012907 switch (CC) {
12908 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012909 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012910 // Converting this to a min would handle NaNs incorrectly, and swapping
12911 // the operands would cause it to handle comparisons between positive
12912 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012913 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012914 if (!UnsafeFPMath &&
12915 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12916 break;
12917 std::swap(LHS, RHS);
12918 }
Dan Gohman670e5392009-09-21 18:03:22 +000012919 Opcode = X86ISD::FMIN;
12920 break;
12921 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012922 // Converting this to a min would handle comparisons between positive
12923 // and negative zero incorrectly.
12924 if (!UnsafeFPMath &&
12925 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12926 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012927 Opcode = X86ISD::FMIN;
12928 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012929 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012930 // Converting this to a min would handle both negative zeros and NaNs
12931 // incorrectly, but we can swap the operands to fix both.
12932 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012933 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012934 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012935 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012936 Opcode = X86ISD::FMIN;
12937 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012938
Dan Gohman670e5392009-09-21 18:03:22 +000012939 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012940 // Converting this to a max would handle comparisons between positive
12941 // and negative zero incorrectly.
12942 if (!UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000012943 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012944 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012945 Opcode = X86ISD::FMAX;
12946 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012947 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012948 // Converting this to a max would handle NaNs incorrectly, and swapping
12949 // the operands would cause it to handle comparisons between positive
12950 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012951 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012952 if (!UnsafeFPMath &&
12953 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12954 break;
12955 std::swap(LHS, RHS);
12956 }
Dan Gohman670e5392009-09-21 18:03:22 +000012957 Opcode = X86ISD::FMAX;
12958 break;
12959 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012960 // Converting this to a max would handle both negative zeros and NaNs
12961 // incorrectly, but we can swap the operands to fix both.
12962 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012963 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012964 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012965 case ISD::SETGE:
12966 Opcode = X86ISD::FMAX;
12967 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000012968 }
Dan Gohman670e5392009-09-21 18:03:22 +000012969 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000012970 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
12971 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012972 switch (CC) {
12973 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012974 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012975 // Converting this to a min would handle comparisons between positive
12976 // and negative zero incorrectly, and swapping the operands would
12977 // cause it to handle NaNs incorrectly.
12978 if (!UnsafeFPMath &&
12979 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000012980 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012981 break;
12982 std::swap(LHS, RHS);
12983 }
Dan Gohman670e5392009-09-21 18:03:22 +000012984 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000012985 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012986 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012987 // Converting this to a min would handle NaNs incorrectly.
12988 if (!UnsafeFPMath &&
12989 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
12990 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012991 Opcode = X86ISD::FMIN;
12992 break;
12993 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012994 // Converting this to a min would handle both negative zeros and NaNs
12995 // incorrectly, but we can swap the operands to fix both.
12996 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012997 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012998 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012999 case ISD::SETGE:
13000 Opcode = X86ISD::FMIN;
13001 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013002
Dan Gohman670e5392009-09-21 18:03:22 +000013003 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013004 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013005 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013006 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013007 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000013008 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013009 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013010 // Converting this to a max would handle comparisons between positive
13011 // and negative zero incorrectly, and swapping the operands would
13012 // cause it to handle NaNs incorrectly.
13013 if (!UnsafeFPMath &&
13014 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000013015 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013016 break;
13017 std::swap(LHS, RHS);
13018 }
Dan Gohman670e5392009-09-21 18:03:22 +000013019 Opcode = X86ISD::FMAX;
13020 break;
13021 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013022 // Converting this to a max would handle both negative zeros and NaNs
13023 // incorrectly, but we can swap the operands to fix both.
13024 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013025 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013026 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013027 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013028 Opcode = X86ISD::FMAX;
13029 break;
13030 }
Chris Lattner83e6c992006-10-04 06:57:07 +000013031 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013032
Chris Lattner47b4ce82009-03-11 05:48:52 +000013033 if (Opcode)
13034 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000013035 }
Eric Christopherfd179292009-08-27 18:07:15 +000013036
Chris Lattnerd1980a52009-03-12 06:52:53 +000013037 // If this is a select between two integer constants, try to do some
13038 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000013039 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
13040 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000013041 // Don't do this for crazy integer types.
13042 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
13043 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000013044 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013045 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000013046
Chris Lattnercee56e72009-03-13 05:53:31 +000013047 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000013048 // Efficiently invertible.
13049 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
13050 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
13051 isa<ConstantSDNode>(Cond.getOperand(1))))) {
13052 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000013053 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013054 }
Eric Christopherfd179292009-08-27 18:07:15 +000013055
Chris Lattnerd1980a52009-03-12 06:52:53 +000013056 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013057 if (FalseC->getAPIntValue() == 0 &&
13058 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013059 if (NeedsCondInvert) // Invert the condition if needed.
13060 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13061 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013062
Chris Lattnerd1980a52009-03-12 06:52:53 +000013063 // Zero extend the condition if needed.
13064 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013065
Chris Lattnercee56e72009-03-13 05:53:31 +000013066 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013067 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013068 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013069 }
Eric Christopherfd179292009-08-27 18:07:15 +000013070
Chris Lattner97a29a52009-03-13 05:22:11 +000013071 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013072 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013073 if (NeedsCondInvert) // Invert the condition if needed.
13074 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13075 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013076
Chris Lattner97a29a52009-03-13 05:22:11 +000013077 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013078 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13079 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013080 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013081 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013082 }
Eric Christopherfd179292009-08-27 18:07:15 +000013083
Chris Lattnercee56e72009-03-13 05:53:31 +000013084 // Optimize cases that will turn into an LEA instruction. This requires
13085 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013086 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013087 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013088 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013089
Chris Lattnercee56e72009-03-13 05:53:31 +000013090 bool isFastMultiplier = false;
13091 if (Diff < 10) {
13092 switch ((unsigned char)Diff) {
13093 default: break;
13094 case 1: // result = add base, cond
13095 case 2: // result = lea base( , cond*2)
13096 case 3: // result = lea base(cond, cond*2)
13097 case 4: // result = lea base( , cond*4)
13098 case 5: // result = lea base(cond, cond*4)
13099 case 8: // result = lea base( , cond*8)
13100 case 9: // result = lea base(cond, cond*8)
13101 isFastMultiplier = true;
13102 break;
13103 }
13104 }
Eric Christopherfd179292009-08-27 18:07:15 +000013105
Chris Lattnercee56e72009-03-13 05:53:31 +000013106 if (isFastMultiplier) {
13107 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
13108 if (NeedsCondInvert) // Invert the condition if needed.
13109 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13110 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013111
Chris Lattnercee56e72009-03-13 05:53:31 +000013112 // Zero extend the condition if needed.
13113 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13114 Cond);
13115 // Scale the condition by the difference.
13116 if (Diff != 1)
13117 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13118 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013119
Chris Lattnercee56e72009-03-13 05:53:31 +000013120 // Add the base if non-zero.
13121 if (FalseC->getAPIntValue() != 0)
13122 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13123 SDValue(FalseC, 0));
13124 return Cond;
13125 }
Eric Christopherfd179292009-08-27 18:07:15 +000013126 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013127 }
13128 }
Eric Christopherfd179292009-08-27 18:07:15 +000013129
Dan Gohman475871a2008-07-27 21:46:04 +000013130 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000013131}
13132
Chris Lattnerd1980a52009-03-12 06:52:53 +000013133/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
13134static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
13135 TargetLowering::DAGCombinerInfo &DCI) {
13136 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000013137
Chris Lattnerd1980a52009-03-12 06:52:53 +000013138 // If the flag operand isn't dead, don't touch this CMOV.
13139 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
13140 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000013141
Evan Chengb5a55d92011-05-24 01:48:22 +000013142 SDValue FalseOp = N->getOperand(0);
13143 SDValue TrueOp = N->getOperand(1);
13144 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
13145 SDValue Cond = N->getOperand(3);
13146 if (CC == X86::COND_E || CC == X86::COND_NE) {
13147 switch (Cond.getOpcode()) {
13148 default: break;
13149 case X86ISD::BSR:
13150 case X86ISD::BSF:
13151 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
13152 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
13153 return (CC == X86::COND_E) ? FalseOp : TrueOp;
13154 }
13155 }
13156
Chris Lattnerd1980a52009-03-12 06:52:53 +000013157 // If this is a select between two integer constants, try to do some
13158 // optimizations. Note that the operands are ordered the opposite of SELECT
13159 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000013160 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
13161 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013162 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
13163 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000013164 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
13165 CC = X86::GetOppositeBranchCondition(CC);
13166 std::swap(TrueC, FalseC);
13167 }
Eric Christopherfd179292009-08-27 18:07:15 +000013168
Chris Lattnerd1980a52009-03-12 06:52:53 +000013169 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013170 // This is efficient for any integer data type (including i8/i16) and
13171 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013172 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013173 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13174 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013175
Chris Lattnerd1980a52009-03-12 06:52:53 +000013176 // Zero extend the condition if needed.
13177 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013178
Chris Lattnerd1980a52009-03-12 06:52:53 +000013179 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
13180 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013181 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013182 if (N->getNumValues() == 2) // Dead flag value?
13183 return DCI.CombineTo(N, Cond, SDValue());
13184 return Cond;
13185 }
Eric Christopherfd179292009-08-27 18:07:15 +000013186
Chris Lattnercee56e72009-03-13 05:53:31 +000013187 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
13188 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000013189 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013190 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13191 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013192
Chris Lattner97a29a52009-03-13 05:22:11 +000013193 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013194 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13195 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013196 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13197 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000013198
Chris Lattner97a29a52009-03-13 05:22:11 +000013199 if (N->getNumValues() == 2) // Dead flag value?
13200 return DCI.CombineTo(N, Cond, SDValue());
13201 return Cond;
13202 }
Eric Christopherfd179292009-08-27 18:07:15 +000013203
Chris Lattnercee56e72009-03-13 05:53:31 +000013204 // Optimize cases that will turn into an LEA instruction. This requires
13205 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013206 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013207 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013208 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013209
Chris Lattnercee56e72009-03-13 05:53:31 +000013210 bool isFastMultiplier = false;
13211 if (Diff < 10) {
13212 switch ((unsigned char)Diff) {
13213 default: break;
13214 case 1: // result = add base, cond
13215 case 2: // result = lea base( , cond*2)
13216 case 3: // result = lea base(cond, cond*2)
13217 case 4: // result = lea base( , cond*4)
13218 case 5: // result = lea base(cond, cond*4)
13219 case 8: // result = lea base( , cond*8)
13220 case 9: // result = lea base(cond, cond*8)
13221 isFastMultiplier = true;
13222 break;
13223 }
13224 }
Eric Christopherfd179292009-08-27 18:07:15 +000013225
Chris Lattnercee56e72009-03-13 05:53:31 +000013226 if (isFastMultiplier) {
13227 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013228 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13229 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000013230 // Zero extend the condition if needed.
13231 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13232 Cond);
13233 // Scale the condition by the difference.
13234 if (Diff != 1)
13235 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13236 DAG.getConstant(Diff, Cond.getValueType()));
13237
13238 // Add the base if non-zero.
13239 if (FalseC->getAPIntValue() != 0)
13240 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13241 SDValue(FalseC, 0));
13242 if (N->getNumValues() == 2) // Dead flag value?
13243 return DCI.CombineTo(N, Cond, SDValue());
13244 return Cond;
13245 }
Eric Christopherfd179292009-08-27 18:07:15 +000013246 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013247 }
13248 }
13249 return SDValue();
13250}
13251
13252
Evan Cheng0b0cd912009-03-28 05:57:29 +000013253/// PerformMulCombine - Optimize a single multiply with constant into two
13254/// in order to implement it with two cheaper instructions, e.g.
13255/// LEA + SHL, LEA + LEA.
13256static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
13257 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000013258 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
13259 return SDValue();
13260
Owen Andersone50ed302009-08-10 22:56:29 +000013261 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000013262 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000013263 return SDValue();
13264
13265 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
13266 if (!C)
13267 return SDValue();
13268 uint64_t MulAmt = C->getZExtValue();
13269 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
13270 return SDValue();
13271
13272 uint64_t MulAmt1 = 0;
13273 uint64_t MulAmt2 = 0;
13274 if ((MulAmt % 9) == 0) {
13275 MulAmt1 = 9;
13276 MulAmt2 = MulAmt / 9;
13277 } else if ((MulAmt % 5) == 0) {
13278 MulAmt1 = 5;
13279 MulAmt2 = MulAmt / 5;
13280 } else if ((MulAmt % 3) == 0) {
13281 MulAmt1 = 3;
13282 MulAmt2 = MulAmt / 3;
13283 }
13284 if (MulAmt2 &&
13285 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
13286 DebugLoc DL = N->getDebugLoc();
13287
13288 if (isPowerOf2_64(MulAmt2) &&
13289 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
13290 // If second multiplifer is pow2, issue it first. We want the multiply by
13291 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
13292 // is an add.
13293 std::swap(MulAmt1, MulAmt2);
13294
13295 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000013296 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013297 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000013298 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000013299 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013300 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000013301 DAG.getConstant(MulAmt1, VT));
13302
Eric Christopherfd179292009-08-27 18:07:15 +000013303 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013304 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000013305 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000013306 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013307 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000013308 DAG.getConstant(MulAmt2, VT));
13309
13310 // Do not add new nodes to DAG combiner worklist.
13311 DCI.CombineTo(N, NewMul, false);
13312 }
13313 return SDValue();
13314}
13315
Evan Chengad9c0a32009-12-15 00:53:42 +000013316static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
13317 SDValue N0 = N->getOperand(0);
13318 SDValue N1 = N->getOperand(1);
13319 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13320 EVT VT = N0.getValueType();
13321
13322 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13323 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013324 if (VT.isInteger() && !VT.isVector() &&
13325 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000013326 N0.getOperand(1).getOpcode() == ISD::Constant) {
13327 SDValue N00 = N0.getOperand(0);
13328 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13329 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13330 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13331 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13332 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13333 APInt ShAmt = N1C->getAPIntValue();
13334 Mask = Mask.shl(ShAmt);
13335 if (Mask != 0)
13336 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13337 N00, DAG.getConstant(Mask, VT));
13338 }
13339 }
13340
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013341
13342 // Hardware support for vector shifts is sparse which makes us scalarize the
13343 // vector operations in many cases. Also, on sandybridge ADD is faster than
13344 // shl.
13345 // (shl V, 1) -> add V,V
13346 if (isSplatVector(N1.getNode())) {
13347 assert(N0.getValueType().isVector() && "Invalid vector shift type");
13348 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
13349 // We shift all of the values by one. In many cases we do not have
13350 // hardware support for this operation. This is better expressed as an ADD
13351 // of two values.
13352 if (N1C && (1 == N1C->getZExtValue())) {
13353 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
13354 }
13355 }
13356
Evan Chengad9c0a32009-12-15 00:53:42 +000013357 return SDValue();
13358}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013359
Nate Begeman740ab032009-01-26 00:52:55 +000013360/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13361/// when possible.
13362static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13363 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013364 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013365 if (N->getOpcode() == ISD::SHL) {
13366 SDValue V = PerformSHLCombine(N, DAG);
13367 if (V.getNode()) return V;
13368 }
Evan Chengad9c0a32009-12-15 00:53:42 +000013369
Nate Begeman740ab032009-01-26 00:52:55 +000013370 // On X86 with SSE2 support, we can transform this to a vector shift if
13371 // all elements are shifted by the same amount. We can't do this in legalize
13372 // because the a constant vector is typically transformed to a constant pool
13373 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013374 if (!Subtarget->hasXMMInt())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013375 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013376
Owen Anderson825b72b2009-08-11 20:47:22 +000013377 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013378 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013379
Mon P Wang3becd092009-01-28 08:12:05 +000013380 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013381 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013382 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013383 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013384 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13385 unsigned NumElts = VT.getVectorNumElements();
13386 unsigned i = 0;
13387 for (; i != NumElts; ++i) {
13388 SDValue Arg = ShAmtOp.getOperand(i);
13389 if (Arg.getOpcode() == ISD::UNDEF) continue;
13390 BaseShAmt = Arg;
13391 break;
13392 }
13393 for (; i != NumElts; ++i) {
13394 SDValue Arg = ShAmtOp.getOperand(i);
13395 if (Arg.getOpcode() == ISD::UNDEF) continue;
13396 if (Arg != BaseShAmt) {
13397 return SDValue();
13398 }
13399 }
13400 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013401 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013402 SDValue InVec = ShAmtOp.getOperand(0);
13403 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13404 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13405 unsigned i = 0;
13406 for (; i != NumElts; ++i) {
13407 SDValue Arg = InVec.getOperand(i);
13408 if (Arg.getOpcode() == ISD::UNDEF) continue;
13409 BaseShAmt = Arg;
13410 break;
13411 }
13412 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13413 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013414 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013415 if (C->getZExtValue() == SplatIdx)
13416 BaseShAmt = InVec.getOperand(1);
13417 }
13418 }
13419 if (BaseShAmt.getNode() == 0)
13420 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13421 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013422 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013423 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013424
Mon P Wangefa42202009-09-03 19:56:25 +000013425 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013426 if (EltVT.bitsGT(MVT::i32))
13427 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13428 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013429 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013430
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013431 // The shift amount is identical so we can do a vector shift.
13432 SDValue ValOp = N->getOperand(0);
13433 switch (N->getOpcode()) {
13434 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013435 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013436 break;
13437 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013438 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013439 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013440 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013441 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013442 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013443 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013444 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013445 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013446 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013447 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013448 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013449 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013450 break;
13451 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013452 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013453 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013454 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013455 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013456 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013457 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013458 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013459 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013460 break;
13461 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013462 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013463 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013464 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013465 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013466 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013467 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013468 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013469 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013470 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013471 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013472 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013473 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013474 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013475 }
13476 return SDValue();
13477}
13478
Nate Begemanb65c1752010-12-17 22:55:37 +000013479
Stuart Hastings865f0932011-06-03 23:53:54 +000013480// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13481// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13482// and friends. Likewise for OR -> CMPNEQSS.
13483static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13484 TargetLowering::DAGCombinerInfo &DCI,
13485 const X86Subtarget *Subtarget) {
13486 unsigned opcode;
13487
13488 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13489 // we're requiring SSE2 for both.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013490 if (Subtarget->hasXMMInt() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013491 SDValue N0 = N->getOperand(0);
13492 SDValue N1 = N->getOperand(1);
13493 SDValue CMP0 = N0->getOperand(1);
13494 SDValue CMP1 = N1->getOperand(1);
13495 DebugLoc DL = N->getDebugLoc();
13496
13497 // The SETCCs should both refer to the same CMP.
13498 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13499 return SDValue();
13500
13501 SDValue CMP00 = CMP0->getOperand(0);
13502 SDValue CMP01 = CMP0->getOperand(1);
13503 EVT VT = CMP00.getValueType();
13504
13505 if (VT == MVT::f32 || VT == MVT::f64) {
13506 bool ExpectingFlags = false;
13507 // Check for any users that want flags:
13508 for (SDNode::use_iterator UI = N->use_begin(),
13509 UE = N->use_end();
13510 !ExpectingFlags && UI != UE; ++UI)
13511 switch (UI->getOpcode()) {
13512 default:
13513 case ISD::BR_CC:
13514 case ISD::BRCOND:
13515 case ISD::SELECT:
13516 ExpectingFlags = true;
13517 break;
13518 case ISD::CopyToReg:
13519 case ISD::SIGN_EXTEND:
13520 case ISD::ZERO_EXTEND:
13521 case ISD::ANY_EXTEND:
13522 break;
13523 }
13524
13525 if (!ExpectingFlags) {
13526 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13527 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13528
13529 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13530 X86::CondCode tmp = cc0;
13531 cc0 = cc1;
13532 cc1 = tmp;
13533 }
13534
13535 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13536 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13537 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13538 X86ISD::NodeType NTOperator = is64BitFP ?
13539 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13540 // FIXME: need symbolic constants for these magic numbers.
13541 // See X86ATTInstPrinter.cpp:printSSECC().
13542 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13543 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13544 DAG.getConstant(x86cc, MVT::i8));
13545 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13546 OnesOrZeroesF);
13547 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13548 DAG.getConstant(1, MVT::i32));
13549 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13550 return OneBitOfTruth;
13551 }
13552 }
13553 }
13554 }
13555 return SDValue();
13556}
13557
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013558/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13559/// so it can be folded inside ANDNP.
13560static bool CanFoldXORWithAllOnes(const SDNode *N) {
13561 EVT VT = N->getValueType(0);
13562
13563 // Match direct AllOnes for 128 and 256-bit vectors
13564 if (ISD::isBuildVectorAllOnes(N))
13565 return true;
13566
13567 // Look through a bit convert.
13568 if (N->getOpcode() == ISD::BITCAST)
13569 N = N->getOperand(0).getNode();
13570
13571 // Sometimes the operand may come from a insert_subvector building a 256-bit
13572 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013573 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013574 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13575 SDValue V1 = N->getOperand(0);
13576 SDValue V2 = N->getOperand(1);
13577
13578 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13579 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13580 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13581 ISD::isBuildVectorAllOnes(V2.getNode()))
13582 return true;
13583 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013584
13585 return false;
13586}
13587
Nate Begemanb65c1752010-12-17 22:55:37 +000013588static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13589 TargetLowering::DAGCombinerInfo &DCI,
13590 const X86Subtarget *Subtarget) {
13591 if (DCI.isBeforeLegalizeOps())
13592 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013593
Stuart Hastings865f0932011-06-03 23:53:54 +000013594 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13595 if (R.getNode())
13596 return R;
13597
Craig Topper54a11172011-10-14 07:06:56 +000013598 EVT VT = N->getValueType(0);
13599
Craig Topperb4c94572011-10-21 06:55:01 +000013600 // Create ANDN, BLSI, and BLSR instructions
13601 // BLSI is X & (-X)
13602 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000013603 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
13604 SDValue N0 = N->getOperand(0);
13605 SDValue N1 = N->getOperand(1);
13606 DebugLoc DL = N->getDebugLoc();
13607
13608 // Check LHS for not
13609 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
13610 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
13611 // Check RHS for not
13612 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
13613 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
13614
Craig Topperb4c94572011-10-21 06:55:01 +000013615 // Check LHS for neg
13616 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
13617 isZero(N0.getOperand(0)))
13618 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
13619
13620 // Check RHS for neg
13621 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
13622 isZero(N1.getOperand(0)))
13623 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
13624
13625 // Check LHS for X-1
13626 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13627 isAllOnes(N0.getOperand(1)))
13628 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
13629
13630 // Check RHS for X-1
13631 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13632 isAllOnes(N1.getOperand(1)))
13633 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
13634
Craig Topper54a11172011-10-14 07:06:56 +000013635 return SDValue();
13636 }
13637
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013638 // Want to form ANDNP nodes:
13639 // 1) In the hopes of then easily combining them with OR and AND nodes
13640 // to form PBLEND/PSIGN.
13641 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013642 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013643 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013644
Nate Begemanb65c1752010-12-17 22:55:37 +000013645 SDValue N0 = N->getOperand(0);
13646 SDValue N1 = N->getOperand(1);
13647 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013648
Nate Begemanb65c1752010-12-17 22:55:37 +000013649 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013650 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013651 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13652 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013653 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013654
13655 // Check RHS for vnot
13656 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013657 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13658 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013659 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013660
Nate Begemanb65c1752010-12-17 22:55:37 +000013661 return SDValue();
13662}
13663
Evan Cheng760d1942010-01-04 21:22:48 +000013664static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013665 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013666 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013667 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013668 return SDValue();
13669
Stuart Hastings865f0932011-06-03 23:53:54 +000013670 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13671 if (R.getNode())
13672 return R;
13673
Evan Cheng760d1942010-01-04 21:22:48 +000013674 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000013675 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000013676 return SDValue();
13677
Evan Cheng760d1942010-01-04 21:22:48 +000013678 SDValue N0 = N->getOperand(0);
13679 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013680
Nate Begemanb65c1752010-12-17 22:55:37 +000013681 // look for psign/blend
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013682 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013683 if (VT == MVT::v2i64) {
13684 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013685 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000013686 std::swap(N0, N1);
13687 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013688 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013689 SDValue Mask = N1.getOperand(0);
13690 SDValue X = N1.getOperand(1);
13691 SDValue Y;
13692 if (N0.getOperand(0) == Mask)
13693 Y = N0.getOperand(1);
13694 if (N0.getOperand(1) == Mask)
13695 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013696
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013697 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000013698 if (!Y.getNode())
13699 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013700
Nate Begemanb65c1752010-12-17 22:55:37 +000013701 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13702 if (Mask.getOpcode() != ISD::BITCAST ||
13703 X.getOpcode() != ISD::BITCAST ||
13704 Y.getOpcode() != ISD::BITCAST)
13705 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013706
Nate Begemanb65c1752010-12-17 22:55:37 +000013707 // Look through mask bitcast.
13708 Mask = Mask.getOperand(0);
13709 EVT MaskVT = Mask.getValueType();
13710
13711 // Validate that the Mask operand is a vector sra node. The sra node
13712 // will be an intrinsic.
13713 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13714 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013715
Nate Begemanb65c1752010-12-17 22:55:37 +000013716 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13717 // there is no psrai.b
13718 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13719 case Intrinsic::x86_sse2_psrai_w:
13720 case Intrinsic::x86_sse2_psrai_d:
13721 break;
13722 default: return SDValue();
13723 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013724
Nate Begemanb65c1752010-12-17 22:55:37 +000013725 // Check that the SRA is all signbits.
13726 SDValue SraC = Mask.getOperand(2);
13727 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13728 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13729 if ((SraAmt + 1) != EltBits)
13730 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013731
Nate Begemanb65c1752010-12-17 22:55:37 +000013732 DebugLoc DL = N->getDebugLoc();
13733
13734 // Now we know we at least have a plendvb with the mask val. See if
13735 // we can form a psignb/w/d.
13736 // psign = x.type == y.type == mask.type && y = sub(0, x);
13737 X = X.getOperand(0);
13738 Y = Y.getOperand(0);
13739 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13740 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
13741 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
13742 unsigned Opc = 0;
13743 switch (EltBits) {
13744 case 8: Opc = X86ISD::PSIGNB; break;
13745 case 16: Opc = X86ISD::PSIGNW; break;
13746 case 32: Opc = X86ISD::PSIGND; break;
13747 default: break;
13748 }
13749 if (Opc) {
13750 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
13751 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
13752 }
13753 }
13754 // PBLENDVB only available on SSE 4.1
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013755 if (!(Subtarget->hasSSE41() || Subtarget->hasAVX()))
Nate Begemanb65c1752010-12-17 22:55:37 +000013756 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013757
Nate Begemanb65c1752010-12-17 22:55:37 +000013758 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
13759 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
13760 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000013761 Mask = DAG.getNode(ISD::VSELECT, DL, MVT::v16i8, Mask, X, Y);
Nate Begemanb65c1752010-12-17 22:55:37 +000013762 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
13763 }
13764 }
13765 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013766
Nate Begemanb65c1752010-12-17 22:55:37 +000013767 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013768 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13769 std::swap(N0, N1);
13770 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13771 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013772 if (!N0.hasOneUse() || !N1.hasOneUse())
13773 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013774
13775 SDValue ShAmt0 = N0.getOperand(1);
13776 if (ShAmt0.getValueType() != MVT::i8)
13777 return SDValue();
13778 SDValue ShAmt1 = N1.getOperand(1);
13779 if (ShAmt1.getValueType() != MVT::i8)
13780 return SDValue();
13781 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13782 ShAmt0 = ShAmt0.getOperand(0);
13783 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13784 ShAmt1 = ShAmt1.getOperand(0);
13785
13786 DebugLoc DL = N->getDebugLoc();
13787 unsigned Opc = X86ISD::SHLD;
13788 SDValue Op0 = N0.getOperand(0);
13789 SDValue Op1 = N1.getOperand(0);
13790 if (ShAmt0.getOpcode() == ISD::SUB) {
13791 Opc = X86ISD::SHRD;
13792 std::swap(Op0, Op1);
13793 std::swap(ShAmt0, ShAmt1);
13794 }
13795
Evan Cheng8b1190a2010-04-28 01:18:01 +000013796 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013797 if (ShAmt1.getOpcode() == ISD::SUB) {
13798 SDValue Sum = ShAmt1.getOperand(0);
13799 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013800 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13801 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13802 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13803 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013804 return DAG.getNode(Opc, DL, VT,
13805 Op0, Op1,
13806 DAG.getNode(ISD::TRUNCATE, DL,
13807 MVT::i8, ShAmt0));
13808 }
13809 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13810 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13811 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013812 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013813 return DAG.getNode(Opc, DL, VT,
13814 N0.getOperand(0), N1.getOperand(0),
13815 DAG.getNode(ISD::TRUNCATE, DL,
13816 MVT::i8, ShAmt0));
13817 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013818
Evan Cheng760d1942010-01-04 21:22:48 +000013819 return SDValue();
13820}
13821
Craig Topperb4c94572011-10-21 06:55:01 +000013822static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
13823 TargetLowering::DAGCombinerInfo &DCI,
13824 const X86Subtarget *Subtarget) {
13825 if (DCI.isBeforeLegalizeOps())
13826 return SDValue();
13827
13828 EVT VT = N->getValueType(0);
13829
13830 if (VT != MVT::i32 && VT != MVT::i64)
13831 return SDValue();
13832
13833 // Create BLSMSK instructions by finding X ^ (X-1)
13834 SDValue N0 = N->getOperand(0);
13835 SDValue N1 = N->getOperand(1);
13836 DebugLoc DL = N->getDebugLoc();
13837
13838 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13839 isAllOnes(N0.getOperand(1)))
13840 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
13841
13842 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13843 isAllOnes(N1.getOperand(1)))
13844 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
13845
13846 return SDValue();
13847}
13848
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013849/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
13850static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
13851 const X86Subtarget *Subtarget) {
13852 LoadSDNode *Ld = cast<LoadSDNode>(N);
13853 EVT RegVT = Ld->getValueType(0);
13854 EVT MemVT = Ld->getMemoryVT();
13855 DebugLoc dl = Ld->getDebugLoc();
13856 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13857
13858 ISD::LoadExtType Ext = Ld->getExtensionType();
13859
Nadav Rotemca6f2962011-09-18 19:00:23 +000013860 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013861 // shuffle. We need SSE4 for the shuffles.
13862 // TODO: It is possible to support ZExt by zeroing the undef values
13863 // during the shuffle phase or after the shuffle.
13864 if (RegVT.isVector() && Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
13865 assert(MemVT != RegVT && "Cannot extend to the same type");
13866 assert(MemVT.isVector() && "Must load a vector from memory");
13867
13868 unsigned NumElems = RegVT.getVectorNumElements();
13869 unsigned RegSz = RegVT.getSizeInBits();
13870 unsigned MemSz = MemVT.getSizeInBits();
13871 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000013872 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013873 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
13874
13875 // Attempt to load the original value using a single load op.
13876 // Find a scalar type which is equal to the loaded word size.
13877 MVT SclrLoadTy = MVT::i8;
13878 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13879 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13880 MVT Tp = (MVT::SimpleValueType)tp;
13881 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() == MemSz) {
13882 SclrLoadTy = Tp;
13883 break;
13884 }
13885 }
13886
13887 // Proceed if a load word is found.
13888 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
13889
13890 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
13891 RegSz/SclrLoadTy.getSizeInBits());
13892
13893 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
13894 RegSz/MemVT.getScalarType().getSizeInBits());
13895 // Can't shuffle using an illegal type.
13896 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13897
13898 // Perform a single load.
13899 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
13900 Ld->getBasePtr(),
13901 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013902 Ld->isNonTemporal(), Ld->isInvariant(),
13903 Ld->getAlignment());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013904
13905 // Insert the word loaded into a vector.
13906 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
13907 LoadUnitVecVT, ScalarLoad);
13908
13909 // Bitcast the loaded value to a vector of the original element type, in
13910 // the size of the target vector type.
13911 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, ScalarInVector);
13912 unsigned SizeRatio = RegSz/MemSz;
13913
13914 // Redistribute the loaded elements into the different locations.
13915 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13916 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
13917
13918 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
13919 DAG.getUNDEF(SlicedVec.getValueType()),
13920 ShuffleVec.data());
13921
13922 // Bitcast to the requested type.
13923 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
13924 // Replace the original load with the new sequence
13925 // and return the new chain.
13926 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
13927 return SDValue(ScalarLoad.getNode(), 1);
13928 }
13929
13930 return SDValue();
13931}
13932
Chris Lattner149a4e52008-02-22 02:09:43 +000013933/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013934static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000013935 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000013936 StoreSDNode *St = cast<StoreSDNode>(N);
13937 EVT VT = St->getValue().getValueType();
13938 EVT StVT = St->getMemoryVT();
13939 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000013940 SDValue StoredVal = St->getOperand(1);
13941 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13942
13943 // If we are saving a concatination of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000013944 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
13945 // 128-bit ones. If in the future the cost becomes only one memory access the
13946 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000013947 if (VT.getSizeInBits() == 256 &&
13948 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
13949 StoredVal.getNumOperands() == 2) {
13950
13951 SDValue Value0 = StoredVal.getOperand(0);
13952 SDValue Value1 = StoredVal.getOperand(1);
13953
13954 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
13955 SDValue Ptr0 = St->getBasePtr();
13956 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
13957
13958 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
13959 St->getPointerInfo(), St->isVolatile(),
13960 St->isNonTemporal(), St->getAlignment());
13961 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
13962 St->getPointerInfo(), St->isVolatile(),
13963 St->isNonTemporal(), St->getAlignment());
13964 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
13965 }
Nadav Rotem614061b2011-08-10 19:30:14 +000013966
13967 // Optimize trunc store (of multiple scalars) to shuffle and store.
13968 // First, pack all of the elements in one place. Next, store to memory
13969 // in fewer chunks.
13970 if (St->isTruncatingStore() && VT.isVector()) {
13971 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13972 unsigned NumElems = VT.getVectorNumElements();
13973 assert(StVT != VT && "Cannot truncate to the same type");
13974 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
13975 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
13976
13977 // From, To sizes and ElemCount must be pow of two
13978 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013979 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000013980 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013981 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013982
Nadav Rotem614061b2011-08-10 19:30:14 +000013983 unsigned SizeRatio = FromSz / ToSz;
13984
13985 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
13986
13987 // Create a type on which we perform the shuffle
13988 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
13989 StVT.getScalarType(), NumElems*SizeRatio);
13990
13991 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
13992
13993 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
13994 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13995 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
13996
13997 // Can't shuffle using an illegal type
13998 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13999
14000 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
14001 DAG.getUNDEF(WideVec.getValueType()),
14002 ShuffleVec.data());
14003 // At this point all of the data is stored at the bottom of the
14004 // register. We now need to save it to mem.
14005
14006 // Find the largest store unit
14007 MVT StoreType = MVT::i8;
14008 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14009 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14010 MVT Tp = (MVT::SimpleValueType)tp;
14011 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
14012 StoreType = Tp;
14013 }
14014
14015 // Bitcast the original vector into a vector of store-size units
14016 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
14017 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
14018 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
14019 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
14020 SmallVector<SDValue, 8> Chains;
14021 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
14022 TLI.getPointerTy());
14023 SDValue Ptr = St->getBasePtr();
14024
14025 // Perform one or more big stores into memory.
14026 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
14027 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
14028 StoreType, ShuffWide,
14029 DAG.getIntPtrConstant(i));
14030 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
14031 St->getPointerInfo(), St->isVolatile(),
14032 St->isNonTemporal(), St->getAlignment());
14033 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
14034 Chains.push_back(Ch);
14035 }
14036
14037 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
14038 Chains.size());
14039 }
14040
14041
Chris Lattner149a4e52008-02-22 02:09:43 +000014042 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
14043 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000014044 // A preferable solution to the general problem is to figure out the right
14045 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000014046
14047 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000014048 if (VT.getSizeInBits() != 64)
14049 return SDValue();
14050
Devang Patel578efa92009-06-05 21:57:13 +000014051 const Function *F = DAG.getMachineFunction().getFunction();
14052 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000014053 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000014054 && Subtarget->hasXMMInt();
Evan Cheng536e6672009-03-12 05:59:15 +000014055 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000014056 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000014057 isa<LoadSDNode>(St->getValue()) &&
14058 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
14059 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014060 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014061 LoadSDNode *Ld = 0;
14062 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000014063 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000014064 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014065 // Must be a store of a load. We currently handle two cases: the load
14066 // is a direct child, and it's under an intervening TokenFactor. It is
14067 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000014068 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000014069 Ld = cast<LoadSDNode>(St->getChain());
14070 else if (St->getValue().hasOneUse() &&
14071 ChainVal->getOpcode() == ISD::TokenFactor) {
14072 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014073 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000014074 TokenFactorIndex = i;
14075 Ld = cast<LoadSDNode>(St->getValue());
14076 } else
14077 Ops.push_back(ChainVal->getOperand(i));
14078 }
14079 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000014080
Evan Cheng536e6672009-03-12 05:59:15 +000014081 if (!Ld || !ISD::isNormalLoad(Ld))
14082 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014083
Evan Cheng536e6672009-03-12 05:59:15 +000014084 // If this is not the MMX case, i.e. we are just turning i64 load/store
14085 // into f64 load/store, avoid the transformation if there are multiple
14086 // uses of the loaded value.
14087 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
14088 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014089
Evan Cheng536e6672009-03-12 05:59:15 +000014090 DebugLoc LdDL = Ld->getDebugLoc();
14091 DebugLoc StDL = N->getDebugLoc();
14092 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
14093 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
14094 // pair instead.
14095 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014096 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000014097 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
14098 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014099 Ld->isNonTemporal(), Ld->isInvariant(),
14100 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014101 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000014102 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000014103 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000014104 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000014105 Ops.size());
14106 }
Evan Cheng536e6672009-03-12 05:59:15 +000014107 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014108 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014109 St->isVolatile(), St->isNonTemporal(),
14110 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000014111 }
Evan Cheng536e6672009-03-12 05:59:15 +000014112
14113 // Otherwise, lower to two pairs of 32-bit loads / stores.
14114 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014115 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
14116 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014117
Owen Anderson825b72b2009-08-11 20:47:22 +000014118 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014119 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014120 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014121 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000014122 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014123 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000014124 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014125 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000014126 MinAlign(Ld->getAlignment(), 4));
14127
14128 SDValue NewChain = LoLd.getValue(1);
14129 if (TokenFactorIndex != -1) {
14130 Ops.push_back(LoLd);
14131 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000014132 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000014133 Ops.size());
14134 }
14135
14136 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014137 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
14138 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014139
14140 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014141 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014142 St->isVolatile(), St->isNonTemporal(),
14143 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014144 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014145 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000014146 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000014147 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000014148 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000014149 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000014150 }
Dan Gohman475871a2008-07-27 21:46:04 +000014151 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000014152}
14153
Duncan Sands17470be2011-09-22 20:15:48 +000014154/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
14155/// and return the operands for the horizontal operation in LHS and RHS. A
14156/// horizontal operation performs the binary operation on successive elements
14157/// of its first operand, then on successive elements of its second operand,
14158/// returning the resulting values in a vector. For example, if
14159/// A = < float a0, float a1, float a2, float a3 >
14160/// and
14161/// B = < float b0, float b1, float b2, float b3 >
14162/// then the result of doing a horizontal operation on A and B is
14163/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
14164/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
14165/// A horizontal-op B, for some already available A and B, and if so then LHS is
14166/// set to A, RHS to B, and the routine returns 'true'.
14167/// Note that the binary operation should have the property that if one of the
14168/// operands is UNDEF then the result is UNDEF.
14169static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool isCommutative) {
14170 // Look for the following pattern: if
14171 // A = < float a0, float a1, float a2, float a3 >
14172 // B = < float b0, float b1, float b2, float b3 >
14173 // and
14174 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
14175 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
14176 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
14177 // which is A horizontal-op B.
14178
14179 // At least one of the operands should be a vector shuffle.
14180 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
14181 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
14182 return false;
14183
14184 EVT VT = LHS.getValueType();
14185 unsigned N = VT.getVectorNumElements();
14186
14187 // View LHS in the form
14188 // LHS = VECTOR_SHUFFLE A, B, LMask
14189 // If LHS is not a shuffle then pretend it is the shuffle
14190 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
14191 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
14192 // type VT.
14193 SDValue A, B;
14194 SmallVector<int, 8> LMask(N);
14195 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14196 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
14197 A = LHS.getOperand(0);
14198 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
14199 B = LHS.getOperand(1);
14200 cast<ShuffleVectorSDNode>(LHS.getNode())->getMask(LMask);
14201 } else {
14202 if (LHS.getOpcode() != ISD::UNDEF)
14203 A = LHS;
14204 for (unsigned i = 0; i != N; ++i)
14205 LMask[i] = i;
14206 }
14207
14208 // Likewise, view RHS in the form
14209 // RHS = VECTOR_SHUFFLE C, D, RMask
14210 SDValue C, D;
14211 SmallVector<int, 8> RMask(N);
14212 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14213 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
14214 C = RHS.getOperand(0);
14215 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
14216 D = RHS.getOperand(1);
14217 cast<ShuffleVectorSDNode>(RHS.getNode())->getMask(RMask);
14218 } else {
14219 if (RHS.getOpcode() != ISD::UNDEF)
14220 C = RHS;
14221 for (unsigned i = 0; i != N; ++i)
14222 RMask[i] = i;
14223 }
14224
14225 // Check that the shuffles are both shuffling the same vectors.
14226 if (!(A == C && B == D) && !(A == D && B == C))
14227 return false;
14228
14229 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
14230 if (!A.getNode() && !B.getNode())
14231 return false;
14232
14233 // If A and B occur in reverse order in RHS, then "swap" them (which means
14234 // rewriting the mask).
14235 if (A != C)
14236 for (unsigned i = 0; i != N; ++i) {
14237 unsigned Idx = RMask[i];
14238 if (Idx < N)
14239 RMask[i] += N;
14240 else if (Idx < 2*N)
14241 RMask[i] -= N;
14242 }
14243
14244 // At this point LHS and RHS are equivalent to
14245 // LHS = VECTOR_SHUFFLE A, B, LMask
14246 // RHS = VECTOR_SHUFFLE A, B, RMask
14247 // Check that the masks correspond to performing a horizontal operation.
14248 for (unsigned i = 0; i != N; ++i) {
14249 unsigned LIdx = LMask[i], RIdx = RMask[i];
14250
14251 // Ignore any UNDEF components.
14252 if (LIdx >= 2*N || RIdx >= 2*N || (!A.getNode() && (LIdx < N || RIdx < N))
14253 || (!B.getNode() && (LIdx >= N || RIdx >= N)))
14254 continue;
14255
14256 // Check that successive elements are being operated on. If not, this is
14257 // not a horizontal operation.
14258 if (!(LIdx == 2*i && RIdx == 2*i + 1) &&
14259 !(isCommutative && LIdx == 2*i + 1 && RIdx == 2*i))
14260 return false;
14261 }
14262
14263 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
14264 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
14265 return true;
14266}
14267
14268/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
14269static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
14270 const X86Subtarget *Subtarget) {
14271 EVT VT = N->getValueType(0);
14272 SDValue LHS = N->getOperand(0);
14273 SDValue RHS = N->getOperand(1);
14274
14275 // Try to synthesize horizontal adds from adds of shuffles.
14276 if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
14277 (VT == MVT::v4f32 || VT == MVT::v2f64) &&
14278 isHorizontalBinOp(LHS, RHS, true))
14279 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
14280 return SDValue();
14281}
14282
14283/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
14284static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
14285 const X86Subtarget *Subtarget) {
14286 EVT VT = N->getValueType(0);
14287 SDValue LHS = N->getOperand(0);
14288 SDValue RHS = N->getOperand(1);
14289
14290 // Try to synthesize horizontal subs from subs of shuffles.
14291 if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
14292 (VT == MVT::v4f32 || VT == MVT::v2f64) &&
14293 isHorizontalBinOp(LHS, RHS, false))
14294 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
14295 return SDValue();
14296}
14297
Chris Lattner6cf73262008-01-25 06:14:17 +000014298/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
14299/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014300static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000014301 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
14302 // F[X]OR(0.0, x) -> x
14303 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000014304 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14305 if (C->getValueAPF().isPosZero())
14306 return N->getOperand(1);
14307 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14308 if (C->getValueAPF().isPosZero())
14309 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000014310 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014311}
14312
14313/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014314static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000014315 // FAND(0.0, x) -> 0.0
14316 // FAND(x, 0.0) -> 0.0
14317 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14318 if (C->getValueAPF().isPosZero())
14319 return N->getOperand(0);
14320 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14321 if (C->getValueAPF().isPosZero())
14322 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000014323 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014324}
14325
Dan Gohmane5af2d32009-01-29 01:59:02 +000014326static SDValue PerformBTCombine(SDNode *N,
14327 SelectionDAG &DAG,
14328 TargetLowering::DAGCombinerInfo &DCI) {
14329 // BT ignores high bits in the bit index operand.
14330 SDValue Op1 = N->getOperand(1);
14331 if (Op1.hasOneUse()) {
14332 unsigned BitWidth = Op1.getValueSizeInBits();
14333 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
14334 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014335 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
14336 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000014337 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000014338 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
14339 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
14340 DCI.CommitTargetLoweringOpt(TLO);
14341 }
14342 return SDValue();
14343}
Chris Lattner83e6c992006-10-04 06:57:07 +000014344
Eli Friedman7a5e5552009-06-07 06:52:44 +000014345static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
14346 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014347 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000014348 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000014349 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000014350 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000014351 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000014352 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014353 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014354 }
14355 return SDValue();
14356}
14357
Evan Cheng2e489c42009-12-16 00:53:11 +000014358static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
14359 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
14360 // (and (i32 x86isd::setcc_carry), 1)
14361 // This eliminates the zext. This transformation is necessary because
14362 // ISD::SETCC is always legalized to i8.
14363 DebugLoc dl = N->getDebugLoc();
14364 SDValue N0 = N->getOperand(0);
14365 EVT VT = N->getValueType(0);
14366 if (N0.getOpcode() == ISD::AND &&
14367 N0.hasOneUse() &&
14368 N0.getOperand(0).hasOneUse()) {
14369 SDValue N00 = N0.getOperand(0);
14370 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
14371 return SDValue();
14372 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
14373 if (!C || C->getZExtValue() != 1)
14374 return SDValue();
14375 return DAG.getNode(ISD::AND, dl, VT,
14376 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
14377 N00.getOperand(0), N00.getOperand(1)),
14378 DAG.getConstant(1, VT));
14379 }
14380
14381 return SDValue();
14382}
14383
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014384// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
14385static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
14386 unsigned X86CC = N->getConstantOperandVal(0);
14387 SDValue EFLAG = N->getOperand(1);
14388 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014389
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014390 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
14391 // a zext and produces an all-ones bit which is more useful than 0/1 in some
14392 // cases.
14393 if (X86CC == X86::COND_B)
14394 return DAG.getNode(ISD::AND, DL, MVT::i8,
14395 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
14396 DAG.getConstant(X86CC, MVT::i8), EFLAG),
14397 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014398
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014399 return SDValue();
14400}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014401
Benjamin Kramer1396c402011-06-18 11:09:41 +000014402static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
14403 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014404 SDValue Op0 = N->getOperand(0);
14405 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
14406 // a 32-bit target where SSE doesn't support i64->FP operations.
14407 if (Op0.getOpcode() == ISD::LOAD) {
14408 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
14409 EVT VT = Ld->getValueType(0);
14410 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
14411 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
14412 !XTLI->getSubtarget()->is64Bit() &&
14413 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000014414 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
14415 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014416 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
14417 return FILDChain;
14418 }
14419 }
14420 return SDValue();
14421}
14422
Chris Lattner23a01992010-12-20 01:37:09 +000014423// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
14424static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
14425 X86TargetLowering::DAGCombinerInfo &DCI) {
14426 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
14427 // the result is either zero or one (depending on the input carry bit).
14428 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
14429 if (X86::isZeroNode(N->getOperand(0)) &&
14430 X86::isZeroNode(N->getOperand(1)) &&
14431 // We don't have a good way to replace an EFLAGS use, so only do this when
14432 // dead right now.
14433 SDValue(N, 1).use_empty()) {
14434 DebugLoc DL = N->getDebugLoc();
14435 EVT VT = N->getValueType(0);
14436 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
14437 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
14438 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
14439 DAG.getConstant(X86::COND_B,MVT::i8),
14440 N->getOperand(2)),
14441 DAG.getConstant(1, VT));
14442 return DCI.CombineTo(N, Res1, CarryOut);
14443 }
14444
14445 return SDValue();
14446}
14447
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014448// fold (add Y, (sete X, 0)) -> adc 0, Y
14449// (add Y, (setne X, 0)) -> sbb -1, Y
14450// (sub (sete X, 0), Y) -> sbb 0, Y
14451// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014452static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014453 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014454
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014455 // Look through ZExts.
14456 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
14457 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
14458 return SDValue();
14459
14460 SDValue SetCC = Ext.getOperand(0);
14461 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
14462 return SDValue();
14463
14464 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
14465 if (CC != X86::COND_E && CC != X86::COND_NE)
14466 return SDValue();
14467
14468 SDValue Cmp = SetCC.getOperand(1);
14469 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000014470 !X86::isZeroNode(Cmp.getOperand(1)) ||
14471 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014472 return SDValue();
14473
14474 SDValue CmpOp0 = Cmp.getOperand(0);
14475 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
14476 DAG.getConstant(1, CmpOp0.getValueType()));
14477
14478 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
14479 if (CC == X86::COND_NE)
14480 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
14481 DL, OtherVal.getValueType(), OtherVal,
14482 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
14483 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
14484 DL, OtherVal.getValueType(), OtherVal,
14485 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
14486}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014487
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014488static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG) {
14489 SDValue Op0 = N->getOperand(0);
14490 SDValue Op1 = N->getOperand(1);
14491
14492 // X86 can't encode an immediate LHS of a sub. See if we can push the
14493 // negation into a preceding instruction.
14494 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014495 // If the RHS of the sub is a XOR with one use and a constant, invert the
14496 // immediate. Then add one to the LHS of the sub so we can turn
14497 // X-Y -> X+~Y+1, saving one register.
14498 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
14499 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000014500 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014501 EVT VT = Op0.getValueType();
14502 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
14503 Op1.getOperand(0),
14504 DAG.getConstant(~XorC, VT));
14505 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000014506 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014507 }
14508 }
14509
14510 return OptimizeConditionalInDecrement(N, DAG);
14511}
14512
Dan Gohman475871a2008-07-27 21:46:04 +000014513SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000014514 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014515 SelectionDAG &DAG = DCI.DAG;
14516 switch (N->getOpcode()) {
14517 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014518 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014519 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000014520 case ISD::VSELECT:
Chris Lattneraf723b92008-01-25 05:46:26 +000014521 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014522 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014523 case ISD::ADD: return OptimizeConditionalInDecrement(N, DAG);
14524 case ISD::SUB: return PerformSubCombine(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000014525 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000014526 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000014527 case ISD::SHL:
14528 case ISD::SRA:
14529 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000014530 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000014531 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000014532 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014533 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000014534 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014535 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000014536 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
14537 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000014538 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000014539 case X86ISD::FOR: return PerformFORCombine(N, DAG);
14540 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000014541 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014542 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000014543 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014544 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014545 case X86ISD::SHUFPS: // Handle all target specific shuffles
14546 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000014547 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014548 case X86ISD::PUNPCKHBW:
14549 case X86ISD::PUNPCKHWD:
14550 case X86ISD::PUNPCKHDQ:
14551 case X86ISD::PUNPCKHQDQ:
14552 case X86ISD::UNPCKHPS:
14553 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +000014554 case X86ISD::VUNPCKHPSY:
14555 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014556 case X86ISD::PUNPCKLBW:
14557 case X86ISD::PUNPCKLWD:
14558 case X86ISD::PUNPCKLDQ:
14559 case X86ISD::PUNPCKLQDQ:
14560 case X86ISD::UNPCKLPS:
14561 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000014562 case X86ISD::VUNPCKLPSY:
14563 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014564 case X86ISD::MOVHLPS:
14565 case X86ISD::MOVLHPS:
14566 case X86ISD::PSHUFD:
14567 case X86ISD::PSHUFHW:
14568 case X86ISD::PSHUFLW:
14569 case X86ISD::MOVSS:
14570 case X86ISD::MOVSD:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000014571 case X86ISD::VPERMILPS:
14572 case X86ISD::VPERMILPSY:
14573 case X86ISD::VPERMILPD:
14574 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000014575 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014576 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014577 }
14578
Dan Gohman475871a2008-07-27 21:46:04 +000014579 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014580}
14581
Evan Chenge5b51ac2010-04-17 06:13:15 +000014582/// isTypeDesirableForOp - Return true if the target has native support for
14583/// the specified value type and it is 'desirable' to use the type for the
14584/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14585/// instruction encodings are longer and some i16 instructions are slow.
14586bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14587 if (!isTypeLegal(VT))
14588 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014589 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014590 return true;
14591
14592 switch (Opc) {
14593 default:
14594 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014595 case ISD::LOAD:
14596 case ISD::SIGN_EXTEND:
14597 case ISD::ZERO_EXTEND:
14598 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014599 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014600 case ISD::SRL:
14601 case ISD::SUB:
14602 case ISD::ADD:
14603 case ISD::MUL:
14604 case ISD::AND:
14605 case ISD::OR:
14606 case ISD::XOR:
14607 return false;
14608 }
14609}
14610
14611/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014612/// beneficial for dag combiner to promote the specified node. If true, it
14613/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014614bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014615 EVT VT = Op.getValueType();
14616 if (VT != MVT::i16)
14617 return false;
14618
Evan Cheng4c26e932010-04-19 19:29:22 +000014619 bool Promote = false;
14620 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014621 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014622 default: break;
14623 case ISD::LOAD: {
14624 LoadSDNode *LD = cast<LoadSDNode>(Op);
14625 // If the non-extending load has a single use and it's not live out, then it
14626 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014627 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14628 Op.hasOneUse()*/) {
14629 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14630 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14631 // The only case where we'd want to promote LOAD (rather then it being
14632 // promoted as an operand is when it's only use is liveout.
14633 if (UI->getOpcode() != ISD::CopyToReg)
14634 return false;
14635 }
14636 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014637 Promote = true;
14638 break;
14639 }
14640 case ISD::SIGN_EXTEND:
14641 case ISD::ZERO_EXTEND:
14642 case ISD::ANY_EXTEND:
14643 Promote = true;
14644 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014645 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014646 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014647 SDValue N0 = Op.getOperand(0);
14648 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014649 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014650 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014651 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014652 break;
14653 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014654 case ISD::ADD:
14655 case ISD::MUL:
14656 case ISD::AND:
14657 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014658 case ISD::XOR:
14659 Commute = true;
14660 // fallthrough
14661 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014662 SDValue N0 = Op.getOperand(0);
14663 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014664 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014665 return false;
14666 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014667 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014668 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014669 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014670 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014671 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014672 }
14673 }
14674
14675 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014676 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014677}
14678
Evan Cheng60c07e12006-07-05 22:17:51 +000014679//===----------------------------------------------------------------------===//
14680// X86 Inline Assembly Support
14681//===----------------------------------------------------------------------===//
14682
Chris Lattnerb8105652009-07-20 17:51:36 +000014683bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14684 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014685
14686 std::string AsmStr = IA->getAsmString();
14687
14688 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014689 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014690 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014691
14692 switch (AsmPieces.size()) {
14693 default: return false;
14694 case 1:
14695 AsmStr = AsmPieces[0];
14696 AsmPieces.clear();
14697 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
14698
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014699 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000014700 // we will turn this bswap into something that will be lowered to logical ops
14701 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14702 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014703 // bswap $0
14704 if (AsmPieces.size() == 2 &&
14705 (AsmPieces[0] == "bswap" ||
14706 AsmPieces[0] == "bswapq" ||
14707 AsmPieces[0] == "bswapl") &&
14708 (AsmPieces[1] == "$0" ||
14709 AsmPieces[1] == "${0:q}")) {
14710 // No need to check constraints, nothing other than the equivalent of
14711 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014712 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014713 if (!Ty || Ty->getBitWidth() % 16 != 0)
14714 return false;
14715 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014716 }
14717 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014718 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014719 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014720 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014721 AsmPieces[1] == "$$8," &&
14722 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014723 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14724 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014725 const std::string &ConstraintsStr = IA->getConstraintString();
14726 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014727 std::sort(AsmPieces.begin(), AsmPieces.end());
14728 if (AsmPieces.size() == 4 &&
14729 AsmPieces[0] == "~{cc}" &&
14730 AsmPieces[1] == "~{dirflag}" &&
14731 AsmPieces[2] == "~{flags}" &&
14732 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014733 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014734 if (!Ty || Ty->getBitWidth() % 16 != 0)
14735 return false;
14736 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014737 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014738 }
14739 break;
14740 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014741 if (CI->getType()->isIntegerTy(32) &&
14742 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14743 SmallVector<StringRef, 4> Words;
14744 SplitString(AsmPieces[0], Words, " \t,");
14745 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14746 Words[2] == "${0:w}") {
14747 Words.clear();
14748 SplitString(AsmPieces[1], Words, " \t,");
14749 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14750 Words[2] == "$0") {
14751 Words.clear();
14752 SplitString(AsmPieces[2], Words, " \t,");
14753 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14754 Words[2] == "${0:w}") {
14755 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014756 const std::string &ConstraintsStr = IA->getConstraintString();
14757 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014758 std::sort(AsmPieces.begin(), AsmPieces.end());
14759 if (AsmPieces.size() == 4 &&
14760 AsmPieces[0] == "~{cc}" &&
14761 AsmPieces[1] == "~{dirflag}" &&
14762 AsmPieces[2] == "~{flags}" &&
14763 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014764 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014765 if (!Ty || Ty->getBitWidth() % 16 != 0)
14766 return false;
14767 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014768 }
14769 }
14770 }
14771 }
14772 }
Evan Cheng55d42002011-01-08 01:24:27 +000014773
14774 if (CI->getType()->isIntegerTy(64)) {
14775 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14776 if (Constraints.size() >= 2 &&
14777 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14778 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14779 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14780 SmallVector<StringRef, 4> Words;
14781 SplitString(AsmPieces[0], Words, " \t");
14782 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014783 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014784 SplitString(AsmPieces[1], Words, " \t");
14785 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14786 Words.clear();
14787 SplitString(AsmPieces[2], Words, " \t,");
14788 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14789 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014790 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014791 if (!Ty || Ty->getBitWidth() % 16 != 0)
14792 return false;
14793 return IntrinsicLowering::LowerToByteSwap(CI);
14794 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014795 }
14796 }
14797 }
14798 }
14799 break;
14800 }
14801 return false;
14802}
14803
14804
14805
Chris Lattnerf4dff842006-07-11 02:54:03 +000014806/// getConstraintType - Given a constraint letter, return the type of
14807/// constraint it is for this target.
14808X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014809X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14810 if (Constraint.size() == 1) {
14811 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014812 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014813 case 'q':
14814 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014815 case 'f':
14816 case 't':
14817 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014818 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014819 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014820 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014821 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014822 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014823 case 'a':
14824 case 'b':
14825 case 'c':
14826 case 'd':
14827 case 'S':
14828 case 'D':
14829 case 'A':
14830 return C_Register;
14831 case 'I':
14832 case 'J':
14833 case 'K':
14834 case 'L':
14835 case 'M':
14836 case 'N':
14837 case 'G':
14838 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014839 case 'e':
14840 case 'Z':
14841 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014842 default:
14843 break;
14844 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014845 }
Chris Lattner4234f572007-03-25 02:14:49 +000014846 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014847}
14848
John Thompson44ab89e2010-10-29 17:29:13 +000014849/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014850/// This object must already have been set up with the operand type
14851/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014852TargetLowering::ConstraintWeight
14853 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014854 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014855 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014856 Value *CallOperandVal = info.CallOperandVal;
14857 // If we don't have a value, we can't do a match,
14858 // but allow it at the lowest weight.
14859 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014860 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014861 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014862 // Look at the constraint type.
14863 switch (*constraint) {
14864 default:
John Thompson44ab89e2010-10-29 17:29:13 +000014865 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
14866 case 'R':
14867 case 'q':
14868 case 'Q':
14869 case 'a':
14870 case 'b':
14871 case 'c':
14872 case 'd':
14873 case 'S':
14874 case 'D':
14875 case 'A':
14876 if (CallOperandVal->getType()->isIntegerTy())
14877 weight = CW_SpecificReg;
14878 break;
14879 case 'f':
14880 case 't':
14881 case 'u':
14882 if (type->isFloatingPointTy())
14883 weight = CW_SpecificReg;
14884 break;
14885 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000014886 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000014887 weight = CW_SpecificReg;
14888 break;
14889 case 'x':
14890 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014891 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000014892 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014893 break;
14894 case 'I':
14895 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
14896 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000014897 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014898 }
14899 break;
John Thompson44ab89e2010-10-29 17:29:13 +000014900 case 'J':
14901 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14902 if (C->getZExtValue() <= 63)
14903 weight = CW_Constant;
14904 }
14905 break;
14906 case 'K':
14907 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14908 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
14909 weight = CW_Constant;
14910 }
14911 break;
14912 case 'L':
14913 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14914 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
14915 weight = CW_Constant;
14916 }
14917 break;
14918 case 'M':
14919 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14920 if (C->getZExtValue() <= 3)
14921 weight = CW_Constant;
14922 }
14923 break;
14924 case 'N':
14925 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14926 if (C->getZExtValue() <= 0xff)
14927 weight = CW_Constant;
14928 }
14929 break;
14930 case 'G':
14931 case 'C':
14932 if (dyn_cast<ConstantFP>(CallOperandVal)) {
14933 weight = CW_Constant;
14934 }
14935 break;
14936 case 'e':
14937 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14938 if ((C->getSExtValue() >= -0x80000000LL) &&
14939 (C->getSExtValue() <= 0x7fffffffLL))
14940 weight = CW_Constant;
14941 }
14942 break;
14943 case 'Z':
14944 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14945 if (C->getZExtValue() <= 0xffffffff)
14946 weight = CW_Constant;
14947 }
14948 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014949 }
14950 return weight;
14951}
14952
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014953/// LowerXConstraint - try to replace an X constraint, which matches anything,
14954/// with another that has more specific requirements based on the type of the
14955/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000014956const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000014957LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000014958 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
14959 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000014960 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014961 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000014962 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014963 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000014964 return "x";
14965 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014966
Chris Lattner5e764232008-04-26 23:02:14 +000014967 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014968}
14969
Chris Lattner48884cd2007-08-25 00:47:38 +000014970/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
14971/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000014972void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000014973 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000014974 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000014975 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000014976 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000014977
Eric Christopher100c8332011-06-02 23:16:42 +000014978 // Only support length 1 constraints for now.
14979 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000014980
Eric Christopher100c8332011-06-02 23:16:42 +000014981 char ConstraintLetter = Constraint[0];
14982 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014983 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000014984 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000014985 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014986 if (C->getZExtValue() <= 31) {
14987 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014988 break;
14989 }
Devang Patel84f7fd22007-03-17 00:13:28 +000014990 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014991 return;
Evan Cheng364091e2008-09-22 23:57:37 +000014992 case 'J':
14993 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014994 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000014995 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14996 break;
14997 }
14998 }
14999 return;
15000 case 'K':
15001 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015002 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000015003 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15004 break;
15005 }
15006 }
15007 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000015008 case 'N':
15009 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015010 if (C->getZExtValue() <= 255) {
15011 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015012 break;
15013 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000015014 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015015 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000015016 case 'e': {
15017 // 32-bit signed value
15018 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015019 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15020 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015021 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015022 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000015023 break;
15024 }
15025 // FIXME gcc accepts some relocatable values here too, but only in certain
15026 // memory models; it's complicated.
15027 }
15028 return;
15029 }
15030 case 'Z': {
15031 // 32-bit unsigned value
15032 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015033 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15034 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015035 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15036 break;
15037 }
15038 }
15039 // FIXME gcc accepts some relocatable values here too, but only in certain
15040 // memory models; it's complicated.
15041 return;
15042 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015043 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015044 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000015045 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015046 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015047 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000015048 break;
15049 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015050
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015051 // In any sort of PIC mode addresses need to be computed at runtime by
15052 // adding in a register or some sort of table lookup. These can't
15053 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000015054 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015055 return;
15056
Chris Lattnerdc43a882007-05-03 16:52:29 +000015057 // If we are in non-pic codegen mode, we allow the address of a global (with
15058 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000015059 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015060 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000015061
Chris Lattner49921962009-05-08 18:23:14 +000015062 // Match either (GA), (GA+C), (GA+C1+C2), etc.
15063 while (1) {
15064 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
15065 Offset += GA->getOffset();
15066 break;
15067 } else if (Op.getOpcode() == ISD::ADD) {
15068 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15069 Offset += C->getZExtValue();
15070 Op = Op.getOperand(0);
15071 continue;
15072 }
15073 } else if (Op.getOpcode() == ISD::SUB) {
15074 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15075 Offset += -C->getZExtValue();
15076 Op = Op.getOperand(0);
15077 continue;
15078 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015079 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015080
Chris Lattner49921962009-05-08 18:23:14 +000015081 // Otherwise, this isn't something we can handle, reject it.
15082 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015083 }
Eric Christopherfd179292009-08-27 18:07:15 +000015084
Dan Gohman46510a72010-04-15 01:51:59 +000015085 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015086 // If we require an extra load to get this address, as in PIC mode, we
15087 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000015088 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
15089 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015090 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000015091
Devang Patel0d881da2010-07-06 22:08:15 +000015092 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
15093 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000015094 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015095 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015096 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015097
Gabor Greifba36cb52008-08-28 21:40:38 +000015098 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000015099 Ops.push_back(Result);
15100 return;
15101 }
Dale Johannesen1784d162010-06-25 21:55:36 +000015102 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015103}
15104
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015105std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000015106X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000015107 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000015108 // First, see if this is a constraint that directly corresponds to an LLVM
15109 // register class.
15110 if (Constraint.size() == 1) {
15111 // GCC Constraint Letters
15112 switch (Constraint[0]) {
15113 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000015114 // TODO: Slight differences here in allocation order and leaving
15115 // RIP in the class. Do they matter any more here than they do
15116 // in the normal allocation?
15117 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
15118 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015119 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015120 return std::make_pair(0U, X86::GR32RegisterClass);
15121 else if (VT == MVT::i16)
15122 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015123 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015124 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015125 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000015126 return std::make_pair(0U, X86::GR64RegisterClass);
15127 break;
15128 }
15129 // 32-bit fallthrough
15130 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015131 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015132 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
15133 else if (VT == MVT::i16)
15134 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015135 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015136 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
15137 else if (VT == MVT::i64)
15138 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
15139 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015140 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000015141 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015142 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000015143 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015144 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000015145 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000015146 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000015147 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000015148 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015149 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015150 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015151 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
15152 if (VT == MVT::i16)
15153 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
15154 if (VT == MVT::i32 || !Subtarget->is64Bit())
15155 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
15156 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015157 case 'f': // FP Stack registers.
15158 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
15159 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000015160 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015161 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015162 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015163 return std::make_pair(0U, X86::RFP64RegisterClass);
15164 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000015165 case 'y': // MMX_REGS if MMX allowed.
15166 if (!Subtarget->hasMMX()) break;
15167 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015168 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015169 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015170 // FALL THROUGH.
15171 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015172 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000015173
Owen Anderson825b72b2009-08-11 20:47:22 +000015174 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000015175 default: break;
15176 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015177 case MVT::f32:
15178 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000015179 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015180 case MVT::f64:
15181 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000015182 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015183 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015184 case MVT::v16i8:
15185 case MVT::v8i16:
15186 case MVT::v4i32:
15187 case MVT::v2i64:
15188 case MVT::v4f32:
15189 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000015190 return std::make_pair(0U, X86::VR128RegisterClass);
15191 }
Chris Lattnerad043e82007-04-09 05:11:28 +000015192 break;
15193 }
15194 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015195
Chris Lattnerf76d1802006-07-31 23:26:50 +000015196 // Use the default implementation in TargetLowering to convert the register
15197 // constraint into a member of a register class.
15198 std::pair<unsigned, const TargetRegisterClass*> Res;
15199 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000015200
15201 // Not found as a standard register?
15202 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015203 // Map st(0) -> st(7) -> ST0
15204 if (Constraint.size() == 7 && Constraint[0] == '{' &&
15205 tolower(Constraint[1]) == 's' &&
15206 tolower(Constraint[2]) == 't' &&
15207 Constraint[3] == '(' &&
15208 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
15209 Constraint[5] == ')' &&
15210 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000015211
Chris Lattner56d77c72009-09-13 22:41:48 +000015212 Res.first = X86::ST0+Constraint[4]-'0';
15213 Res.second = X86::RFP80RegisterClass;
15214 return Res;
15215 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015216
Chris Lattner56d77c72009-09-13 22:41:48 +000015217 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015218 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000015219 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000015220 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015221 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000015222 }
Chris Lattner56d77c72009-09-13 22:41:48 +000015223
15224 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015225 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015226 Res.first = X86::EFLAGS;
15227 Res.second = X86::CCRRegisterClass;
15228 return Res;
15229 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015230
Dale Johannesen330169f2008-11-13 21:52:36 +000015231 // 'A' means EAX + EDX.
15232 if (Constraint == "A") {
15233 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000015234 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015235 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000015236 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000015237 return Res;
15238 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015239
Chris Lattnerf76d1802006-07-31 23:26:50 +000015240 // Otherwise, check to see if this is a register class of the wrong value
15241 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
15242 // turn into {ax},{dx}.
15243 if (Res.second->hasType(VT))
15244 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015245
Chris Lattnerf76d1802006-07-31 23:26:50 +000015246 // All of the single-register GCC register classes map their values onto
15247 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
15248 // really want an 8-bit or 32-bit register, map to the appropriate register
15249 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000015250 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015251 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015252 unsigned DestReg = 0;
15253 switch (Res.first) {
15254 default: break;
15255 case X86::AX: DestReg = X86::AL; break;
15256 case X86::DX: DestReg = X86::DL; break;
15257 case X86::CX: DestReg = X86::CL; break;
15258 case X86::BX: DestReg = X86::BL; break;
15259 }
15260 if (DestReg) {
15261 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015262 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015263 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015264 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015265 unsigned DestReg = 0;
15266 switch (Res.first) {
15267 default: break;
15268 case X86::AX: DestReg = X86::EAX; break;
15269 case X86::DX: DestReg = X86::EDX; break;
15270 case X86::CX: DestReg = X86::ECX; break;
15271 case X86::BX: DestReg = X86::EBX; break;
15272 case X86::SI: DestReg = X86::ESI; break;
15273 case X86::DI: DestReg = X86::EDI; break;
15274 case X86::BP: DestReg = X86::EBP; break;
15275 case X86::SP: DestReg = X86::ESP; break;
15276 }
15277 if (DestReg) {
15278 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015279 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015280 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015281 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015282 unsigned DestReg = 0;
15283 switch (Res.first) {
15284 default: break;
15285 case X86::AX: DestReg = X86::RAX; break;
15286 case X86::DX: DestReg = X86::RDX; break;
15287 case X86::CX: DestReg = X86::RCX; break;
15288 case X86::BX: DestReg = X86::RBX; break;
15289 case X86::SI: DestReg = X86::RSI; break;
15290 case X86::DI: DestReg = X86::RDI; break;
15291 case X86::BP: DestReg = X86::RBP; break;
15292 case X86::SP: DestReg = X86::RSP; break;
15293 }
15294 if (DestReg) {
15295 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015296 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015297 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000015298 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000015299 } else if (Res.second == X86::FR32RegisterClass ||
15300 Res.second == X86::FR64RegisterClass ||
15301 Res.second == X86::VR128RegisterClass) {
15302 // Handle references to XMM physical registers that got mapped into the
15303 // wrong class. This can happen with constraints like {xmm0} where the
15304 // target independent register mapper will just pick the first match it can
15305 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000015306 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015307 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000015308 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015309 Res.second = X86::FR64RegisterClass;
15310 else if (X86::VR128RegisterClass->hasType(VT))
15311 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000015312 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015313
Chris Lattnerf76d1802006-07-31 23:26:50 +000015314 return Res;
15315}