blob: 5bd3e2b6711bb06178fb45fc2019f546f21dfd5a [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000041#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000044#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000045#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000048#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000050#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000053#include "llvm/Support/raw_ostream.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000054#include "llvm/Target/TargetOptions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000055using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000056using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000057
Evan Chengb1712452010-01-27 06:25:16 +000058STATISTIC(NumTailCalls, "Number of tail calls");
59
Evan Cheng10e86422008-04-25 19:11:04 +000060// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000061static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000062 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000063
David Greenea5f26012011-02-07 19:36:54 +000064static SDValue Insert128BitVector(SDValue Result,
65 SDValue Vec,
66 SDValue Idx,
67 SelectionDAG &DAG,
68 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000069
David Greenea5f26012011-02-07 19:36:54 +000070static SDValue Extract128BitVector(SDValue Vec,
71 SDValue Idx,
72 SelectionDAG &DAG,
73 DebugLoc dl);
74
75/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
76/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000077/// simple subregister reference. Idx is an index in the 128 bits we
78/// want. It need not be aligned to a 128-bit bounday. That makes
79/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000080static SDValue Extract128BitVector(SDValue Vec,
81 SDValue Idx,
82 SelectionDAG &DAG,
83 DebugLoc dl) {
84 EVT VT = Vec.getValueType();
85 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000086 EVT ElVT = VT.getVectorElementType();
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000087 int Factor = VT.getSizeInBits()/128;
88 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
89 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000090
91 // Extract from UNDEF is UNDEF.
92 if (Vec.getOpcode() == ISD::UNDEF)
93 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
94
95 if (isa<ConstantSDNode>(Idx)) {
96 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
97
98 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
99 // we can match to VEXTRACTF128.
100 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
101
102 // This is the index of the first element of the 128-bit chunk
103 // we want.
104 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
105 * ElemsPerChunk);
106
107 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000108 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
109 VecIdx);
110
111 return Result;
112 }
113
114 return SDValue();
115}
116
117/// Generate a DAG to put 128-bits into a vector > 128 bits. This
118/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000119/// simple superregister reference. Idx is an index in the 128 bits
120/// we want. It need not be aligned to a 128-bit bounday. That makes
121/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000122static SDValue Insert128BitVector(SDValue Result,
123 SDValue Vec,
124 SDValue Idx,
125 SelectionDAG &DAG,
126 DebugLoc dl) {
127 if (isa<ConstantSDNode>(Idx)) {
128 EVT VT = Vec.getValueType();
129 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
130
131 EVT ElVT = VT.getVectorElementType();
David Greenea5f26012011-02-07 19:36:54 +0000132 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
David Greenea5f26012011-02-07 19:36:54 +0000133 EVT ResultVT = Result.getValueType();
134
135 // Insert the relevant 128 bits.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000136 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000137
138 // This is the index of the first element of the 128-bit chunk
139 // we want.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000140 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
David Greenea5f26012011-02-07 19:36:54 +0000141 * ElemsPerChunk);
142
143 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000144 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
145 VecIdx);
146 return Result;
147 }
148
149 return SDValue();
150}
151
Chris Lattnerf0144122009-07-28 03:13:23 +0000152static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000153 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
154 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000155
Evan Cheng2bffee22011-02-01 01:14:13 +0000156 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000157 if (is64Bit)
158 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000159 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000160 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000161
Evan Cheng203576a2011-07-20 19:50:42 +0000162 if (Subtarget->isTargetELF())
163 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000164 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000165 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000166 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000167}
168
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000169X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000170 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000171 Subtarget = &TM.getSubtarget<X86Subtarget>();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000172 X86ScalarSSEf64 = Subtarget->hasXMMInt();
173 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000174 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000175
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000176 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000177 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000178
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000179 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000180 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000181
182 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000183 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000184 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
185 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000186
Eric Christopherde5e1012011-03-11 01:05:58 +0000187 // For 64-bit since we have so many registers use the ILP scheduler, for
188 // 32-bit code use the register pressure specific scheduling.
189 if (Subtarget->is64Bit())
190 setSchedulingPreference(Sched::ILP);
191 else
192 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000193 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000194
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000195 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000196 // Setup Windows compiler runtime calls.
197 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000198 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000199 setLibcallName(RTLIB::SREM_I64, "_allrem");
200 setLibcallName(RTLIB::UREM_I64, "_aullrem");
201 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000202 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000203 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000204 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000205 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000206 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
207 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
208 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000209 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
210 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000211 }
212
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000213 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000214 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000215 setUseUnderscoreSetJmp(false);
216 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000217 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000218 // MS runtime is weird: it exports _setjmp, but longjmp!
219 setUseUnderscoreSetJmp(true);
220 setUseUnderscoreLongJmp(false);
221 } else {
222 setUseUnderscoreSetJmp(true);
223 setUseUnderscoreLongJmp(true);
224 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000225
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000226 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000228 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000230 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000232
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000234
Scott Michelfdc40a02009-02-17 22:15:04 +0000235 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000237 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000239 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
241 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000242
243 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
245 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
246 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
247 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
248 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
249 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000250
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000251 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
252 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000253 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
254 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
255 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000256
Evan Cheng25ab6902006-09-08 06:48:29 +0000257 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
259 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000260 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000261 // We have an algorithm for SSE2->double, and we turn this into a
262 // 64-bit FILD followed by conditional FADD for other targets.
263 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000264 // We have an algorithm for SSE2, and we turn this into a 64-bit
265 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000266 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000267 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000268
269 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
270 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
272 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000273
Devang Patel6a784892009-06-05 18:48:29 +0000274 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000275 // SSE has no i16 to fp conversion, only i32
276 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000278 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000280 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
282 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000283 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000284 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
286 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000287 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000288
Dale Johannesen73328d12007-09-19 23:55:34 +0000289 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
290 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
292 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000293
Evan Cheng02568ff2006-01-30 22:13:22 +0000294 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
295 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
297 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000298
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000299 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000301 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000303 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
305 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000306 }
307
308 // Handle FP_TO_UINT by promoting the destination to a larger signed
309 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
311 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
312 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000313
Evan Cheng25ab6902006-09-08 06:48:29 +0000314 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
316 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000317 } else if (!UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000318 // Since AVX is a superset of SSE3, only check for SSE here.
319 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 // Expand FP_TO_UINT into a select.
321 // FIXME: We would like to use a Custom expander here eventually to do
322 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000325 // With SSE3 we can use fisttpll to convert to a signed i64; without
326 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000328 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000329
Chris Lattner399610a2006-12-05 18:22:22 +0000330 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000331 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000332 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
333 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000334 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000335 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000336 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000337 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000338 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000339 }
Chris Lattner21f66852005-12-23 05:15:23 +0000340
Dan Gohmanb00ee212008-02-18 19:34:53 +0000341 // Scalar integer divide and remainder are lowered to use operations that
342 // produce two results, to match the available instructions. This exposes
343 // the two-result form to trivial CSE, which is able to combine x/y and x%y
344 // into a single instruction.
345 //
346 // Scalar integer multiply-high is also lowered to use two-result
347 // operations, to match the available instructions. However, plain multiply
348 // (low) operations are left as Legal, as there are single-result
349 // instructions for this in x86. Using the two-result multiply instructions
350 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000351 for (unsigned i = 0, e = 4; i != e; ++i) {
352 MVT VT = IntVTs[i];
353 setOperationAction(ISD::MULHS, VT, Expand);
354 setOperationAction(ISD::MULHU, VT, Expand);
355 setOperationAction(ISD::SDIV, VT, Expand);
356 setOperationAction(ISD::UDIV, VT, Expand);
357 setOperationAction(ISD::SREM, VT, Expand);
358 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000359
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000360 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000361 setOperationAction(ISD::ADDC, VT, Custom);
362 setOperationAction(ISD::ADDE, VT, Custom);
363 setOperationAction(ISD::SUBC, VT, Custom);
364 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000365 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000366
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
368 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
369 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
370 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000371 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
375 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
376 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
377 setOperationAction(ISD::FREM , MVT::f32 , Expand);
378 setOperationAction(ISD::FREM , MVT::f64 , Expand);
379 setOperationAction(ISD::FREM , MVT::f80 , Expand);
380 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000381
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
383 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000384 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
385 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
387 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000388 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000389 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
390 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000391 }
392
Benjamin Kramer1292c222010-12-04 20:32:23 +0000393 if (Subtarget->hasPOPCNT()) {
394 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
395 } else {
396 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
397 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
398 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
399 if (Subtarget->is64Bit())
400 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
401 }
402
Owen Anderson825b72b2009-08-11 20:47:22 +0000403 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
404 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000405
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000406 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000407 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000408 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000409 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000410 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
412 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
413 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
414 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
415 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000416 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000417 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
418 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
419 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
420 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000421 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000422 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000423 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000424 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000426
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000427 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000428 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
429 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
430 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
431 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000432 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
434 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000435 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000436 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
438 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
439 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
440 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000441 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000442 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000443 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
445 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
446 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000447 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000448 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
449 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
450 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000451 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000452
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000453 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000454 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000455
Eric Christopher9a9d2752010-07-22 02:48:34 +0000456 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000457 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000458
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000459 // On X86 and X86-64, atomic operations are lowered to locked instructions.
460 // Locked instructions, in turn, have implicit fence semantics (all memory
461 // operations are flushed before issuing the locked instruction, and they
462 // are not buffered), so we can fold away the common pattern of
463 // fence-atomic-fence.
464 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000465
Mon P Wang63307c32008-05-05 19:05:59 +0000466 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000467 for (unsigned i = 0, e = 4; i != e; ++i) {
468 MVT VT = IntVTs[i];
469 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
470 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000471 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000472 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000473
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000474 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000475 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000476 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
477 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
478 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
479 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
480 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
481 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
482 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000483 }
484
Eli Friedman43f51ae2011-08-26 21:21:21 +0000485 if (Subtarget->hasCmpxchg16b()) {
486 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
487 }
488
Evan Cheng3c992d22006-03-07 02:02:57 +0000489 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000490 if (!Subtarget->isTargetDarwin() &&
491 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000492 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000493 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000494 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000495
Owen Anderson825b72b2009-08-11 20:47:22 +0000496 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
497 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
498 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
499 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000500 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000501 setExceptionPointerRegister(X86::RAX);
502 setExceptionSelectorRegister(X86::RDX);
503 } else {
504 setExceptionPointerRegister(X86::EAX);
505 setExceptionSelectorRegister(X86::EDX);
506 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000507 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
508 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000509
Duncan Sands4a544a72011-09-06 13:37:06 +0000510 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
511 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000512
Owen Anderson825b72b2009-08-11 20:47:22 +0000513 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000514
Nate Begemanacc398c2006-01-25 18:21:52 +0000515 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 setOperationAction(ISD::VASTART , MVT::Other, Custom);
517 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000518 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 setOperationAction(ISD::VAARG , MVT::Other, Custom);
520 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000521 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 setOperationAction(ISD::VAARG , MVT::Other, Expand);
523 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000524 }
Evan Chengae642192007-03-02 23:16:35 +0000525
Owen Anderson825b72b2009-08-11 20:47:22 +0000526 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
527 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000528
529 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
530 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
531 MVT::i64 : MVT::i32, Custom);
532 else if (EnableSegmentedStacks)
533 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
534 MVT::i64 : MVT::i32, Custom);
535 else
536 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
537 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000538
Evan Chengc7ce29b2009-02-13 22:36:38 +0000539 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000540 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000541 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000542 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
543 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000544
Evan Cheng223547a2006-01-31 22:28:30 +0000545 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 setOperationAction(ISD::FABS , MVT::f64, Custom);
547 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000548
549 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 setOperationAction(ISD::FNEG , MVT::f64, Custom);
551 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000552
Evan Cheng68c47cb2007-01-05 07:55:56 +0000553 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
555 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000556
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000557 // Lower this to FGETSIGNx86 plus an AND.
558 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
559 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
560
Evan Chengd25e9e82006-02-02 00:28:23 +0000561 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::FSIN , MVT::f64, Expand);
563 setOperationAction(ISD::FCOS , MVT::f64, Expand);
564 setOperationAction(ISD::FSIN , MVT::f32, Expand);
565 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000566
Chris Lattnera54aa942006-01-29 06:26:08 +0000567 // Expand FP immediates into loads from the stack, except for the special
568 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000569 addLegalFPImmediate(APFloat(+0.0)); // xorpd
570 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000571 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000572 // Use SSE for f32, x87 for f64.
573 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000574 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
575 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000576
577 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000578 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000579
580 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000581 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000582
Owen Anderson825b72b2009-08-11 20:47:22 +0000583 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000584
585 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
587 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000588
589 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000590 setOperationAction(ISD::FSIN , MVT::f32, Expand);
591 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592
Nate Begemane1795842008-02-14 08:57:00 +0000593 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000594 addLegalFPImmediate(APFloat(+0.0f)); // xorps
595 addLegalFPImmediate(APFloat(+0.0)); // FLD0
596 addLegalFPImmediate(APFloat(+1.0)); // FLD1
597 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
598 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
599
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000600 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
602 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000603 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000604 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000605 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000606 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000607 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
608 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000609
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
611 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
612 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
613 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000614
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000615 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
617 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000618 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000619 addLegalFPImmediate(APFloat(+0.0)); // FLD0
620 addLegalFPImmediate(APFloat(+1.0)); // FLD1
621 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
622 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000623 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
624 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
625 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
626 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000627 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000628
Cameron Zwarich33390842011-07-08 21:39:21 +0000629 // We don't support FMA.
630 setOperationAction(ISD::FMA, MVT::f64, Expand);
631 setOperationAction(ISD::FMA, MVT::f32, Expand);
632
Dale Johannesen59a58732007-08-05 18:49:15 +0000633 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000634 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
636 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
637 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000638 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000639 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000640 addLegalFPImmediate(TmpFlt); // FLD0
641 TmpFlt.changeSign();
642 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000643
644 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000645 APFloat TmpFlt2(+1.0);
646 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
647 &ignored);
648 addLegalFPImmediate(TmpFlt2); // FLD1
649 TmpFlt2.changeSign();
650 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
651 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000652
Evan Chengc7ce29b2009-02-13 22:36:38 +0000653 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
655 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000656 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000657
658 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000659 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000660
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000661 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
663 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
664 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000665
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::FLOG, MVT::f80, Expand);
667 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
668 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
669 setOperationAction(ISD::FEXP, MVT::f80, Expand);
670 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000671
Mon P Wangf007a8b2008-11-06 05:31:54 +0000672 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000673 // (for widening) or expand (for scalarization). Then we will selectively
674 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
676 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
677 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
678 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
679 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
680 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
681 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
682 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
683 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
684 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
685 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
686 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000692 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000693 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
694 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000695 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000716 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000717 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000726 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000727 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000731 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000732 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
733 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
734 setTruncStoreAction((MVT::SimpleValueType)VT,
735 (MVT::SimpleValueType)InnerVT, Expand);
736 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
737 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
738 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000739 }
740
Evan Chengc7ce29b2009-02-13 22:36:38 +0000741 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
742 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000743 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000744 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000745 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000746 }
747
Dale Johannesen0488fb62010-09-30 23:57:10 +0000748 // MMX-sized vectors (other than x86mmx) are expected to be expanded
749 // into smaller operations.
750 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
751 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
752 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
753 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
754 setOperationAction(ISD::AND, MVT::v8i8, Expand);
755 setOperationAction(ISD::AND, MVT::v4i16, Expand);
756 setOperationAction(ISD::AND, MVT::v2i32, Expand);
757 setOperationAction(ISD::AND, MVT::v1i64, Expand);
758 setOperationAction(ISD::OR, MVT::v8i8, Expand);
759 setOperationAction(ISD::OR, MVT::v4i16, Expand);
760 setOperationAction(ISD::OR, MVT::v2i32, Expand);
761 setOperationAction(ISD::OR, MVT::v1i64, Expand);
762 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
763 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
764 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
765 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
766 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
767 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
768 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
769 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
770 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
771 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
772 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
773 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
774 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000775 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
776 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
777 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
778 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000779
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000780 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000781 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000782
Owen Anderson825b72b2009-08-11 20:47:22 +0000783 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
784 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
785 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
786 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
787 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
788 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
789 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
790 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
791 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
792 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
793 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000794 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000795 }
796
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000797 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000798 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000799
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000800 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
801 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000802 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
803 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
804 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
805 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000806
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
808 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
809 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
810 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
811 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
812 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
813 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
814 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
815 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
816 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
817 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
818 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
819 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
820 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
821 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
822 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000823
Nadav Rotem354efd82011-09-18 14:57:03 +0000824 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000825 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
826 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
827 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000828
Owen Anderson825b72b2009-08-11 20:47:22 +0000829 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
830 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
831 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
832 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
833 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000834
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000835 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
836 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
837 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
838 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
839 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
840
Evan Cheng2c3ae372006-04-12 21:21:57 +0000841 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
843 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000844 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000845 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000846 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000847 // Do not attempt to custom lower non-128-bit vectors
848 if (!VT.is128BitVector())
849 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000850 setOperationAction(ISD::BUILD_VECTOR,
851 VT.getSimpleVT().SimpleTy, Custom);
852 setOperationAction(ISD::VECTOR_SHUFFLE,
853 VT.getSimpleVT().SimpleTy, Custom);
854 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
855 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000856 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000857
Owen Anderson825b72b2009-08-11 20:47:22 +0000858 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
859 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
860 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
861 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
862 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
863 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000864
Nate Begemancdd1eec2008-02-12 22:51:28 +0000865 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000866 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
867 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000868 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000869
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000870 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
872 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000873 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000874
875 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000876 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000877 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000878
Owen Andersond6662ad2009-08-10 20:46:15 +0000879 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000881 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000882 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000883 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000885 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000886 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000887 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000889 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000890
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000892
Evan Cheng2c3ae372006-04-12 21:21:57 +0000893 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000894 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
895 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
896 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
897 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000898
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
900 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000901 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000902
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000903 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000904 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
905 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
906 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
907 setOperationAction(ISD::FRINT, MVT::f32, Legal);
908 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
909 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
910 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
911 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
912 setOperationAction(ISD::FRINT, MVT::f64, Legal);
913 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
914
Nate Begeman14d12ca2008-02-11 04:19:36 +0000915 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000916 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000917
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000918 // Can turn SHL into an integer multiply.
919 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000920 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000921
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000922 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
923 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
924 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
925 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
926 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000927
Nate Begeman14d12ca2008-02-11 04:19:36 +0000928 // i8 and i16 vectors are custom , because the source register and source
929 // source memory operand types are not the same width. f32 vectors are
930 // custom since the immediate controlling the insert encodes additional
931 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000932 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
933 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
934 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
935 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000936
Owen Anderson825b72b2009-08-11 20:47:22 +0000937 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
938 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
939 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
940 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000941
942 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000943 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
944 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000945 }
946 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000947
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000948 if (Subtarget->hasXMMInt()) {
Nadav Rotem43012222011-05-11 08:12:09 +0000949 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
950 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
951 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000952 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000953
954 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
955 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
956 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
957
958 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
959 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
960 }
961
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000962 if (Subtarget->hasSSE42() || Subtarget->hasAVX())
Duncan Sands28b77e92011-09-06 19:07:46 +0000963 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000964
David Greene9b9838d2009-06-29 16:47:10 +0000965 if (!UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000966 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
967 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
968 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
969 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
970 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
971 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000972
Owen Anderson825b72b2009-08-11 20:47:22 +0000973 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000974 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
975 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000976
Owen Anderson825b72b2009-08-11 20:47:22 +0000977 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
978 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
979 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
980 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
981 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
982 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000983
Owen Anderson825b72b2009-08-11 20:47:22 +0000984 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
985 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
986 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
987 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
988 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
989 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000990
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000991 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
992 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +0000993 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +0000994
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +0000995 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
996 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
997 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
998 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
999 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
1000 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
1001
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001002 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1003 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1004 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1005 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1006
1007 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1008 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1009 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1010 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1011
1012 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
1013 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
1014
Duncan Sands28b77e92011-09-06 19:07:46 +00001015 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1016 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1017 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1018 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001019
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001020 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1021 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1022 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1023
Nadav Rotemfbad25e2011-09-11 15:02:23 +00001024 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1025 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1026 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1027 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001028
Craig Topper13894fa2011-08-24 06:14:18 +00001029 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1030 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1031 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1032 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1033
1034 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1035 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1036 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1037 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1038
1039 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1040 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1041 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1042 // Don't lower v32i8 because there is no 128-bit byte mul
1043
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001044 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001045 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001046 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1047 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1048 EVT VT = SVT;
1049
1050 // Extract subvector is special because the value type
1051 // (result) is 128-bit but the source is 256-bit wide.
1052 if (VT.is128BitVector())
1053 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1054
1055 // Do not attempt to custom lower other non-256-bit vectors
1056 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001057 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001058
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001059 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1060 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1061 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1062 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001063 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001064 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001065 }
1066
David Greene54d8eba2011-01-27 22:38:56 +00001067 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001068 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1069 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1070 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001071
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001072 // Do not attempt to promote non-256-bit vectors
1073 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001074 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001075
1076 setOperationAction(ISD::AND, SVT, Promote);
1077 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1078 setOperationAction(ISD::OR, SVT, Promote);
1079 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1080 setOperationAction(ISD::XOR, SVT, Promote);
1081 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1082 setOperationAction(ISD::LOAD, SVT, Promote);
1083 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1084 setOperationAction(ISD::SELECT, SVT, Promote);
1085 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001086 }
David Greene9b9838d2009-06-29 16:47:10 +00001087 }
1088
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001089 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1090 // of this type with custom code.
1091 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1092 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1093 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1094 }
1095
Evan Cheng6be2c582006-04-05 23:38:46 +00001096 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001097 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001098
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001099
Eli Friedman962f5492010-06-02 19:35:46 +00001100 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1101 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001102 //
Eli Friedman962f5492010-06-02 19:35:46 +00001103 // FIXME: We really should do custom legalization for addition and
1104 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1105 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001106 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1107 // Add/Sub/Mul with overflow operations are custom lowered.
1108 MVT VT = IntVTs[i];
1109 setOperationAction(ISD::SADDO, VT, Custom);
1110 setOperationAction(ISD::UADDO, VT, Custom);
1111 setOperationAction(ISD::SSUBO, VT, Custom);
1112 setOperationAction(ISD::USUBO, VT, Custom);
1113 setOperationAction(ISD::SMULO, VT, Custom);
1114 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001115 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001116
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001117 // There are no 8-bit 3-address imul/mul instructions
1118 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1119 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001120
Evan Chengd54f2d52009-03-31 19:38:51 +00001121 if (!Subtarget->is64Bit()) {
1122 // These libcalls are not available in 32-bit.
1123 setLibcallName(RTLIB::SHL_I128, 0);
1124 setLibcallName(RTLIB::SRL_I128, 0);
1125 setLibcallName(RTLIB::SRA_I128, 0);
1126 }
1127
Evan Cheng206ee9d2006-07-07 08:33:52 +00001128 // We have target-specific dag combine patterns for the following nodes:
1129 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001130 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001131 setTargetDAGCombine(ISD::BUILD_VECTOR);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001132 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001133 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001134 setTargetDAGCombine(ISD::SHL);
1135 setTargetDAGCombine(ISD::SRA);
1136 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001137 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001138 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001139 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001140 setTargetDAGCombine(ISD::FADD);
1141 setTargetDAGCombine(ISD::FSUB);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001142 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001143 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001144 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001145 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001146 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001147 if (Subtarget->is64Bit())
1148 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001149
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001150 computeRegisterProperties();
1151
Evan Cheng05219282011-01-06 06:52:41 +00001152 // On Darwin, -Os means optimize for size without hurting performance,
1153 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001154 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001155 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001156 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001157 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1158 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1159 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001160 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001161 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001162
1163 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001164}
1165
Scott Michel5b8f82e2008-03-10 15:42:14 +00001166
Duncan Sands28b77e92011-09-06 19:07:46 +00001167EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1168 if (!VT.isVector()) return MVT::i8;
1169 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001170}
1171
1172
Evan Cheng29286502008-01-23 23:17:41 +00001173/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1174/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001175static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001176 if (MaxAlign == 16)
1177 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001178 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001179 if (VTy->getBitWidth() == 128)
1180 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001181 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001182 unsigned EltAlign = 0;
1183 getMaxByValAlign(ATy->getElementType(), EltAlign);
1184 if (EltAlign > MaxAlign)
1185 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001186 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001187 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1188 unsigned EltAlign = 0;
1189 getMaxByValAlign(STy->getElementType(i), EltAlign);
1190 if (EltAlign > MaxAlign)
1191 MaxAlign = EltAlign;
1192 if (MaxAlign == 16)
1193 break;
1194 }
1195 }
1196 return;
1197}
1198
1199/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1200/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001201/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1202/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001203unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001204 if (Subtarget->is64Bit()) {
1205 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001206 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001207 if (TyAlign > 8)
1208 return TyAlign;
1209 return 8;
1210 }
1211
Evan Cheng29286502008-01-23 23:17:41 +00001212 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001213 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001214 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001215 return Align;
1216}
Chris Lattner2b02a442007-02-25 08:29:00 +00001217
Evan Chengf0df0312008-05-15 08:39:06 +00001218/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001219/// and store operations as a result of memset, memcpy, and memmove
1220/// lowering. If DstAlign is zero that means it's safe to destination
1221/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1222/// means there isn't a need to check it against alignment requirement,
1223/// probably because the source does not need to be loaded. If
1224/// 'NonScalarIntSafe' is true, that means it's safe to return a
1225/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1226/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1227/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001228/// It returns EVT::Other if the type should be determined using generic
1229/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001230EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001231X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1232 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001233 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001234 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001235 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001236 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1237 // linux. This is because the stack realignment code can't handle certain
1238 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001239 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001240 if (NonScalarIntSafe &&
1241 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001242 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001243 (Subtarget->isUnalignedMemAccessFast() ||
1244 ((DstAlign == 0 || DstAlign >= 16) &&
1245 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001246 Subtarget->getStackAlignment() >= 16) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001247 if (Subtarget->hasAVX() &&
1248 Subtarget->getStackAlignment() >= 32)
1249 return MVT::v8f32;
1250 if (Subtarget->hasXMMInt())
Evan Cheng255f20f2010-04-01 06:04:33 +00001251 return MVT::v4i32;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001252 if (Subtarget->hasXMM())
Evan Cheng255f20f2010-04-01 06:04:33 +00001253 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001254 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001255 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001256 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001257 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001258 // Do not use f64 to lower memcpy if source is string constant. It's
1259 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001260 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001261 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001262 }
Evan Chengf0df0312008-05-15 08:39:06 +00001263 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001264 return MVT::i64;
1265 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001266}
1267
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001268/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1269/// current function. The returned value is a member of the
1270/// MachineJumpTableInfo::JTEntryKind enum.
1271unsigned X86TargetLowering::getJumpTableEncoding() const {
1272 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1273 // symbol.
1274 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1275 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001276 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001277
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001278 // Otherwise, use the normal jump table encoding heuristics.
1279 return TargetLowering::getJumpTableEncoding();
1280}
1281
Chris Lattnerc64daab2010-01-26 05:02:42 +00001282const MCExpr *
1283X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1284 const MachineBasicBlock *MBB,
1285 unsigned uid,MCContext &Ctx) const{
1286 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1287 Subtarget->isPICStyleGOT());
1288 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1289 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001290 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1291 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001292}
1293
Evan Chengcc415862007-11-09 01:32:10 +00001294/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1295/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001296SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001297 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001298 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001299 // This doesn't have DebugLoc associated with it, but is not really the
1300 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001301 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001302 return Table;
1303}
1304
Chris Lattner589c6f62010-01-26 06:28:43 +00001305/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1306/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1307/// MCExpr.
1308const MCExpr *X86TargetLowering::
1309getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1310 MCContext &Ctx) const {
1311 // X86-64 uses RIP relative addressing based on the jump table label.
1312 if (Subtarget->isPICStyleRIPRel())
1313 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1314
1315 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001316 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001317}
1318
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001319// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001320std::pair<const TargetRegisterClass*, uint8_t>
1321X86TargetLowering::findRepresentativeClass(EVT VT) const{
1322 const TargetRegisterClass *RRC = 0;
1323 uint8_t Cost = 1;
1324 switch (VT.getSimpleVT().SimpleTy) {
1325 default:
1326 return TargetLowering::findRepresentativeClass(VT);
1327 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1328 RRC = (Subtarget->is64Bit()
1329 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1330 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001331 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001332 RRC = X86::VR64RegisterClass;
1333 break;
1334 case MVT::f32: case MVT::f64:
1335 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1336 case MVT::v4f32: case MVT::v2f64:
1337 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1338 case MVT::v4f64:
1339 RRC = X86::VR128RegisterClass;
1340 break;
1341 }
1342 return std::make_pair(RRC, Cost);
1343}
1344
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001345bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1346 unsigned &Offset) const {
1347 if (!Subtarget->isTargetLinux())
1348 return false;
1349
1350 if (Subtarget->is64Bit()) {
1351 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1352 Offset = 0x28;
1353 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1354 AddressSpace = 256;
1355 else
1356 AddressSpace = 257;
1357 } else {
1358 // %gs:0x14 on i386
1359 Offset = 0x14;
1360 AddressSpace = 256;
1361 }
1362 return true;
1363}
1364
1365
Chris Lattner2b02a442007-02-25 08:29:00 +00001366//===----------------------------------------------------------------------===//
1367// Return Value Calling Convention Implementation
1368//===----------------------------------------------------------------------===//
1369
Chris Lattner59ed56b2007-02-28 04:55:35 +00001370#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001371
Michael J. Spencerec38de22010-10-10 22:04:20 +00001372bool
Eric Christopher471e4222011-06-08 23:55:35 +00001373X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1374 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001375 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001376 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001377 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001378 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001379 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001380 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001381}
1382
Dan Gohman98ca4f22009-08-05 01:29:28 +00001383SDValue
1384X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001385 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001386 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001387 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001388 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001389 MachineFunction &MF = DAG.getMachineFunction();
1390 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001391
Chris Lattner9774c912007-02-27 05:28:59 +00001392 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001393 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001394 RVLocs, *DAG.getContext());
1395 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001396
Evan Chengdcea1632010-02-04 02:40:39 +00001397 // Add the regs to the liveout set for the function.
1398 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1399 for (unsigned i = 0; i != RVLocs.size(); ++i)
1400 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1401 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001402
Dan Gohman475871a2008-07-27 21:46:04 +00001403 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001404
Dan Gohman475871a2008-07-27 21:46:04 +00001405 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001406 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1407 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001408 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1409 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001410
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001411 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001412 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1413 CCValAssign &VA = RVLocs[i];
1414 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001415 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001416 EVT ValVT = ValToCopy.getValueType();
1417
Dale Johannesenc4510512010-09-24 19:05:48 +00001418 // If this is x86-64, and we disabled SSE, we can't return FP values,
1419 // or SSE or MMX vectors.
1420 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1421 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001422 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001423 report_fatal_error("SSE register return with SSE disabled");
1424 }
1425 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1426 // llvm-gcc has never done it right and no one has noticed, so this
1427 // should be OK for now.
1428 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001429 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001430 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001431
Chris Lattner447ff682008-03-11 03:23:40 +00001432 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1433 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001434 if (VA.getLocReg() == X86::ST0 ||
1435 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001436 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1437 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001438 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001439 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001440 RetOps.push_back(ValToCopy);
1441 // Don't emit a copytoreg.
1442 continue;
1443 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001444
Evan Cheng242b38b2009-02-23 09:03:22 +00001445 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1446 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001447 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001448 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001449 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001450 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001451 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1452 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001453 // If we don't have SSE2 available, convert to v4f32 so the generated
1454 // register is legal.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001455 if (!Subtarget->hasXMMInt())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001456 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001457 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001458 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001459 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001460
Dale Johannesendd64c412009-02-04 00:33:20 +00001461 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001462 Flag = Chain.getValue(1);
1463 }
Dan Gohman61a92132008-04-21 23:59:07 +00001464
1465 // The x86-64 ABI for returning structs by value requires that we copy
1466 // the sret argument into %rax for the return. We saved the argument into
1467 // a virtual register in the entry block, so now we copy the value out
1468 // and into %rax.
1469 if (Subtarget->is64Bit() &&
1470 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1471 MachineFunction &MF = DAG.getMachineFunction();
1472 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1473 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001474 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001475 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001476 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001477
Dale Johannesendd64c412009-02-04 00:33:20 +00001478 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001479 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001480
1481 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001482 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001483 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001484
Chris Lattner447ff682008-03-11 03:23:40 +00001485 RetOps[0] = Chain; // Update chain.
1486
1487 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001488 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001489 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001490
1491 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001492 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001493}
1494
Evan Cheng3d2125c2010-11-30 23:55:39 +00001495bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1496 if (N->getNumValues() != 1)
1497 return false;
1498 if (!N->hasNUsesOfValue(1, 0))
1499 return false;
1500
1501 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001502 if (Copy->getOpcode() != ISD::CopyToReg &&
1503 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001504 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001505
1506 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001507 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001508 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001509 if (UI->getOpcode() != X86ISD::RET_FLAG)
1510 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001511 HasRet = true;
1512 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001513
Evan Cheng1bf891a2010-12-01 22:59:46 +00001514 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001515}
1516
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001517EVT
1518X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001519 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001520 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001521 // TODO: Is this also valid on 32-bit?
1522 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001523 ReturnMVT = MVT::i8;
1524 else
1525 ReturnMVT = MVT::i32;
1526
1527 EVT MinVT = getRegisterType(Context, ReturnMVT);
1528 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001529}
1530
Dan Gohman98ca4f22009-08-05 01:29:28 +00001531/// LowerCallResult - Lower the result values of a call into the
1532/// appropriate copies out of appropriate physical registers.
1533///
1534SDValue
1535X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001536 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001537 const SmallVectorImpl<ISD::InputArg> &Ins,
1538 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001539 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001540
Chris Lattnere32bbf62007-02-28 07:09:55 +00001541 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001542 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001543 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001544 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1545 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001546 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001547
Chris Lattner3085e152007-02-25 08:59:22 +00001548 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001549 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001550 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001551 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001552
Torok Edwin3f142c32009-02-01 18:15:56 +00001553 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001554 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001555 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001556 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001557 }
1558
Evan Cheng79fb3b42009-02-20 20:43:02 +00001559 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001560
1561 // If this is a call to a function that returns an fp value on the floating
1562 // point stack, we must guarantee the the value is popped from the stack, so
1563 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001564 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001565 // instead.
1566 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1567 // If we prefer to use the value in xmm registers, copy it out as f80 and
1568 // use a truncate to move it from fp stack reg to xmm reg.
1569 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001570 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001571 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1572 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001573 Val = Chain.getValue(0);
1574
1575 // Round the f80 to the right size, which also moves it to the appropriate
1576 // xmm register.
1577 if (CopyVT != VA.getValVT())
1578 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1579 // This truncation won't change the value.
1580 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001581 } else {
1582 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1583 CopyVT, InFlag).getValue(1);
1584 Val = Chain.getValue(0);
1585 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001586 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001587 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001588 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001589
Dan Gohman98ca4f22009-08-05 01:29:28 +00001590 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001591}
1592
1593
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001594//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001595// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001596//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001597// StdCall calling convention seems to be standard for many Windows' API
1598// routines and around. It differs from C calling convention just a little:
1599// callee should clean up the stack, not caller. Symbols should be also
1600// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001601// For info on fast calling convention see Fast Calling Convention (tail call)
1602// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001603
Dan Gohman98ca4f22009-08-05 01:29:28 +00001604/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001605/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001606static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1607 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001608 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001609
Dan Gohman98ca4f22009-08-05 01:29:28 +00001610 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001611}
1612
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001613/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001614/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001615static bool
1616ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1617 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001618 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001619
Dan Gohman98ca4f22009-08-05 01:29:28 +00001620 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001621}
1622
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001623/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1624/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001625/// the specific parameter attribute. The copy will be passed as a byval
1626/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001627static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001628CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001629 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1630 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001631 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001632
Dale Johannesendd64c412009-02-04 00:33:20 +00001633 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001634 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001635 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001636}
1637
Chris Lattner29689432010-03-11 00:22:57 +00001638/// IsTailCallConvention - Return true if the calling convention is one that
1639/// supports tail call optimization.
1640static bool IsTailCallConvention(CallingConv::ID CC) {
1641 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1642}
1643
Evan Cheng485fafc2011-03-21 01:19:09 +00001644bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1645 if (!CI->isTailCall())
1646 return false;
1647
1648 CallSite CS(CI);
1649 CallingConv::ID CalleeCC = CS.getCallingConv();
1650 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1651 return false;
1652
1653 return true;
1654}
1655
Evan Cheng0c439eb2010-01-27 00:07:07 +00001656/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1657/// a tailcall target by changing its ABI.
1658static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001659 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001660}
1661
Dan Gohman98ca4f22009-08-05 01:29:28 +00001662SDValue
1663X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001664 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001665 const SmallVectorImpl<ISD::InputArg> &Ins,
1666 DebugLoc dl, SelectionDAG &DAG,
1667 const CCValAssign &VA,
1668 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001669 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001670 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001671 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001672 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001673 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001674 EVT ValVT;
1675
1676 // If value is passed by pointer we have address passed instead of the value
1677 // itself.
1678 if (VA.getLocInfo() == CCValAssign::Indirect)
1679 ValVT = VA.getLocVT();
1680 else
1681 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001682
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001683 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001684 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001685 // In case of tail call optimization mark all arguments mutable. Since they
1686 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001687 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001688 unsigned Bytes = Flags.getByValSize();
1689 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1690 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001691 return DAG.getFrameIndex(FI, getPointerTy());
1692 } else {
1693 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001694 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001695 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1696 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001697 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001698 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001699 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001700}
1701
Dan Gohman475871a2008-07-27 21:46:04 +00001702SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001703X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001704 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001705 bool isVarArg,
1706 const SmallVectorImpl<ISD::InputArg> &Ins,
1707 DebugLoc dl,
1708 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001709 SmallVectorImpl<SDValue> &InVals)
1710 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001711 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001712 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001713
Gordon Henriksen86737662008-01-05 16:56:59 +00001714 const Function* Fn = MF.getFunction();
1715 if (Fn->hasExternalLinkage() &&
1716 Subtarget->isTargetCygMing() &&
1717 Fn->getName() == "main")
1718 FuncInfo->setForceFramePointer(true);
1719
Evan Cheng1bc78042006-04-26 01:20:17 +00001720 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001721 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001722 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001723
Chris Lattner29689432010-03-11 00:22:57 +00001724 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1725 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001726
Chris Lattner638402b2007-02-28 07:00:42 +00001727 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001728 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001729 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001730 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001731
1732 // Allocate shadow area for Win64
1733 if (IsWin64) {
1734 CCInfo.AllocateStack(32, 8);
1735 }
1736
Duncan Sands45907662010-10-31 13:21:44 +00001737 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001738
Chris Lattnerf39f7712007-02-28 05:46:49 +00001739 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001740 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001741 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1742 CCValAssign &VA = ArgLocs[i];
1743 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1744 // places.
1745 assert(VA.getValNo() != LastVal &&
1746 "Don't support value assigned to multiple locs yet");
1747 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001748
Chris Lattnerf39f7712007-02-28 05:46:49 +00001749 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001750 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001751 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001752 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001753 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001754 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001755 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001756 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001757 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001758 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001759 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001760 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1761 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001762 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001763 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001764 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001765 RC = X86::VR64RegisterClass;
1766 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001767 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001768
Devang Patel68e6bee2011-02-21 23:21:26 +00001769 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001770 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001771
Chris Lattnerf39f7712007-02-28 05:46:49 +00001772 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1773 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1774 // right size.
1775 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001776 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001777 DAG.getValueType(VA.getValVT()));
1778 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001779 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001780 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001781 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001782 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001783
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001784 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001785 // Handle MMX values passed in XMM regs.
1786 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001787 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1788 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001789 } else
1790 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001791 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001792 } else {
1793 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001794 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001795 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001796
1797 // If value is passed via pointer - do a load.
1798 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001799 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1800 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001801
Dan Gohman98ca4f22009-08-05 01:29:28 +00001802 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001803 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001804
Dan Gohman61a92132008-04-21 23:59:07 +00001805 // The x86-64 ABI for returning structs by value requires that we copy
1806 // the sret argument into %rax for the return. Save the argument into
1807 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001808 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001809 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1810 unsigned Reg = FuncInfo->getSRetReturnReg();
1811 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001812 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001813 FuncInfo->setSRetReturnReg(Reg);
1814 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001815 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001816 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001817 }
1818
Chris Lattnerf39f7712007-02-28 05:46:49 +00001819 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001820 // Align stack specially for tail calls.
1821 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001822 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001823
Evan Cheng1bc78042006-04-26 01:20:17 +00001824 // If the function takes variable number of arguments, make a frame index for
1825 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001826 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001827 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1828 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001829 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001830 }
1831 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001832 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1833
1834 // FIXME: We should really autogenerate these arrays
1835 static const unsigned GPR64ArgRegsWin64[] = {
1836 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001837 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001838 static const unsigned GPR64ArgRegs64Bit[] = {
1839 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1840 };
1841 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001842 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1843 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1844 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001845 const unsigned *GPR64ArgRegs;
1846 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001847
1848 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001849 // The XMM registers which might contain var arg parameters are shadowed
1850 // in their paired GPR. So we only need to save the GPR to their home
1851 // slots.
1852 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001853 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001854 } else {
1855 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1856 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001857
1858 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001859 }
1860 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1861 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001862
Devang Patel578efa92009-06-05 21:57:13 +00001863 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001864 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001865 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001866 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001867 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001868 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001869 // Kernel mode asks for SSE to be disabled, so don't push them
1870 // on the stack.
1871 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001872
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001873 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001874 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001875 // Get to the caller-allocated home save location. Add 8 to account
1876 // for the return address.
1877 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001878 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001879 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001880 // Fixup to set vararg frame on shadow area (4 x i64).
1881 if (NumIntRegs < 4)
1882 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001883 } else {
1884 // For X86-64, if there are vararg parameters that are passed via
1885 // registers, then we must store them to their spots on the stack so they
1886 // may be loaded by deferencing the result of va_next.
1887 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1888 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1889 FuncInfo->setRegSaveFrameIndex(
1890 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001891 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001892 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001893
Gordon Henriksen86737662008-01-05 16:56:59 +00001894 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001895 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001896 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1897 getPointerTy());
1898 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001899 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001900 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1901 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001902 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001903 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001904 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001905 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001906 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001907 MachinePointerInfo::getFixedStack(
1908 FuncInfo->getRegSaveFrameIndex(), Offset),
1909 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001910 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001911 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001912 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001913
Dan Gohmanface41a2009-08-16 21:24:25 +00001914 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1915 // Now store the XMM (fp + vector) parameter registers.
1916 SmallVector<SDValue, 11> SaveXMMOps;
1917 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001918
Devang Patel68e6bee2011-02-21 23:21:26 +00001919 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001920 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1921 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001922
Dan Gohman1e93df62010-04-17 14:41:14 +00001923 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1924 FuncInfo->getRegSaveFrameIndex()));
1925 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1926 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001927
Dan Gohmanface41a2009-08-16 21:24:25 +00001928 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001929 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001930 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001931 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1932 SaveXMMOps.push_back(Val);
1933 }
1934 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1935 MVT::Other,
1936 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001937 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001938
1939 if (!MemOps.empty())
1940 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1941 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001942 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001943 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001944
Gordon Henriksen86737662008-01-05 16:56:59 +00001945 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00001946 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001947 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001948 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001949 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001950 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001951 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001952 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001953 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001954
Gordon Henriksen86737662008-01-05 16:56:59 +00001955 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001956 // RegSaveFrameIndex is X86-64 only.
1957 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001958 if (CallConv == CallingConv::X86_FastCall ||
1959 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001960 // fastcc functions can't have varargs.
1961 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001962 }
Evan Cheng25caf632006-05-23 21:06:34 +00001963
Rafael Espindola76927d752011-08-30 19:39:58 +00001964 FuncInfo->setArgumentStackSize(StackSize);
1965
Dan Gohman98ca4f22009-08-05 01:29:28 +00001966 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001967}
1968
Dan Gohman475871a2008-07-27 21:46:04 +00001969SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001970X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1971 SDValue StackPtr, SDValue Arg,
1972 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001973 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001974 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001975 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001976 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001977 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001978 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001979 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001980
1981 return DAG.getStore(Chain, dl, Arg, PtrOff,
1982 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001983 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001984}
1985
Bill Wendling64e87322009-01-16 19:25:27 +00001986/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001987/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001988SDValue
1989X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001990 SDValue &OutRetAddr, SDValue Chain,
1991 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001992 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001993 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001994 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001995 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001996
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001997 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001998 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1999 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002000 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002001}
2002
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002003/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002004/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002005static SDValue
2006EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002007 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002008 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002009 // Store the return address to the appropriate stack slot.
2010 if (!FPDiff) return Chain;
2011 // Calculate the new stack slot for the return address.
2012 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002013 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002014 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002015 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002016 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002017 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002018 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002019 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002020 return Chain;
2021}
2022
Dan Gohman98ca4f22009-08-05 01:29:28 +00002023SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002024X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002025 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002026 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002027 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002028 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002029 const SmallVectorImpl<ISD::InputArg> &Ins,
2030 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002031 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002032 MachineFunction &MF = DAG.getMachineFunction();
2033 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002034 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002035 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002036 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002037
Evan Cheng5f941932010-02-05 02:21:12 +00002038 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002039 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002040 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2041 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002042 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002043
2044 // Sibcalls are automatically detected tailcalls which do not require
2045 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002046 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002047 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002048
2049 if (isTailCall)
2050 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002051 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002052
Chris Lattner29689432010-03-11 00:22:57 +00002053 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2054 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002055
Chris Lattner638402b2007-02-28 07:00:42 +00002056 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002057 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002058 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002059 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002060
2061 // Allocate shadow area for Win64
2062 if (IsWin64) {
2063 CCInfo.AllocateStack(32, 8);
2064 }
2065
Duncan Sands45907662010-10-31 13:21:44 +00002066 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002067
Chris Lattner423c5f42007-02-28 05:31:48 +00002068 // Get a count of how many bytes are to be pushed on the stack.
2069 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002070 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002071 // This is a sibcall. The memory operands are available in caller's
2072 // own caller's stack.
2073 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002074 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002075 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002076
Gordon Henriksen86737662008-01-05 16:56:59 +00002077 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002078 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002079 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002080 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002081 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2082 FPDiff = NumBytesCallerPushed - NumBytes;
2083
2084 // Set the delta of movement of the returnaddr stackslot.
2085 // But only set if delta is greater than previous delta.
2086 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2087 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2088 }
2089
Evan Chengf22f9b32010-02-06 03:28:46 +00002090 if (!IsSibcall)
2091 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002092
Dan Gohman475871a2008-07-27 21:46:04 +00002093 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002094 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002095 if (isTailCall && FPDiff)
2096 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2097 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002098
Dan Gohman475871a2008-07-27 21:46:04 +00002099 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2100 SmallVector<SDValue, 8> MemOpChains;
2101 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002102
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002103 // Walk the register/memloc assignments, inserting copies/loads. In the case
2104 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002105 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2106 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002107 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002108 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002109 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002110 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002111
Chris Lattner423c5f42007-02-28 05:31:48 +00002112 // Promote the value if needed.
2113 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002114 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002115 case CCValAssign::Full: break;
2116 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002117 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002118 break;
2119 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002120 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002121 break;
2122 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002123 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2124 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002125 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002126 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2127 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002128 } else
2129 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2130 break;
2131 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002132 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002133 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002134 case CCValAssign::Indirect: {
2135 // Store the argument.
2136 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002137 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002138 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002139 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002140 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002141 Arg = SpillSlot;
2142 break;
2143 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002144 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002145
Chris Lattner423c5f42007-02-28 05:31:48 +00002146 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002147 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2148 if (isVarArg && IsWin64) {
2149 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2150 // shadow reg if callee is a varargs function.
2151 unsigned ShadowReg = 0;
2152 switch (VA.getLocReg()) {
2153 case X86::XMM0: ShadowReg = X86::RCX; break;
2154 case X86::XMM1: ShadowReg = X86::RDX; break;
2155 case X86::XMM2: ShadowReg = X86::R8; break;
2156 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002157 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002158 if (ShadowReg)
2159 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002160 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002161 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002162 assert(VA.isMemLoc());
2163 if (StackPtr.getNode() == 0)
2164 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2165 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2166 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002167 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002168 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002169
Evan Cheng32fe1032006-05-25 00:59:30 +00002170 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002171 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002172 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002173
Evan Cheng347d5f72006-04-28 21:29:37 +00002174 // Build a sequence of copy-to-reg nodes chained together with token chain
2175 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002176 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002177 // Tail call byval lowering might overwrite argument registers so in case of
2178 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002179 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002180 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002181 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002182 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002183 InFlag = Chain.getValue(1);
2184 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002185
Chris Lattner88e1fd52009-07-09 04:24:46 +00002186 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002187 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2188 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002189 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002190 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2191 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002192 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002193 InFlag);
2194 InFlag = Chain.getValue(1);
2195 } else {
2196 // If we are tail calling and generating PIC/GOT style code load the
2197 // address of the callee into ECX. The value in ecx is used as target of
2198 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2199 // for tail calls on PIC/GOT architectures. Normally we would just put the
2200 // address of GOT into ebx and then call target@PLT. But for tail calls
2201 // ebx would be restored (since ebx is callee saved) before jumping to the
2202 // target@PLT.
2203
2204 // Note: The actual moving to ECX is done further down.
2205 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2206 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2207 !G->getGlobal()->hasProtectedVisibility())
2208 Callee = LowerGlobalAddress(Callee, DAG);
2209 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002210 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002211 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002212 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002213
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002214 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002215 // From AMD64 ABI document:
2216 // For calls that may call functions that use varargs or stdargs
2217 // (prototype-less calls or calls to functions containing ellipsis (...) in
2218 // the declaration) %al is used as hidden argument to specify the number
2219 // of SSE registers used. The contents of %al do not need to match exactly
2220 // the number of registers, but must be an ubound on the number of SSE
2221 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002222
Gordon Henriksen86737662008-01-05 16:56:59 +00002223 // Count the number of XMM registers allocated.
2224 static const unsigned XMMArgRegs[] = {
2225 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2226 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2227 };
2228 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002229 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002230 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002231
Dale Johannesendd64c412009-02-04 00:33:20 +00002232 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002233 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002234 InFlag = Chain.getValue(1);
2235 }
2236
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002237
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002238 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002239 if (isTailCall) {
2240 // Force all the incoming stack arguments to be loaded from the stack
2241 // before any new outgoing arguments are stored to the stack, because the
2242 // outgoing stack slots may alias the incoming argument stack slots, and
2243 // the alias isn't otherwise explicit. This is slightly more conservative
2244 // than necessary, because it means that each store effectively depends
2245 // on every argument instead of just those arguments it would clobber.
2246 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2247
Dan Gohman475871a2008-07-27 21:46:04 +00002248 SmallVector<SDValue, 8> MemOpChains2;
2249 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002250 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002251 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002252 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002253 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002254 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2255 CCValAssign &VA = ArgLocs[i];
2256 if (VA.isRegLoc())
2257 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002258 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002259 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002260 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002261 // Create frame index.
2262 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002263 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002264 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002265 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002266
Duncan Sands276dcbd2008-03-21 09:14:45 +00002267 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002268 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002269 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002270 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002271 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002272 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002273 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002274
Dan Gohman98ca4f22009-08-05 01:29:28 +00002275 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2276 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002277 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002278 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002279 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002280 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002281 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002282 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002283 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002284 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002285 }
2286 }
2287
2288 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002289 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002290 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002291
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002292 // Copy arguments to their registers.
2293 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002294 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002295 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002296 InFlag = Chain.getValue(1);
2297 }
Dan Gohman475871a2008-07-27 21:46:04 +00002298 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002299
Gordon Henriksen86737662008-01-05 16:56:59 +00002300 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002301 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002302 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002303 }
2304
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002305 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2306 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2307 // In the 64-bit large code model, we have to make all calls
2308 // through a register, since the call instruction's 32-bit
2309 // pc-relative offset may not be large enough to hold the whole
2310 // address.
2311 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002312 // If the callee is a GlobalAddress node (quite common, every direct call
2313 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2314 // it.
2315
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002316 // We should use extra load for direct calls to dllimported functions in
2317 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002318 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002319 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002320 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002321 bool ExtraLoad = false;
2322 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002323
Chris Lattner48a7d022009-07-09 05:02:21 +00002324 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2325 // external symbols most go through the PLT in PIC mode. If the symbol
2326 // has hidden or protected visibility, or if it is static or local, then
2327 // we don't need to use the PLT - we can directly call it.
2328 if (Subtarget->isTargetELF() &&
2329 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002330 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002331 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002332 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002333 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002334 (!Subtarget->getTargetTriple().isMacOSX() ||
2335 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002336 // PC-relative references to external symbols should go through $stub,
2337 // unless we're building with the leopard linker or later, which
2338 // automatically synthesizes these stubs.
2339 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002340 } else if (Subtarget->isPICStyleRIPRel() &&
2341 isa<Function>(GV) &&
2342 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2343 // If the function is marked as non-lazy, generate an indirect call
2344 // which loads from the GOT directly. This avoids runtime overhead
2345 // at the cost of eager binding (and one extra byte of encoding).
2346 OpFlags = X86II::MO_GOTPCREL;
2347 WrapperKind = X86ISD::WrapperRIP;
2348 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002349 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002350
Devang Patel0d881da2010-07-06 22:08:15 +00002351 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002352 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002353
2354 // Add a wrapper if needed.
2355 if (WrapperKind != ISD::DELETED_NODE)
2356 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2357 // Add extra indirection if needed.
2358 if (ExtraLoad)
2359 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2360 MachinePointerInfo::getGOT(),
2361 false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002362 }
Bill Wendling056292f2008-09-16 21:48:12 +00002363 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002364 unsigned char OpFlags = 0;
2365
Evan Cheng1bf891a2010-12-01 22:59:46 +00002366 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2367 // external symbols should go through the PLT.
2368 if (Subtarget->isTargetELF() &&
2369 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2370 OpFlags = X86II::MO_PLT;
2371 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002372 (!Subtarget->getTargetTriple().isMacOSX() ||
2373 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002374 // PC-relative references to external symbols should go through $stub,
2375 // unless we're building with the leopard linker or later, which
2376 // automatically synthesizes these stubs.
2377 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002378 }
Eric Christopherfd179292009-08-27 18:07:15 +00002379
Chris Lattner48a7d022009-07-09 05:02:21 +00002380 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2381 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002382 }
2383
Chris Lattnerd96d0722007-02-25 06:40:16 +00002384 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002385 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002386 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002387
Evan Chengf22f9b32010-02-06 03:28:46 +00002388 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002389 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2390 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002391 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002392 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002393
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002394 Ops.push_back(Chain);
2395 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002396
Dan Gohman98ca4f22009-08-05 01:29:28 +00002397 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002398 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002399
Gordon Henriksen86737662008-01-05 16:56:59 +00002400 // Add argument registers to the end of the list so that they are known live
2401 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002402 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2403 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2404 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002405
Evan Cheng586ccac2008-03-18 23:36:35 +00002406 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002407 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002408 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2409
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002410 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002411 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002412 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002413
Gabor Greifba36cb52008-08-28 21:40:38 +00002414 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002415 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002416
Dan Gohman98ca4f22009-08-05 01:29:28 +00002417 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002418 // We used to do:
2419 //// If this is the first return lowered for this function, add the regs
2420 //// to the liveout set for the function.
2421 // This isn't right, although it's probably harmless on x86; liveouts
2422 // should be computed from returns not tail calls. Consider a void
2423 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002424 return DAG.getNode(X86ISD::TC_RETURN, dl,
2425 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002426 }
2427
Dale Johannesenace16102009-02-03 19:33:06 +00002428 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002429 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002430
Chris Lattner2d297092006-05-23 18:50:38 +00002431 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002432 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002433 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002434 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002435 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002436 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002437 // pops the hidden struct pointer, so we have to push it back.
2438 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002439 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002440 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002441 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002442
Gordon Henriksenae636f82008-01-03 16:47:34 +00002443 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002444 if (!IsSibcall) {
2445 Chain = DAG.getCALLSEQ_END(Chain,
2446 DAG.getIntPtrConstant(NumBytes, true),
2447 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2448 true),
2449 InFlag);
2450 InFlag = Chain.getValue(1);
2451 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002452
Chris Lattner3085e152007-02-25 08:59:22 +00002453 // Handle result values, copying them out of physregs into vregs that we
2454 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002455 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2456 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002457}
2458
Evan Cheng25ab6902006-09-08 06:48:29 +00002459
2460//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002461// Fast Calling Convention (tail call) implementation
2462//===----------------------------------------------------------------------===//
2463
2464// Like std call, callee cleans arguments, convention except that ECX is
2465// reserved for storing the tail called function address. Only 2 registers are
2466// free for argument passing (inreg). Tail call optimization is performed
2467// provided:
2468// * tailcallopt is enabled
2469// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002470// On X86_64 architecture with GOT-style position independent code only local
2471// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002472// To keep the stack aligned according to platform abi the function
2473// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2474// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002475// If a tail called function callee has more arguments than the caller the
2476// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002477// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002478// original REtADDR, but before the saved framepointer or the spilled registers
2479// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2480// stack layout:
2481// arg1
2482// arg2
2483// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002484// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002485// move area ]
2486// (possible EBP)
2487// ESI
2488// EDI
2489// local1 ..
2490
2491/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2492/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002493unsigned
2494X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2495 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002496 MachineFunction &MF = DAG.getMachineFunction();
2497 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002498 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002499 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002500 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002501 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002502 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002503 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2504 // Number smaller than 12 so just add the difference.
2505 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2506 } else {
2507 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002508 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002509 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002510 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002511 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002512}
2513
Evan Cheng5f941932010-02-05 02:21:12 +00002514/// MatchingStackOffset - Return true if the given stack call argument is
2515/// already available in the same position (relatively) of the caller's
2516/// incoming argument stack.
2517static
2518bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2519 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2520 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002521 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2522 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002523 if (Arg.getOpcode() == ISD::CopyFromReg) {
2524 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002525 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002526 return false;
2527 MachineInstr *Def = MRI->getVRegDef(VR);
2528 if (!Def)
2529 return false;
2530 if (!Flags.isByVal()) {
2531 if (!TII->isLoadFromStackSlot(Def, FI))
2532 return false;
2533 } else {
2534 unsigned Opcode = Def->getOpcode();
2535 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2536 Def->getOperand(1).isFI()) {
2537 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002538 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002539 } else
2540 return false;
2541 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002542 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2543 if (Flags.isByVal())
2544 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002545 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002546 // define @foo(%struct.X* %A) {
2547 // tail call @bar(%struct.X* byval %A)
2548 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002549 return false;
2550 SDValue Ptr = Ld->getBasePtr();
2551 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2552 if (!FINode)
2553 return false;
2554 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002555 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002556 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002557 FI = FINode->getIndex();
2558 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002559 } else
2560 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002561
Evan Cheng4cae1332010-03-05 08:38:04 +00002562 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002563 if (!MFI->isFixedObjectIndex(FI))
2564 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002565 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002566}
2567
Dan Gohman98ca4f22009-08-05 01:29:28 +00002568/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2569/// for tail call optimization. Targets which want to do tail call
2570/// optimization should implement this function.
2571bool
2572X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002573 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002574 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002575 bool isCalleeStructRet,
2576 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002577 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002578 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002579 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002580 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002581 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002582 CalleeCC != CallingConv::C)
2583 return false;
2584
Evan Cheng7096ae42010-01-29 06:45:59 +00002585 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002586 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002587 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002588 CallingConv::ID CallerCC = CallerF->getCallingConv();
2589 bool CCMatch = CallerCC == CalleeCC;
2590
Dan Gohman1797ed52010-02-08 20:27:50 +00002591 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002592 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002593 return true;
2594 return false;
2595 }
2596
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002597 // Look for obvious safe cases to perform tail call optimization that do not
2598 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002599
Evan Cheng2c12cb42010-03-26 16:26:03 +00002600 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2601 // emit a special epilogue.
2602 if (RegInfo->needsStackRealignment(MF))
2603 return false;
2604
Evan Chenga375d472010-03-15 18:54:48 +00002605 // Also avoid sibcall optimization if either caller or callee uses struct
2606 // return semantics.
2607 if (isCalleeStructRet || isCallerStructRet)
2608 return false;
2609
Chad Rosier2416da32011-06-24 21:15:36 +00002610 // An stdcall caller is expected to clean up its arguments; the callee
2611 // isn't going to do that.
2612 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2613 return false;
2614
Chad Rosier871f6642011-05-18 19:59:50 +00002615 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002616 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002617 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002618
2619 // Optimizing for varargs on Win64 is unlikely to be safe without
2620 // additional testing.
2621 if (Subtarget->isTargetWin64())
2622 return false;
2623
Chad Rosier871f6642011-05-18 19:59:50 +00002624 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002625 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2626 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002627
Chad Rosier871f6642011-05-18 19:59:50 +00002628 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2629 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2630 if (!ArgLocs[i].isRegLoc())
2631 return false;
2632 }
2633
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002634 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2635 // Therefore if it's not used by the call it is not safe to optimize this into
2636 // a sibcall.
2637 bool Unused = false;
2638 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2639 if (!Ins[i].Used) {
2640 Unused = true;
2641 break;
2642 }
2643 }
2644 if (Unused) {
2645 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002646 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2647 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002648 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002649 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002650 CCValAssign &VA = RVLocs[i];
2651 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2652 return false;
2653 }
2654 }
2655
Evan Cheng13617962010-04-30 01:12:32 +00002656 // If the calling conventions do not match, then we'd better make sure the
2657 // results are returned in the same way as what the caller expects.
2658 if (!CCMatch) {
2659 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002660 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2661 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002662 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2663
2664 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002665 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2666 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002667 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2668
2669 if (RVLocs1.size() != RVLocs2.size())
2670 return false;
2671 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2672 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2673 return false;
2674 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2675 return false;
2676 if (RVLocs1[i].isRegLoc()) {
2677 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2678 return false;
2679 } else {
2680 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2681 return false;
2682 }
2683 }
2684 }
2685
Evan Chenga6bff982010-01-30 01:22:00 +00002686 // If the callee takes no arguments then go on to check the results of the
2687 // call.
2688 if (!Outs.empty()) {
2689 // Check if stack adjustment is needed. For now, do not do this if any
2690 // argument is passed on the stack.
2691 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002692 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2693 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002694
2695 // Allocate shadow area for Win64
2696 if (Subtarget->isTargetWin64()) {
2697 CCInfo.AllocateStack(32, 8);
2698 }
2699
Duncan Sands45907662010-10-31 13:21:44 +00002700 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002701 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002702 MachineFunction &MF = DAG.getMachineFunction();
2703 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2704 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002705
2706 // Check if the arguments are already laid out in the right way as
2707 // the caller's fixed stack objects.
2708 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002709 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2710 const X86InstrInfo *TII =
2711 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002712 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2713 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002714 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002715 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002716 if (VA.getLocInfo() == CCValAssign::Indirect)
2717 return false;
2718 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002719 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2720 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002721 return false;
2722 }
2723 }
2724 }
Evan Cheng9c044672010-05-29 01:35:22 +00002725
2726 // If the tailcall address may be in a register, then make sure it's
2727 // possible to register allocate for it. In 32-bit, the call address can
2728 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002729 // callee-saved registers are restored. These happen to be the same
2730 // registers used to pass 'inreg' arguments so watch out for those.
2731 if (!Subtarget->is64Bit() &&
2732 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002733 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002734 unsigned NumInRegs = 0;
2735 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2736 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002737 if (!VA.isRegLoc())
2738 continue;
2739 unsigned Reg = VA.getLocReg();
2740 switch (Reg) {
2741 default: break;
2742 case X86::EAX: case X86::EDX: case X86::ECX:
2743 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002744 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002745 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002746 }
2747 }
2748 }
Evan Chenga6bff982010-01-30 01:22:00 +00002749 }
Evan Chengb1712452010-01-27 06:25:16 +00002750
Evan Cheng86809cc2010-02-03 03:28:02 +00002751 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002752}
2753
Dan Gohman3df24e62008-09-03 23:12:08 +00002754FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002755X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2756 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002757}
2758
2759
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002760//===----------------------------------------------------------------------===//
2761// Other Lowering Hooks
2762//===----------------------------------------------------------------------===//
2763
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002764static bool MayFoldLoad(SDValue Op) {
2765 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2766}
2767
2768static bool MayFoldIntoStore(SDValue Op) {
2769 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2770}
2771
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002772static bool isTargetShuffle(unsigned Opcode) {
2773 switch(Opcode) {
2774 default: return false;
2775 case X86ISD::PSHUFD:
2776 case X86ISD::PSHUFHW:
2777 case X86ISD::PSHUFLW:
2778 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002779 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002780 case X86ISD::SHUFPS:
2781 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002782 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002783 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002784 case X86ISD::MOVLPS:
2785 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002786 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002787 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002788 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002789 case X86ISD::MOVSS:
2790 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002791 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002792 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002793 case X86ISD::VUNPCKLPSY:
2794 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002795 case X86ISD::PUNPCKLWD:
2796 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002797 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002798 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002799 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002800 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002801 case X86ISD::VUNPCKHPSY:
2802 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002803 case X86ISD::PUNPCKHWD:
2804 case X86ISD::PUNPCKHBW:
2805 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002806 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002807 case X86ISD::VPERMILPS:
2808 case X86ISD::VPERMILPSY:
2809 case X86ISD::VPERMILPD:
2810 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002811 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002812 return true;
2813 }
2814 return false;
2815}
2816
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002817static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002818 SDValue V1, SelectionDAG &DAG) {
2819 switch(Opc) {
2820 default: llvm_unreachable("Unknown x86 shuffle node");
2821 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002822 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002823 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002824 return DAG.getNode(Opc, dl, VT, V1);
2825 }
2826
2827 return SDValue();
2828}
2829
2830static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002831 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002832 switch(Opc) {
2833 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002834 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002835 case X86ISD::PSHUFHW:
2836 case X86ISD::PSHUFLW:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002837 case X86ISD::VPERMILPS:
2838 case X86ISD::VPERMILPSY:
2839 case X86ISD::VPERMILPD:
2840 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002841 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2842 }
2843
2844 return SDValue();
2845}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002846
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002847static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2848 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2849 switch(Opc) {
2850 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002851 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002852 case X86ISD::SHUFPD:
2853 case X86ISD::SHUFPS:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002854 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002855 return DAG.getNode(Opc, dl, VT, V1, V2,
2856 DAG.getConstant(TargetMask, MVT::i8));
2857 }
2858 return SDValue();
2859}
2860
2861static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2862 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2863 switch(Opc) {
2864 default: llvm_unreachable("Unknown x86 shuffle node");
2865 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002866 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002867 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002868 case X86ISD::MOVLPS:
2869 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002870 case X86ISD::MOVSS:
2871 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002872 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002873 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002874 case X86ISD::VUNPCKLPSY:
2875 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002876 case X86ISD::PUNPCKLWD:
2877 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002878 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002879 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002880 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002881 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002882 case X86ISD::VUNPCKHPSY:
2883 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002884 case X86ISD::PUNPCKHWD:
2885 case X86ISD::PUNPCKHBW:
2886 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002887 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002888 return DAG.getNode(Opc, dl, VT, V1, V2);
2889 }
2890 return SDValue();
2891}
2892
Dan Gohmand858e902010-04-17 15:26:15 +00002893SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002894 MachineFunction &MF = DAG.getMachineFunction();
2895 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2896 int ReturnAddrIndex = FuncInfo->getRAIndex();
2897
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002898 if (ReturnAddrIndex == 0) {
2899 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002900 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002901 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002902 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002903 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002904 }
2905
Evan Cheng25ab6902006-09-08 06:48:29 +00002906 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002907}
2908
2909
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002910bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2911 bool hasSymbolicDisplacement) {
2912 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002913 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002914 return false;
2915
2916 // If we don't have a symbolic displacement - we don't have any extra
2917 // restrictions.
2918 if (!hasSymbolicDisplacement)
2919 return true;
2920
2921 // FIXME: Some tweaks might be needed for medium code model.
2922 if (M != CodeModel::Small && M != CodeModel::Kernel)
2923 return false;
2924
2925 // For small code model we assume that latest object is 16MB before end of 31
2926 // bits boundary. We may also accept pretty large negative constants knowing
2927 // that all objects are in the positive half of address space.
2928 if (M == CodeModel::Small && Offset < 16*1024*1024)
2929 return true;
2930
2931 // For kernel code model we know that all object resist in the negative half
2932 // of 32bits address space. We may not accept negative offsets, since they may
2933 // be just off and we may accept pretty large positive ones.
2934 if (M == CodeModel::Kernel && Offset > 0)
2935 return true;
2936
2937 return false;
2938}
2939
Evan Chengef41ff62011-06-23 17:54:54 +00002940/// isCalleePop - Determines whether the callee is required to pop its
2941/// own arguments. Callee pop is necessary to support tail calls.
2942bool X86::isCalleePop(CallingConv::ID CallingConv,
2943 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
2944 if (IsVarArg)
2945 return false;
2946
2947 switch (CallingConv) {
2948 default:
2949 return false;
2950 case CallingConv::X86_StdCall:
2951 return !is64Bit;
2952 case CallingConv::X86_FastCall:
2953 return !is64Bit;
2954 case CallingConv::X86_ThisCall:
2955 return !is64Bit;
2956 case CallingConv::Fast:
2957 return TailCallOpt;
2958 case CallingConv::GHC:
2959 return TailCallOpt;
2960 }
2961}
2962
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002963/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2964/// specific condition code, returning the condition code and the LHS/RHS of the
2965/// comparison to make.
2966static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2967 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002968 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002969 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2970 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2971 // X > -1 -> X == 0, jump !sign.
2972 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002973 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002974 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2975 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002976 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002977 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002978 // X < 1 -> X <= 0
2979 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002980 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002981 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002982 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002983
Evan Chengd9558e02006-01-06 00:43:03 +00002984 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002985 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002986 case ISD::SETEQ: return X86::COND_E;
2987 case ISD::SETGT: return X86::COND_G;
2988 case ISD::SETGE: return X86::COND_GE;
2989 case ISD::SETLT: return X86::COND_L;
2990 case ISD::SETLE: return X86::COND_LE;
2991 case ISD::SETNE: return X86::COND_NE;
2992 case ISD::SETULT: return X86::COND_B;
2993 case ISD::SETUGT: return X86::COND_A;
2994 case ISD::SETULE: return X86::COND_BE;
2995 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002996 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002997 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002998
Chris Lattner4c78e022008-12-23 23:42:27 +00002999 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003000
Chris Lattner4c78e022008-12-23 23:42:27 +00003001 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003002 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3003 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003004 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3005 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003006 }
3007
Chris Lattner4c78e022008-12-23 23:42:27 +00003008 switch (SetCCOpcode) {
3009 default: break;
3010 case ISD::SETOLT:
3011 case ISD::SETOLE:
3012 case ISD::SETUGT:
3013 case ISD::SETUGE:
3014 std::swap(LHS, RHS);
3015 break;
3016 }
3017
3018 // On a floating point condition, the flags are set as follows:
3019 // ZF PF CF op
3020 // 0 | 0 | 0 | X > Y
3021 // 0 | 0 | 1 | X < Y
3022 // 1 | 0 | 0 | X == Y
3023 // 1 | 1 | 1 | unordered
3024 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003025 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003026 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003027 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003028 case ISD::SETOLT: // flipped
3029 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003030 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003031 case ISD::SETOLE: // flipped
3032 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003033 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003034 case ISD::SETUGT: // flipped
3035 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003036 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003037 case ISD::SETUGE: // flipped
3038 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003039 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003040 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003041 case ISD::SETNE: return X86::COND_NE;
3042 case ISD::SETUO: return X86::COND_P;
3043 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003044 case ISD::SETOEQ:
3045 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003046 }
Evan Chengd9558e02006-01-06 00:43:03 +00003047}
3048
Evan Cheng4a460802006-01-11 00:33:36 +00003049/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3050/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003051/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003052static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003053 switch (X86CC) {
3054 default:
3055 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003056 case X86::COND_B:
3057 case X86::COND_BE:
3058 case X86::COND_E:
3059 case X86::COND_P:
3060 case X86::COND_A:
3061 case X86::COND_AE:
3062 case X86::COND_NE:
3063 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003064 return true;
3065 }
3066}
3067
Evan Chengeb2f9692009-10-27 19:56:55 +00003068/// isFPImmLegal - Returns true if the target can instruction select the
3069/// specified FP immediate natively. If false, the legalizer will
3070/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003071bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003072 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3073 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3074 return true;
3075 }
3076 return false;
3077}
3078
Nate Begeman9008ca62009-04-27 18:41:29 +00003079/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3080/// the specified range (L, H].
3081static bool isUndefOrInRange(int Val, int Low, int Hi) {
3082 return (Val < 0) || (Val >= Low && Val < Hi);
3083}
3084
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003085/// isUndefOrInRange - Return true if every element in Mask, begining
3086/// from position Pos and ending in Pos+Size, falls within the specified
3087/// range (L, L+Pos]. or is undef.
3088static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3089 int Pos, int Size, int Low, int Hi) {
3090 for (int i = Pos, e = Pos+Size; i != e; ++i)
3091 if (!isUndefOrInRange(Mask[i], Low, Hi))
3092 return false;
3093 return true;
3094}
3095
Nate Begeman9008ca62009-04-27 18:41:29 +00003096/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3097/// specified value.
3098static bool isUndefOrEqual(int Val, int CmpVal) {
3099 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003100 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003101 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003102}
3103
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003104/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3105/// from position Pos and ending in Pos+Size, falls within the specified
3106/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003107static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3108 int Pos, int Size, int Low) {
3109 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3110 if (!isUndefOrEqual(Mask[i], Low))
3111 return false;
3112 return true;
3113}
3114
Nate Begeman9008ca62009-04-27 18:41:29 +00003115/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3116/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3117/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003118static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003119 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003120 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003121 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003122 return (Mask[0] < 2 && Mask[1] < 2);
3123 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003124}
3125
Nate Begeman9008ca62009-04-27 18:41:29 +00003126bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003127 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003128 N->getMask(M);
3129 return ::isPSHUFDMask(M, N->getValueType(0));
3130}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003131
Nate Begeman9008ca62009-04-27 18:41:29 +00003132/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3133/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003134static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003135 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003136 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003137
Nate Begeman9008ca62009-04-27 18:41:29 +00003138 // Lower quadword copied in order or undef.
3139 for (int i = 0; i != 4; ++i)
3140 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003141 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003142
Evan Cheng506d3df2006-03-29 23:07:14 +00003143 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003144 for (int i = 4; i != 8; ++i)
3145 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003146 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003147
Evan Cheng506d3df2006-03-29 23:07:14 +00003148 return true;
3149}
3150
Nate Begeman9008ca62009-04-27 18:41:29 +00003151bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003152 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003153 N->getMask(M);
3154 return ::isPSHUFHWMask(M, N->getValueType(0));
3155}
Evan Cheng506d3df2006-03-29 23:07:14 +00003156
Nate Begeman9008ca62009-04-27 18:41:29 +00003157/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3158/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003159static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003160 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003161 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003162
Rafael Espindola15684b22009-04-24 12:40:33 +00003163 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003164 for (int i = 4; i != 8; ++i)
3165 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003166 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003167
Rafael Espindola15684b22009-04-24 12:40:33 +00003168 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003169 for (int i = 0; i != 4; ++i)
3170 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003171 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003172
Rafael Espindola15684b22009-04-24 12:40:33 +00003173 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003174}
3175
Nate Begeman9008ca62009-04-27 18:41:29 +00003176bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003177 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003178 N->getMask(M);
3179 return ::isPSHUFLWMask(M, N->getValueType(0));
3180}
3181
Nate Begemana09008b2009-10-19 02:17:23 +00003182/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3183/// is suitable for input to PALIGNR.
3184static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003185 bool hasSSSE3OrAVX) {
Nate Begemana09008b2009-10-19 02:17:23 +00003186 int i, e = VT.getVectorNumElements();
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003187 if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
3188 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003189
Nate Begemana09008b2009-10-19 02:17:23 +00003190 // Do not handle v2i64 / v2f64 shuffles with palignr.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003191 if (e < 4 || !hasSSSE3OrAVX)
Nate Begemana09008b2009-10-19 02:17:23 +00003192 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003193
Nate Begemana09008b2009-10-19 02:17:23 +00003194 for (i = 0; i != e; ++i)
3195 if (Mask[i] >= 0)
3196 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003197
Nate Begemana09008b2009-10-19 02:17:23 +00003198 // All undef, not a palignr.
3199 if (i == e)
3200 return false;
3201
Eli Friedman63f8dde2011-07-25 21:36:45 +00003202 // Make sure we're shifting in the right direction.
3203 if (Mask[i] <= i)
3204 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003205
3206 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003207
Nate Begemana09008b2009-10-19 02:17:23 +00003208 // Check the rest of the elements to see if they are consecutive.
3209 for (++i; i != e; ++i) {
3210 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003211 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003212 return false;
3213 }
3214 return true;
3215}
3216
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003217/// isVSHUFPSYMask - Return true if the specified VECTOR_SHUFFLE operand
3218/// specifies a shuffle of elements that is suitable for input to 256-bit
3219/// VSHUFPSY.
3220static bool isVSHUFPSYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3221 const X86Subtarget *Subtarget) {
3222 int NumElems = VT.getVectorNumElements();
3223
3224 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3225 return false;
3226
3227 if (NumElems != 8)
3228 return false;
3229
3230 // VSHUFPSY divides the resulting vector into 4 chunks.
3231 // The sources are also splitted into 4 chunks, and each destination
3232 // chunk must come from a different source chunk.
3233 //
3234 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3235 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3236 //
3237 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3238 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3239 //
3240 int QuarterSize = NumElems/4;
3241 int HalfSize = QuarterSize*2;
3242 for (int i = 0; i < QuarterSize; ++i)
3243 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3244 return false;
3245 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3246 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3247 return false;
3248
3249 // The mask of the second half must be the same as the first but with
3250 // the appropriate offsets. This works in the same way as VPERMILPS
3251 // works with masks.
3252 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3253 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3254 return false;
3255 int FstHalfIdx = i-HalfSize;
3256 if (Mask[FstHalfIdx] < 0)
3257 continue;
3258 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3259 return false;
3260 }
3261 for (int i = QuarterSize*3; i < NumElems; ++i) {
3262 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3263 return false;
3264 int FstHalfIdx = i-HalfSize;
3265 if (Mask[FstHalfIdx] < 0)
3266 continue;
3267 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3268 return false;
3269
3270 }
3271
3272 return true;
3273}
3274
3275/// getShuffleVSHUFPSYImmediate - Return the appropriate immediate to shuffle
3276/// the specified VECTOR_MASK mask with VSHUFPSY instruction.
3277static unsigned getShuffleVSHUFPSYImmediate(SDNode *N) {
3278 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3279 EVT VT = SVOp->getValueType(0);
3280 int NumElems = VT.getVectorNumElements();
3281
3282 assert(NumElems == 8 && VT.getSizeInBits() == 256 &&
3283 "Only supports v8i32 and v8f32 types");
3284
3285 int HalfSize = NumElems/2;
3286 unsigned Mask = 0;
3287 for (int i = 0; i != NumElems ; ++i) {
3288 if (SVOp->getMaskElt(i) < 0)
3289 continue;
3290 // The mask of the first half must be equal to the second one.
3291 unsigned Shamt = (i%HalfSize)*2;
3292 unsigned Elt = SVOp->getMaskElt(i) % HalfSize;
3293 Mask |= Elt << Shamt;
3294 }
3295
3296 return Mask;
3297}
3298
3299/// isVSHUFPDYMask - Return true if the specified VECTOR_SHUFFLE operand
3300/// specifies a shuffle of elements that is suitable for input to 256-bit
3301/// VSHUFPDY. This shuffle doesn't have the same restriction as the PS
3302/// version and the mask of the second half isn't binded with the first
3303/// one.
3304static bool isVSHUFPDYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3305 const X86Subtarget *Subtarget) {
3306 int NumElems = VT.getVectorNumElements();
3307
3308 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3309 return false;
3310
3311 if (NumElems != 4)
3312 return false;
3313
3314 // VSHUFPSY divides the resulting vector into 4 chunks.
3315 // The sources are also splitted into 4 chunks, and each destination
3316 // chunk must come from a different source chunk.
3317 //
3318 // SRC1 => X3 X2 X1 X0
3319 // SRC2 => Y3 Y2 Y1 Y0
3320 //
3321 // DST => Y2..Y3, X2..X3, Y1..Y0, X1..X0
3322 //
3323 int QuarterSize = NumElems/4;
3324 int HalfSize = QuarterSize*2;
3325 for (int i = 0; i < QuarterSize; ++i)
3326 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3327 return false;
3328 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3329 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3330 return false;
3331 for (int i = QuarterSize*2; i < QuarterSize*3; ++i)
3332 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3333 return false;
3334 for (int i = QuarterSize*3; i < NumElems; ++i)
3335 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3336 return false;
3337
3338 return true;
3339}
3340
3341/// getShuffleVSHUFPDYImmediate - Return the appropriate immediate to shuffle
3342/// the specified VECTOR_MASK mask with VSHUFPDY instruction.
3343static unsigned getShuffleVSHUFPDYImmediate(SDNode *N) {
3344 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3345 EVT VT = SVOp->getValueType(0);
3346 int NumElems = VT.getVectorNumElements();
3347
3348 assert(NumElems == 4 && VT.getSizeInBits() == 256 &&
3349 "Only supports v4i64 and v4f64 types");
3350
3351 int HalfSize = NumElems/2;
3352 unsigned Mask = 0;
3353 for (int i = 0; i != NumElems ; ++i) {
3354 if (SVOp->getMaskElt(i) < 0)
3355 continue;
3356 int Elt = SVOp->getMaskElt(i) % HalfSize;
3357 Mask |= Elt << i;
3358 }
3359
3360 return Mask;
3361}
3362
Evan Cheng14aed5e2006-03-24 01:18:28 +00003363/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003364/// specifies a shuffle of elements that is suitable for input to 128-bit
3365/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003366static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003367 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003368
3369 if (VT.getSizeInBits() != 128)
3370 return false;
3371
Nate Begeman9008ca62009-04-27 18:41:29 +00003372 if (NumElems != 2 && NumElems != 4)
3373 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003374
Nate Begeman9008ca62009-04-27 18:41:29 +00003375 int Half = NumElems / 2;
3376 for (int i = 0; i < Half; ++i)
3377 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003378 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003379 for (int i = Half; i < NumElems; ++i)
3380 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003381 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003382
Evan Cheng14aed5e2006-03-24 01:18:28 +00003383 return true;
3384}
3385
Nate Begeman9008ca62009-04-27 18:41:29 +00003386bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3387 SmallVector<int, 8> M;
3388 N->getMask(M);
3389 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003390}
3391
Evan Cheng213d2cf2007-05-17 18:45:50 +00003392/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003393/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3394/// half elements to come from vector 1 (which would equal the dest.) and
3395/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003396static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003397 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003398
3399 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003400 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003401
Nate Begeman9008ca62009-04-27 18:41:29 +00003402 int Half = NumElems / 2;
3403 for (int i = 0; i < Half; ++i)
3404 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003405 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003406 for (int i = Half; i < NumElems; ++i)
3407 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003408 return false;
3409 return true;
3410}
3411
Nate Begeman9008ca62009-04-27 18:41:29 +00003412static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3413 SmallVector<int, 8> M;
3414 N->getMask(M);
3415 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003416}
3417
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003418/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3419/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003420bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003421 EVT VT = N->getValueType(0);
3422 unsigned NumElems = VT.getVectorNumElements();
3423
3424 if (VT.getSizeInBits() != 128)
3425 return false;
3426
3427 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003428 return false;
3429
Evan Cheng2064a2b2006-03-28 06:50:32 +00003430 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003431 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3432 isUndefOrEqual(N->getMaskElt(1), 7) &&
3433 isUndefOrEqual(N->getMaskElt(2), 2) &&
3434 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003435}
3436
Nate Begeman0b10b912009-11-07 23:17:15 +00003437/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3438/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3439/// <2, 3, 2, 3>
3440bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003441 EVT VT = N->getValueType(0);
3442 unsigned NumElems = VT.getVectorNumElements();
3443
3444 if (VT.getSizeInBits() != 128)
3445 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003446
Nate Begeman0b10b912009-11-07 23:17:15 +00003447 if (NumElems != 4)
3448 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003449
Nate Begeman0b10b912009-11-07 23:17:15 +00003450 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003451 isUndefOrEqual(N->getMaskElt(1), 3) &&
3452 isUndefOrEqual(N->getMaskElt(2), 2) &&
3453 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003454}
3455
Evan Cheng5ced1d82006-04-06 23:23:56 +00003456/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3457/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003458bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3459 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003460
Evan Cheng5ced1d82006-04-06 23:23:56 +00003461 if (NumElems != 2 && NumElems != 4)
3462 return false;
3463
Evan Chengc5cdff22006-04-07 21:53:05 +00003464 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003465 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003466 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003467
Evan Chengc5cdff22006-04-07 21:53:05 +00003468 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003469 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003470 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003471
3472 return true;
3473}
3474
Nate Begeman0b10b912009-11-07 23:17:15 +00003475/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3476/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3477bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003478 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003479
David Greenea20244d2011-03-02 17:23:43 +00003480 if ((NumElems != 2 && NumElems != 4)
3481 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003482 return false;
3483
Evan Chengc5cdff22006-04-07 21:53:05 +00003484 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003485 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003486 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003487
Nate Begeman9008ca62009-04-27 18:41:29 +00003488 for (unsigned i = 0; i < NumElems/2; ++i)
3489 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003490 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003491
3492 return true;
3493}
3494
Evan Cheng0038e592006-03-28 00:39:58 +00003495/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3496/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003497static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003498 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003499 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003500
3501 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3502 "Unsupported vector type for unpckh");
3503
3504 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng0038e592006-03-28 00:39:58 +00003505 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003506
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003507 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3508 // independently on 128-bit lanes.
3509 unsigned NumLanes = VT.getSizeInBits()/128;
3510 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003511
3512 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003513 unsigned End = NumLaneElts;
3514 for (unsigned s = 0; s < NumLanes; ++s) {
3515 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003516 i != End;
3517 i += 2, ++j) {
3518 int BitI = Mask[i];
3519 int BitI1 = Mask[i+1];
3520 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003521 return false;
David Greenea20244d2011-03-02 17:23:43 +00003522 if (V2IsSplat) {
3523 if (!isUndefOrEqual(BitI1, NumElts))
3524 return false;
3525 } else {
3526 if (!isUndefOrEqual(BitI1, j + NumElts))
3527 return false;
3528 }
Evan Cheng39623da2006-04-20 08:58:49 +00003529 }
David Greenea20244d2011-03-02 17:23:43 +00003530 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003531 Start += NumLaneElts;
3532 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003533 }
David Greenea20244d2011-03-02 17:23:43 +00003534
Evan Cheng0038e592006-03-28 00:39:58 +00003535 return true;
3536}
3537
Nate Begeman9008ca62009-04-27 18:41:29 +00003538bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3539 SmallVector<int, 8> M;
3540 N->getMask(M);
3541 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003542}
3543
Evan Cheng4fcb9222006-03-28 02:43:26 +00003544/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3545/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003546static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003547 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003548 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003549
3550 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3551 "Unsupported vector type for unpckh");
3552
3553 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003554 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003555
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003556 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3557 // independently on 128-bit lanes.
3558 unsigned NumLanes = VT.getSizeInBits()/128;
3559 unsigned NumLaneElts = NumElts/NumLanes;
3560
3561 unsigned Start = 0;
3562 unsigned End = NumLaneElts;
3563 for (unsigned l = 0; l != NumLanes; ++l) {
3564 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3565 i != End; i += 2, ++j) {
3566 int BitI = Mask[i];
3567 int BitI1 = Mask[i+1];
3568 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003569 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003570 if (V2IsSplat) {
3571 if (isUndefOrEqual(BitI1, NumElts))
3572 return false;
3573 } else {
3574 if (!isUndefOrEqual(BitI1, j+NumElts))
3575 return false;
3576 }
Evan Cheng39623da2006-04-20 08:58:49 +00003577 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003578 // Process the next 128 bits.
3579 Start += NumLaneElts;
3580 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003581 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003582 return true;
3583}
3584
Nate Begeman9008ca62009-04-27 18:41:29 +00003585bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3586 SmallVector<int, 8> M;
3587 N->getMask(M);
3588 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003589}
3590
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003591/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3592/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3593/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003594static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003595 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003596 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003597 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003598
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003599 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3600 // FIXME: Need a better way to get rid of this, there's no latency difference
3601 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3602 // the former later. We should also remove the "_undef" special mask.
3603 if (NumElems == 4 && VT.getSizeInBits() == 256)
3604 return false;
3605
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003606 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3607 // independently on 128-bit lanes.
3608 unsigned NumLanes = VT.getSizeInBits() / 128;
3609 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003610
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003611 for (unsigned s = 0; s < NumLanes; ++s) {
3612 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3613 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003614 i += 2, ++j) {
3615 int BitI = Mask[i];
3616 int BitI1 = Mask[i+1];
3617
3618 if (!isUndefOrEqual(BitI, j))
3619 return false;
3620 if (!isUndefOrEqual(BitI1, j))
3621 return false;
3622 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003623 }
David Greenea20244d2011-03-02 17:23:43 +00003624
Rafael Espindola15684b22009-04-24 12:40:33 +00003625 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003626}
3627
Nate Begeman9008ca62009-04-27 18:41:29 +00003628bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3629 SmallVector<int, 8> M;
3630 N->getMask(M);
3631 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3632}
3633
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003634/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3635/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3636/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003637static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003638 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003639 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3640 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003641
Nate Begeman9008ca62009-04-27 18:41:29 +00003642 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3643 int BitI = Mask[i];
3644 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003645 if (!isUndefOrEqual(BitI, j))
3646 return false;
3647 if (!isUndefOrEqual(BitI1, j))
3648 return false;
3649 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003650 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003651}
3652
Nate Begeman9008ca62009-04-27 18:41:29 +00003653bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3654 SmallVector<int, 8> M;
3655 N->getMask(M);
3656 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3657}
3658
Evan Cheng017dcc62006-04-21 01:05:10 +00003659/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3660/// specifies a shuffle of elements that is suitable for input to MOVSS,
3661/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003662static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003663 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003664 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003665
3666 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003667
Nate Begeman9008ca62009-04-27 18:41:29 +00003668 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003669 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003670
Nate Begeman9008ca62009-04-27 18:41:29 +00003671 for (int i = 1; i < NumElts; ++i)
3672 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003673 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003674
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003675 return true;
3676}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003677
Nate Begeman9008ca62009-04-27 18:41:29 +00003678bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3679 SmallVector<int, 8> M;
3680 N->getMask(M);
3681 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003682}
3683
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003684/// isVPERM2F128Mask - Match 256-bit shuffles where the elements are considered
3685/// as permutations between 128-bit chunks or halves. As an example: this
3686/// shuffle bellow:
3687/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3688/// The first half comes from the second half of V1 and the second half from the
3689/// the second half of V2.
3690static bool isVPERM2F128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3691 const X86Subtarget *Subtarget) {
3692 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3693 return false;
3694
3695 // The shuffle result is divided into half A and half B. In total the two
3696 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3697 // B must come from C, D, E or F.
3698 int HalfSize = VT.getVectorNumElements()/2;
3699 bool MatchA = false, MatchB = false;
3700
3701 // Check if A comes from one of C, D, E, F.
3702 for (int Half = 0; Half < 4; ++Half) {
3703 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3704 MatchA = true;
3705 break;
3706 }
3707 }
3708
3709 // Check if B comes from one of C, D, E, F.
3710 for (int Half = 0; Half < 4; ++Half) {
3711 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3712 MatchB = true;
3713 break;
3714 }
3715 }
3716
3717 return MatchA && MatchB;
3718}
3719
3720/// getShuffleVPERM2F128Immediate - Return the appropriate immediate to shuffle
3721/// the specified VECTOR_MASK mask with VPERM2F128 instructions.
3722static unsigned getShuffleVPERM2F128Immediate(SDNode *N) {
3723 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3724 EVT VT = SVOp->getValueType(0);
3725
3726 int HalfSize = VT.getVectorNumElements()/2;
3727
3728 int FstHalf = 0, SndHalf = 0;
3729 for (int i = 0; i < HalfSize; ++i) {
3730 if (SVOp->getMaskElt(i) > 0) {
3731 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3732 break;
3733 }
3734 }
3735 for (int i = HalfSize; i < HalfSize*2; ++i) {
3736 if (SVOp->getMaskElt(i) > 0) {
3737 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3738 break;
3739 }
3740 }
3741
3742 return (FstHalf | (SndHalf << 4));
3743}
3744
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003745/// isVPERMILPDMask - Return true if the specified VECTOR_SHUFFLE operand
3746/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3747/// Note that VPERMIL mask matching is different depending whether theunderlying
3748/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3749/// to the same elements of the low, but to the higher half of the source.
3750/// In VPERMILPD the two lanes could be shuffled independently of each other
3751/// with the same restriction that lanes can't be crossed.
3752static bool isVPERMILPDMask(const SmallVectorImpl<int> &Mask, EVT VT,
3753 const X86Subtarget *Subtarget) {
3754 int NumElts = VT.getVectorNumElements();
3755 int NumLanes = VT.getSizeInBits()/128;
3756
3757 if (!Subtarget->hasAVX())
3758 return false;
3759
Eli Friedmandca62d52011-10-10 22:28:47 +00003760 // Only match 256-bit with 64-bit types
3761 if (VT.getSizeInBits() != 256 || NumElts != 4)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003762 return false;
3763
3764 // The mask on the high lane is independent of the low. Both can match
3765 // any element in inside its own lane, but can't cross.
3766 int LaneSize = NumElts/NumLanes;
3767 for (int l = 0; l < NumLanes; ++l)
3768 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3769 int LaneStart = l*LaneSize;
3770 if (!isUndefOrInRange(Mask[i], LaneStart, LaneStart+LaneSize))
3771 return false;
3772 }
3773
3774 return true;
3775}
3776
3777/// isVPERMILPSMask - Return true if the specified VECTOR_SHUFFLE operand
3778/// specifies a shuffle of elements that is suitable for input to VPERMILPS*.
3779/// Note that VPERMIL mask matching is different depending whether theunderlying
3780/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3781/// to the same elements of the low, but to the higher half of the source.
3782/// In VPERMILPD the two lanes could be shuffled independently of each other
3783/// with the same restriction that lanes can't be crossed.
3784static bool isVPERMILPSMask(const SmallVectorImpl<int> &Mask, EVT VT,
3785 const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003786 unsigned NumElts = VT.getVectorNumElements();
3787 unsigned NumLanes = VT.getSizeInBits()/128;
3788
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003789 if (!Subtarget->hasAVX())
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003790 return false;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003791
Eli Friedmandca62d52011-10-10 22:28:47 +00003792 // Only match 256-bit with 32-bit types
3793 if (VT.getSizeInBits() != 256 || NumElts != 8)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003794 return false;
3795
3796 // The mask on the high lane should be the same as the low. Actually,
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003797 // they can differ if any of the corresponding index in a lane is undef
3798 // and the other stays in range.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003799 int LaneSize = NumElts/NumLanes;
3800 for (int i = 0; i < LaneSize; ++i) {
3801 int HighElt = i+LaneSize;
Bruno Cardoso Lopes155a92a2011-08-10 01:54:17 +00003802 bool HighValid = isUndefOrInRange(Mask[HighElt], LaneSize, NumElts);
3803 bool LowValid = isUndefOrInRange(Mask[i], 0, LaneSize);
3804
3805 if (!HighValid || !LowValid)
3806 return false;
3807 if (Mask[i] < 0 || Mask[HighElt] < 0)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003808 continue;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003809 if (Mask[HighElt]-Mask[i] != LaneSize)
3810 return false;
3811 }
3812
3813 return true;
3814}
3815
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003816/// getShuffleVPERMILPSImmediate - Return the appropriate immediate to shuffle
3817/// the specified VECTOR_MASK mask with VPERMILPS* instructions.
3818static unsigned getShuffleVPERMILPSImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003819 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3820 EVT VT = SVOp->getValueType(0);
3821
3822 int NumElts = VT.getVectorNumElements();
3823 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003824 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003825
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003826 // Although the mask is equal for both lanes do it twice to get the cases
3827 // where a mask will match because the same mask element is undef on the
3828 // first half but valid on the second. This would get pathological cases
3829 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003830 unsigned Mask = 0;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003831 for (int l = 0; l < NumLanes; ++l) {
3832 for (int i = 0; i < LaneSize; ++i) {
3833 int MaskElt = SVOp->getMaskElt(i+(l*LaneSize));
3834 if (MaskElt < 0)
3835 continue;
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003836 if (MaskElt >= LaneSize)
3837 MaskElt -= LaneSize;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003838 Mask |= MaskElt << (i*2);
3839 }
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003840 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003841
3842 return Mask;
3843}
3844
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003845/// getShuffleVPERMILPDImmediate - Return the appropriate immediate to shuffle
3846/// the specified VECTOR_MASK mask with VPERMILPD* instructions.
3847static unsigned getShuffleVPERMILPDImmediate(SDNode *N) {
3848 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3849 EVT VT = SVOp->getValueType(0);
3850
3851 int NumElts = VT.getVectorNumElements();
3852 int NumLanes = VT.getSizeInBits()/128;
3853
3854 unsigned Mask = 0;
3855 int LaneSize = NumElts/NumLanes;
3856 for (int l = 0; l < NumLanes; ++l)
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003857 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3858 int MaskElt = SVOp->getMaskElt(i);
3859 if (MaskElt < 0)
3860 continue;
3861 Mask |= (MaskElt-l*LaneSize) << i;
3862 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003863
3864 return Mask;
3865}
3866
Evan Cheng017dcc62006-04-21 01:05:10 +00003867/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3868/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003869/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003870static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003871 bool V2IsSplat = false, bool V2IsUndef = false) {
3872 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003873 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003874 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003875
Nate Begeman9008ca62009-04-27 18:41:29 +00003876 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003877 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003878
Nate Begeman9008ca62009-04-27 18:41:29 +00003879 for (int i = 1; i < NumOps; ++i)
3880 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3881 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3882 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003883 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003884
Evan Cheng39623da2006-04-20 08:58:49 +00003885 return true;
3886}
3887
Nate Begeman9008ca62009-04-27 18:41:29 +00003888static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003889 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003890 SmallVector<int, 8> M;
3891 N->getMask(M);
3892 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003893}
3894
Evan Chengd9539472006-04-14 21:59:03 +00003895/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3896/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003897/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3898bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3899 const X86Subtarget *Subtarget) {
3900 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003901 return false;
3902
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003903 // The second vector must be undef
3904 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3905 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003906
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003907 EVT VT = N->getValueType(0);
3908 unsigned NumElems = VT.getVectorNumElements();
3909
3910 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3911 (VT.getSizeInBits() == 256 && NumElems != 8))
3912 return false;
3913
3914 // "i+1" is the value the indexed mask element must have
3915 for (unsigned i = 0; i < NumElems; i += 2)
3916 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3917 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003918 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003919
3920 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003921}
3922
3923/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3924/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003925/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3926bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
3927 const X86Subtarget *Subtarget) {
3928 if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003929 return false;
3930
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003931 // The second vector must be undef
3932 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3933 return false;
3934
3935 EVT VT = N->getValueType(0);
3936 unsigned NumElems = VT.getVectorNumElements();
3937
3938 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3939 (VT.getSizeInBits() == 256 && NumElems != 8))
3940 return false;
3941
3942 // "i" is the value the indexed mask element must have
3943 for (unsigned i = 0; i < NumElems; i += 2)
3944 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
3945 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003946 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003947
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003948 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003949}
3950
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003951/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3952/// specifies a shuffle of elements that is suitable for input to 256-bit
3953/// version of MOVDDUP.
3954static bool isMOVDDUPYMask(ShuffleVectorSDNode *N,
3955 const X86Subtarget *Subtarget) {
3956 EVT VT = N->getValueType(0);
3957 int NumElts = VT.getVectorNumElements();
3958 bool V2IsUndef = N->getOperand(1).getOpcode() == ISD::UNDEF;
3959
3960 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256 ||
3961 !V2IsUndef || NumElts != 4)
3962 return false;
3963
3964 for (int i = 0; i != NumElts/2; ++i)
3965 if (!isUndefOrEqual(N->getMaskElt(i), 0))
3966 return false;
3967 for (int i = NumElts/2; i != NumElts; ++i)
3968 if (!isUndefOrEqual(N->getMaskElt(i), NumElts/2))
3969 return false;
3970 return true;
3971}
3972
Evan Cheng0b457f02008-09-25 20:50:48 +00003973/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003974/// specifies a shuffle of elements that is suitable for input to 128-bit
3975/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003976bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003977 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00003978
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003979 if (VT.getSizeInBits() != 128)
3980 return false;
3981
3982 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003983 for (int i = 0; i < e; ++i)
3984 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003985 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003986 for (int i = 0; i < e; ++i)
3987 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003988 return false;
3989 return true;
3990}
3991
David Greenec38a03e2011-02-03 15:50:00 +00003992/// isVEXTRACTF128Index - Return true if the specified
3993/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3994/// suitable for input to VEXTRACTF128.
3995bool X86::isVEXTRACTF128Index(SDNode *N) {
3996 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3997 return false;
3998
3999 // The index should be aligned on a 128-bit boundary.
4000 uint64_t Index =
4001 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4002
4003 unsigned VL = N->getValueType(0).getVectorNumElements();
4004 unsigned VBits = N->getValueType(0).getSizeInBits();
4005 unsigned ElSize = VBits / VL;
4006 bool Result = (Index * ElSize) % 128 == 0;
4007
4008 return Result;
4009}
4010
David Greeneccacdc12011-02-04 16:08:29 +00004011/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4012/// operand specifies a subvector insert that is suitable for input to
4013/// VINSERTF128.
4014bool X86::isVINSERTF128Index(SDNode *N) {
4015 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4016 return false;
4017
4018 // The index should be aligned on a 128-bit boundary.
4019 uint64_t Index =
4020 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4021
4022 unsigned VL = N->getValueType(0).getVectorNumElements();
4023 unsigned VBits = N->getValueType(0).getSizeInBits();
4024 unsigned ElSize = VBits / VL;
4025 bool Result = (Index * ElSize) % 128 == 0;
4026
4027 return Result;
4028}
4029
Evan Cheng63d33002006-03-22 08:01:21 +00004030/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004031/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00004032unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004033 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4034 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4035
Evan Chengb9df0ca2006-03-22 02:53:00 +00004036 unsigned Shift = (NumOperands == 4) ? 2 : 1;
4037 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00004038 for (int i = 0; i < NumOperands; ++i) {
4039 int Val = SVOp->getMaskElt(NumOperands-i-1);
4040 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00004041 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00004042 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00004043 if (i != NumOperands - 1)
4044 Mask <<= Shift;
4045 }
Evan Cheng63d33002006-03-22 08:01:21 +00004046 return Mask;
4047}
4048
Evan Cheng506d3df2006-03-29 23:07:14 +00004049/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004050/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004051unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004052 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004053 unsigned Mask = 0;
4054 // 8 nodes, but we only care about the last 4.
4055 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004056 int Val = SVOp->getMaskElt(i);
4057 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004058 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00004059 if (i != 4)
4060 Mask <<= 2;
4061 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004062 return Mask;
4063}
4064
4065/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004066/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004067unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004068 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004069 unsigned Mask = 0;
4070 // 8 nodes, but we only care about the first 4.
4071 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004072 int Val = SVOp->getMaskElt(i);
4073 if (Val >= 0)
4074 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004075 if (i != 0)
4076 Mask <<= 2;
4077 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004078 return Mask;
4079}
4080
Nate Begemana09008b2009-10-19 02:17:23 +00004081/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4082/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4083unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4084 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4085 EVT VVT = N->getValueType(0);
4086 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4087 int Val = 0;
4088
4089 unsigned i, e;
4090 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4091 Val = SVOp->getMaskElt(i);
4092 if (Val >= 0)
4093 break;
4094 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004095 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004096 return (Val - i) * EltSize;
4097}
4098
David Greenec38a03e2011-02-03 15:50:00 +00004099/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4100/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4101/// instructions.
4102unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4103 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4104 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4105
4106 uint64_t Index =
4107 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4108
4109 EVT VecVT = N->getOperand(0).getValueType();
4110 EVT ElVT = VecVT.getVectorElementType();
4111
4112 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004113 return Index / NumElemsPerChunk;
4114}
4115
David Greeneccacdc12011-02-04 16:08:29 +00004116/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4117/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4118/// instructions.
4119unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4120 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4121 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4122
4123 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004124 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004125
4126 EVT VecVT = N->getValueType(0);
4127 EVT ElVT = VecVT.getVectorElementType();
4128
4129 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004130 return Index / NumElemsPerChunk;
4131}
4132
Evan Cheng37b73872009-07-30 08:33:02 +00004133/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4134/// constant +0.0.
4135bool X86::isZeroNode(SDValue Elt) {
4136 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004137 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004138 (isa<ConstantFPSDNode>(Elt) &&
4139 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4140}
4141
Nate Begeman9008ca62009-04-27 18:41:29 +00004142/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4143/// their permute mask.
4144static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4145 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004146 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004147 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004148 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004149
Nate Begeman5a5ca152009-04-29 05:20:52 +00004150 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004151 int idx = SVOp->getMaskElt(i);
4152 if (idx < 0)
4153 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004154 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004155 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004156 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004157 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004158 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004159 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4160 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004161}
4162
Evan Cheng779ccea2007-12-07 21:30:01 +00004163/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
4164/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00004165static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004166 unsigned NumElems = VT.getVectorNumElements();
4167 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004168 int idx = Mask[i];
4169 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004170 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004171 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004172 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004173 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004174 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004175 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004176}
4177
Evan Cheng533a0aa2006-04-19 20:35:22 +00004178/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4179/// match movhlps. The lower half elements should come from upper half of
4180/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004181/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004182static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004183 EVT VT = Op->getValueType(0);
4184 if (VT.getSizeInBits() != 128)
4185 return false;
4186 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004187 return false;
4188 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004189 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004190 return false;
4191 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004192 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004193 return false;
4194 return true;
4195}
4196
Evan Cheng5ced1d82006-04-06 23:23:56 +00004197/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004198/// is promoted to a vector. It also returns the LoadSDNode by reference if
4199/// required.
4200static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004201 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4202 return false;
4203 N = N->getOperand(0).getNode();
4204 if (!ISD::isNON_EXTLoad(N))
4205 return false;
4206 if (LD)
4207 *LD = cast<LoadSDNode>(N);
4208 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004209}
4210
Evan Cheng533a0aa2006-04-19 20:35:22 +00004211/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4212/// match movlp{s|d}. The lower half elements should come from lower half of
4213/// V1 (and in order), and the upper half elements should come from the upper
4214/// half of V2 (and in order). And since V1 will become the source of the
4215/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004216static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4217 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004218 EVT VT = Op->getValueType(0);
4219 if (VT.getSizeInBits() != 128)
4220 return false;
4221
Evan Cheng466685d2006-10-09 20:57:25 +00004222 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004223 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004224 // Is V2 is a vector load, don't do this transformation. We will try to use
4225 // load folding shufps op.
4226 if (ISD::isNON_EXTLoad(V2))
4227 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004228
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004229 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004230
Evan Cheng533a0aa2006-04-19 20:35:22 +00004231 if (NumElems != 2 && NumElems != 4)
4232 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004233 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004234 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004235 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004236 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004237 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004238 return false;
4239 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004240}
4241
Evan Cheng39623da2006-04-20 08:58:49 +00004242/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4243/// all the same.
4244static bool isSplatVector(SDNode *N) {
4245 if (N->getOpcode() != ISD::BUILD_VECTOR)
4246 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004247
Dan Gohman475871a2008-07-27 21:46:04 +00004248 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004249 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4250 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004251 return false;
4252 return true;
4253}
4254
Evan Cheng213d2cf2007-05-17 18:45:50 +00004255/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004256/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004257/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004258static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004259 SDValue V1 = N->getOperand(0);
4260 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004261 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4262 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004263 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004264 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004265 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004266 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4267 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004268 if (Opc != ISD::BUILD_VECTOR ||
4269 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004270 return false;
4271 } else if (Idx >= 0) {
4272 unsigned Opc = V1.getOpcode();
4273 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4274 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004275 if (Opc != ISD::BUILD_VECTOR ||
4276 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004277 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004278 }
4279 }
4280 return true;
4281}
4282
4283/// getZeroVector - Returns a vector of specified type with all zero elements.
4284///
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004285static SDValue getZeroVector(EVT VT, bool HasXMMInt, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004286 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004287 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004288
Dale Johannesen0488fb62010-09-30 23:57:10 +00004289 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004290 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004291 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004292 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004293 if (HasXMMInt) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004294 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4295 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4296 } else { // SSE1
4297 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4298 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4299 }
4300 } else if (VT.getSizeInBits() == 256) { // AVX
4301 // 256-bit logic and arithmetic instructions in AVX are
4302 // all floating-point, no support for integer ops. Default
4303 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004304 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004305 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4306 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004307 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004308 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004309}
4310
Chris Lattner8a594482007-11-25 00:24:49 +00004311/// getOnesVector - Returns a vector of specified type with all bits set.
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004312/// Always build ones vectors as <4 x i32>. For 256-bit types, use two
4313/// <4 x i32> inserted in a <8 x i32> appropriately. Then bitcast to their
4314/// original type, ensuring they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00004315static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004316 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004317 assert((VT.is128BitVector() || VT.is256BitVector())
4318 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004319
Owen Anderson825b72b2009-08-11 20:47:22 +00004320 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004321 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4322 Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004323
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004324 if (VT.is256BitVector()) {
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004325 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4326 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4327 Vec = Insert128BitVector(InsV, Vec,
4328 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4329 }
4330
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004331 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004332}
4333
Evan Cheng39623da2006-04-20 08:58:49 +00004334/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4335/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004336static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004337 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004338 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004339
Evan Cheng39623da2006-04-20 08:58:49 +00004340 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004341 SmallVector<int, 8> MaskVec;
4342 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004343
Nate Begeman5a5ca152009-04-29 05:20:52 +00004344 for (unsigned i = 0; i != NumElems; ++i) {
4345 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004346 MaskVec[i] = NumElems;
4347 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004348 }
Evan Cheng39623da2006-04-20 08:58:49 +00004349 }
Evan Cheng39623da2006-04-20 08:58:49 +00004350 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004351 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4352 SVOp->getOperand(1), &MaskVec[0]);
4353 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004354}
4355
Evan Cheng017dcc62006-04-21 01:05:10 +00004356/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4357/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004358static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004359 SDValue V2) {
4360 unsigned NumElems = VT.getVectorNumElements();
4361 SmallVector<int, 8> Mask;
4362 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004363 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004364 Mask.push_back(i);
4365 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004366}
4367
Nate Begeman9008ca62009-04-27 18:41:29 +00004368/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004369static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004370 SDValue V2) {
4371 unsigned NumElems = VT.getVectorNumElements();
4372 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004373 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004374 Mask.push_back(i);
4375 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004376 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004377 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004378}
4379
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004380/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004381static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004382 SDValue V2) {
4383 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004384 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004385 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004386 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004387 Mask.push_back(i + Half);
4388 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004389 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004390 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004391}
4392
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004393// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004394// a generic shuffle instruction because the target has no such instructions.
4395// Generate shuffles which repeat i16 and i8 several times until they can be
4396// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004397static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004398 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004399 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004400 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004401
Nate Begeman9008ca62009-04-27 18:41:29 +00004402 while (NumElems > 4) {
4403 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004404 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004405 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004406 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004407 EltNo -= NumElems/2;
4408 }
4409 NumElems >>= 1;
4410 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004411 return V;
4412}
Eric Christopherfd179292009-08-27 18:07:15 +00004413
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004414/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4415static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4416 EVT VT = V.getValueType();
4417 DebugLoc dl = V.getDebugLoc();
4418 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4419 && "Vector size not supported");
4420
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004421 if (VT.getSizeInBits() == 128) {
4422 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004423 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004424 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4425 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004426 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004427 // To use VPERMILPS to splat scalars, the second half of indicies must
4428 // refer to the higher part, which is a duplication of the lower one,
4429 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004430 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4431 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004432
4433 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4434 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4435 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004436 }
4437
4438 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4439}
4440
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004441/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004442static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4443 EVT SrcVT = SV->getValueType(0);
4444 SDValue V1 = SV->getOperand(0);
4445 DebugLoc dl = SV->getDebugLoc();
4446
4447 int EltNo = SV->getSplatIndex();
4448 int NumElems = SrcVT.getVectorNumElements();
4449 unsigned Size = SrcVT.getSizeInBits();
4450
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004451 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4452 "Unknown how to promote splat for type");
4453
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004454 // Extract the 128-bit part containing the splat element and update
4455 // the splat element index when it refers to the higher register.
4456 if (Size == 256) {
4457 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4458 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4459 if (Idx > 0)
4460 EltNo -= NumElems/2;
4461 }
4462
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004463 // All i16 and i8 vector types can't be used directly by a generic shuffle
4464 // instruction because the target has no such instruction. Generate shuffles
4465 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004466 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004467 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004468 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004469 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004470
4471 // Recreate the 256-bit vector and place the same 128-bit vector
4472 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004473 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004474 if (Size == 256) {
4475 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4476 DAG.getConstant(0, MVT::i32), DAG, dl);
4477 V1 = Insert128BitVector(InsV, V1,
4478 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4479 }
4480
4481 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004482}
4483
Evan Chengba05f722006-04-21 23:03:30 +00004484/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004485/// vector of zero or undef vector. This produces a shuffle where the low
4486/// element of V2 is swizzled into the zero/undef vector, landing at element
4487/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Dan Gohman475871a2008-07-27 21:46:04 +00004488static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004489 bool isZero, bool HasXMMInt,
4490 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004491 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004492 SDValue V1 = isZero
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004493 ? getZeroVector(VT, HasXMMInt, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004494 unsigned NumElems = VT.getVectorNumElements();
4495 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004496 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004497 // If this is the insertion idx, put the low elt of V2 here.
4498 MaskVec.push_back(i == Idx ? NumElems : i);
4499 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004500}
4501
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004502/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4503/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004504static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4505 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004506 if (Depth == 6)
4507 return SDValue(); // Limit search depth.
4508
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004509 SDValue V = SDValue(N, 0);
4510 EVT VT = V.getValueType();
4511 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004512
4513 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4514 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4515 Index = SV->getMaskElt(Index);
4516
4517 if (Index < 0)
4518 return DAG.getUNDEF(VT.getVectorElementType());
4519
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004520 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004521 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004522 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004523 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004524
4525 // Recurse into target specific vector shuffles to find scalars.
4526 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004527 int NumElems = VT.getVectorNumElements();
4528 SmallVector<unsigned, 16> ShuffleMask;
4529 SDValue ImmN;
4530
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004531 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004532 case X86ISD::SHUFPS:
4533 case X86ISD::SHUFPD:
4534 ImmN = N->getOperand(N->getNumOperands()-1);
4535 DecodeSHUFPSMask(NumElems,
4536 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4537 ShuffleMask);
4538 break;
4539 case X86ISD::PUNPCKHBW:
4540 case X86ISD::PUNPCKHWD:
4541 case X86ISD::PUNPCKHDQ:
4542 case X86ISD::PUNPCKHQDQ:
4543 DecodePUNPCKHMask(NumElems, ShuffleMask);
4544 break;
4545 case X86ISD::UNPCKHPS:
4546 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00004547 case X86ISD::VUNPCKHPSY:
4548 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004549 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4550 break;
4551 case X86ISD::PUNPCKLBW:
4552 case X86ISD::PUNPCKLWD:
4553 case X86ISD::PUNPCKLDQ:
4554 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004555 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004556 break;
4557 case X86ISD::UNPCKLPS:
4558 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004559 case X86ISD::VUNPCKLPSY:
4560 case X86ISD::VUNPCKLPDY:
4561 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004562 break;
4563 case X86ISD::MOVHLPS:
4564 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4565 break;
4566 case X86ISD::MOVLHPS:
4567 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4568 break;
4569 case X86ISD::PSHUFD:
4570 ImmN = N->getOperand(N->getNumOperands()-1);
4571 DecodePSHUFMask(NumElems,
4572 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4573 ShuffleMask);
4574 break;
4575 case X86ISD::PSHUFHW:
4576 ImmN = N->getOperand(N->getNumOperands()-1);
4577 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4578 ShuffleMask);
4579 break;
4580 case X86ISD::PSHUFLW:
4581 ImmN = N->getOperand(N->getNumOperands()-1);
4582 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4583 ShuffleMask);
4584 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004585 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004586 case X86ISD::MOVSD: {
4587 // The index 0 always comes from the first element of the second source,
4588 // this is why MOVSS and MOVSD are used in the first place. The other
4589 // elements come from the other positions of the first source vector.
4590 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004591 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4592 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004593 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004594 case X86ISD::VPERMILPS:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004595 ImmN = N->getOperand(N->getNumOperands()-1);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004596 DecodeVPERMILPSMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004597 ShuffleMask);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004598 break;
4599 case X86ISD::VPERMILPSY:
4600 ImmN = N->getOperand(N->getNumOperands()-1);
4601 DecodeVPERMILPSMask(8, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4602 ShuffleMask);
4603 break;
4604 case X86ISD::VPERMILPD:
4605 ImmN = N->getOperand(N->getNumOperands()-1);
4606 DecodeVPERMILPDMask(2, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4607 ShuffleMask);
4608 break;
4609 case X86ISD::VPERMILPDY:
4610 ImmN = N->getOperand(N->getNumOperands()-1);
4611 DecodeVPERMILPDMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4612 ShuffleMask);
4613 break;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004614 case X86ISD::VPERM2F128:
4615 ImmN = N->getOperand(N->getNumOperands()-1);
4616 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4617 ShuffleMask);
4618 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004619 case X86ISD::MOVDDUP:
4620 case X86ISD::MOVLHPD:
4621 case X86ISD::MOVLPD:
4622 case X86ISD::MOVLPS:
4623 case X86ISD::MOVSHDUP:
4624 case X86ISD::MOVSLDUP:
4625 case X86ISD::PALIGN:
4626 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004627 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004628 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004629 return SDValue();
4630 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004631
4632 Index = ShuffleMask[Index];
4633 if (Index < 0)
4634 return DAG.getUNDEF(VT.getVectorElementType());
4635
4636 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4637 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4638 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004639 }
4640
4641 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004642 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004643 V = V.getOperand(0);
4644 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004645 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004646
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004647 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004648 return SDValue();
4649 }
4650
4651 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4652 return (Index == 0) ? V.getOperand(0)
4653 : DAG.getUNDEF(VT.getVectorElementType());
4654
4655 if (V.getOpcode() == ISD::BUILD_VECTOR)
4656 return V.getOperand(Index);
4657
4658 return SDValue();
4659}
4660
4661/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4662/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004663/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004664static
4665unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4666 bool ZerosFromLeft, SelectionDAG &DAG) {
4667 int i = 0;
4668
4669 while (i < NumElems) {
4670 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004671 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004672 if (!(Elt.getNode() &&
4673 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4674 break;
4675 ++i;
4676 }
4677
4678 return i;
4679}
4680
4681/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4682/// MaskE correspond consecutively to elements from one of the vector operands,
4683/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4684static
4685bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4686 int OpIdx, int NumElems, unsigned &OpNum) {
4687 bool SeenV1 = false;
4688 bool SeenV2 = false;
4689
4690 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4691 int Idx = SVOp->getMaskElt(i);
4692 // Ignore undef indicies
4693 if (Idx < 0)
4694 continue;
4695
4696 if (Idx < NumElems)
4697 SeenV1 = true;
4698 else
4699 SeenV2 = true;
4700
4701 // Only accept consecutive elements from the same vector
4702 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4703 return false;
4704 }
4705
4706 OpNum = SeenV1 ? 0 : 1;
4707 return true;
4708}
4709
4710/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4711/// logical left shift of a vector.
4712static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4713 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4714 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4715 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4716 false /* check zeros from right */, DAG);
4717 unsigned OpSrc;
4718
4719 if (!NumZeros)
4720 return false;
4721
4722 // Considering the elements in the mask that are not consecutive zeros,
4723 // check if they consecutively come from only one of the source vectors.
4724 //
4725 // V1 = {X, A, B, C} 0
4726 // \ \ \ /
4727 // vector_shuffle V1, V2 <1, 2, 3, X>
4728 //
4729 if (!isShuffleMaskConsecutive(SVOp,
4730 0, // Mask Start Index
4731 NumElems-NumZeros-1, // Mask End Index
4732 NumZeros, // Where to start looking in the src vector
4733 NumElems, // Number of elements in vector
4734 OpSrc)) // Which source operand ?
4735 return false;
4736
4737 isLeft = false;
4738 ShAmt = NumZeros;
4739 ShVal = SVOp->getOperand(OpSrc);
4740 return true;
4741}
4742
4743/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4744/// logical left shift of a vector.
4745static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4746 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4747 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4748 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4749 true /* check zeros from left */, DAG);
4750 unsigned OpSrc;
4751
4752 if (!NumZeros)
4753 return false;
4754
4755 // Considering the elements in the mask that are not consecutive zeros,
4756 // check if they consecutively come from only one of the source vectors.
4757 //
4758 // 0 { A, B, X, X } = V2
4759 // / \ / /
4760 // vector_shuffle V1, V2 <X, X, 4, 5>
4761 //
4762 if (!isShuffleMaskConsecutive(SVOp,
4763 NumZeros, // Mask Start Index
4764 NumElems-1, // Mask End Index
4765 0, // Where to start looking in the src vector
4766 NumElems, // Number of elements in vector
4767 OpSrc)) // Which source operand ?
4768 return false;
4769
4770 isLeft = true;
4771 ShAmt = NumZeros;
4772 ShVal = SVOp->getOperand(OpSrc);
4773 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004774}
4775
4776/// isVectorShift - Returns true if the shuffle can be implemented as a
4777/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004778static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004779 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004780 // Although the logic below support any bitwidth size, there are no
4781 // shift instructions which handle more than 128-bit vectors.
4782 if (SVOp->getValueType(0).getSizeInBits() > 128)
4783 return false;
4784
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004785 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4786 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4787 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004788
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004789 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004790}
4791
Evan Chengc78d3b42006-04-24 18:01:45 +00004792/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4793///
Dan Gohman475871a2008-07-27 21:46:04 +00004794static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004795 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004796 SelectionDAG &DAG,
4797 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004798 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004799 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004800
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004801 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004802 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004803 bool First = true;
4804 for (unsigned i = 0; i < 16; ++i) {
4805 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4806 if (ThisIsNonZero && First) {
4807 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004808 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004809 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004810 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004811 First = false;
4812 }
4813
4814 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004815 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004816 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4817 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004818 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004819 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004820 }
4821 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004822 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4823 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4824 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004825 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004826 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004827 } else
4828 ThisElt = LastElt;
4829
Gabor Greifba36cb52008-08-28 21:40:38 +00004830 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004831 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004832 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004833 }
4834 }
4835
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004836 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004837}
4838
Bill Wendlinga348c562007-03-22 18:42:45 +00004839/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004840///
Dan Gohman475871a2008-07-27 21:46:04 +00004841static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004842 unsigned NumNonZero, unsigned NumZero,
4843 SelectionDAG &DAG,
4844 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004845 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004846 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004847
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004848 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004849 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004850 bool First = true;
4851 for (unsigned i = 0; i < 8; ++i) {
4852 bool isNonZero = (NonZeros & (1 << i)) != 0;
4853 if (isNonZero) {
4854 if (First) {
4855 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004856 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004857 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004858 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004859 First = false;
4860 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004861 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004862 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004863 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004864 }
4865 }
4866
4867 return V;
4868}
4869
Evan Chengf26ffe92008-05-29 08:22:04 +00004870/// getVShift - Return a vector logical shift node.
4871///
Owen Andersone50ed302009-08-10 22:56:29 +00004872static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004873 unsigned NumBits, SelectionDAG &DAG,
4874 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004875 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004876 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004877 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004878 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4879 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004880 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004881 DAG.getConstant(NumBits,
4882 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004883}
4884
Dan Gohman475871a2008-07-27 21:46:04 +00004885SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004886X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004887 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004888
Evan Chengc3630942009-12-09 21:00:30 +00004889 // Check if the scalar load can be widened into a vector load. And if
4890 // the address is "base + cst" see if the cst can be "absorbed" into
4891 // the shuffle mask.
4892 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4893 SDValue Ptr = LD->getBasePtr();
4894 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4895 return SDValue();
4896 EVT PVT = LD->getValueType(0);
4897 if (PVT != MVT::i32 && PVT != MVT::f32)
4898 return SDValue();
4899
4900 int FI = -1;
4901 int64_t Offset = 0;
4902 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4903 FI = FINode->getIndex();
4904 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004905 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004906 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4907 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4908 Offset = Ptr.getConstantOperandVal(1);
4909 Ptr = Ptr.getOperand(0);
4910 } else {
4911 return SDValue();
4912 }
4913
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004914 // FIXME: 256-bit vector instructions don't require a strict alignment,
4915 // improve this code to support it better.
4916 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004917 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004918 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004919 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004920 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004921 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004922 // Can't change the alignment. FIXME: It's possible to compute
4923 // the exact stack offset and reference FI + adjust offset instead.
4924 // If someone *really* cares about this. That's the way to implement it.
4925 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004926 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004927 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004928 }
4929 }
4930
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004931 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004932 // Ptr + (Offset & ~15).
4933 if (Offset < 0)
4934 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004935 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004936 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004937 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004938 if (StartOffset)
4939 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4940 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4941
4942 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004943 int NumElems = VT.getVectorNumElements();
4944
4945 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
4946 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4947 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004948 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004949 false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004950
4951 // Canonicalize it to a v4i32 or v8i32 shuffle.
4952 SmallVector<int, 8> Mask;
4953 for (int i = 0; i < NumElems; ++i)
4954 Mask.push_back(EltNo);
4955
4956 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
4957 return DAG.getNode(ISD::BITCAST, dl, NVT,
4958 DAG.getVectorShuffle(CanonVT, dl, V1,
4959 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004960 }
4961
4962 return SDValue();
4963}
4964
Michael J. Spencerec38de22010-10-10 22:04:20 +00004965/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4966/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004967/// load which has the same value as a build_vector whose operands are 'elts'.
4968///
4969/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004970///
Nate Begeman1449f292010-03-24 22:19:06 +00004971/// FIXME: we'd also like to handle the case where the last elements are zero
4972/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4973/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004974static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004975 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004976 EVT EltVT = VT.getVectorElementType();
4977 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004978
Nate Begemanfdea31a2010-03-24 20:49:50 +00004979 LoadSDNode *LDBase = NULL;
4980 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004981
Nate Begeman1449f292010-03-24 22:19:06 +00004982 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004983 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004984 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004985 for (unsigned i = 0; i < NumElems; ++i) {
4986 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004987
Nate Begemanfdea31a2010-03-24 20:49:50 +00004988 if (!Elt.getNode() ||
4989 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4990 return SDValue();
4991 if (!LDBase) {
4992 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4993 return SDValue();
4994 LDBase = cast<LoadSDNode>(Elt.getNode());
4995 LastLoadedElt = i;
4996 continue;
4997 }
4998 if (Elt.getOpcode() == ISD::UNDEF)
4999 continue;
5000
5001 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5002 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5003 return SDValue();
5004 LastLoadedElt = i;
5005 }
Nate Begeman1449f292010-03-24 22:19:06 +00005006
5007 // If we have found an entire vector of loads and undefs, then return a large
5008 // load of the entire vector width starting at the base pointer. If we found
5009 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005010 if (LastLoadedElt == NumElems - 1) {
5011 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005012 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005013 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005014 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005015 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005016 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005017 LDBase->isVolatile(), LDBase->isNonTemporal(),
5018 LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00005019 } else if (NumElems == 4 && LastLoadedElt == 1 &&
5020 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005021 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5022 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005023 SDValue ResNode =
5024 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5025 LDBase->getPointerInfo(),
5026 LDBase->getAlignment(),
5027 false/*isVolatile*/, true/*ReadMem*/,
5028 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005029 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005030 }
5031 return SDValue();
5032}
5033
Evan Chengc3630942009-12-09 21:00:30 +00005034SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005035X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005036 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005037
David Greenef125a292011-02-08 19:04:41 +00005038 EVT VT = Op.getValueType();
5039 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005040 unsigned NumElems = Op.getNumOperands();
5041
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005042 // Vectors containing all zeros can be matched by pxor and xorps later
5043 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5044 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5045 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005046 if (Op.getValueType() == MVT::v4i32 ||
5047 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005048 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005049
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005050 return getZeroVector(Op.getValueType(), Subtarget->hasXMMInt(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005051 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005052
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005053 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
5054 // vectors or broken into v4i32 operations on 256-bit vectors.
5055 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
5056 if (Op.getValueType() == MVT::v4i32)
5057 return Op;
5058
5059 return getOnesVector(Op.getValueType(), DAG, dl);
5060 }
5061
Owen Andersone50ed302009-08-10 22:56:29 +00005062 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005063
Evan Cheng0db9fe62006-04-25 20:13:52 +00005064 unsigned NumZero = 0;
5065 unsigned NumNonZero = 0;
5066 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005067 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005068 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005069 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005070 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005071 if (Elt.getOpcode() == ISD::UNDEF)
5072 continue;
5073 Values.insert(Elt);
5074 if (Elt.getOpcode() != ISD::Constant &&
5075 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005076 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005077 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005078 NumZero++;
5079 else {
5080 NonZeros |= (1 << i);
5081 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005082 }
5083 }
5084
Chris Lattner97a2a562010-08-26 05:24:29 +00005085 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5086 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005087 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005088
Chris Lattner67f453a2008-03-09 05:42:06 +00005089 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005090 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005091 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005092 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005093
Chris Lattner62098042008-03-09 01:05:04 +00005094 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5095 // the value are obviously zero, truncate the value to i32 and do the
5096 // insertion that way. Only do this if the value is non-constant or if the
5097 // value is a constant being inserted into element 0. It is cheaper to do
5098 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005099 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005100 (!IsAllConstants || Idx == 0)) {
5101 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005102 // Handle SSE only.
5103 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5104 EVT VecVT = MVT::v4i32;
5105 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005106
Chris Lattner62098042008-03-09 01:05:04 +00005107 // Truncate the value (which may itself be a constant) to i32, and
5108 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005109 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005110 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005111 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005112 Subtarget->hasXMMInt(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005113
Chris Lattner62098042008-03-09 01:05:04 +00005114 // Now we have our 32-bit value zero extended in the low element of
5115 // a vector. If Idx != 0, swizzle it into place.
5116 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005117 SmallVector<int, 4> Mask;
5118 Mask.push_back(Idx);
5119 for (unsigned i = 1; i != VecElts; ++i)
5120 Mask.push_back(i);
5121 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005122 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005123 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005124 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005125 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005126 }
5127 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005128
Chris Lattner19f79692008-03-08 22:59:52 +00005129 // If we have a constant or non-constant insertion into the low element of
5130 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5131 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005132 // depending on what the source datatype is.
5133 if (Idx == 0) {
5134 if (NumZero == 0) {
5135 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005136 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5137 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005138 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5139 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005140 return getShuffleVectorZeroOrUndef(Item, 0, true,Subtarget->hasXMMInt(),
Eli Friedman10415532009-06-06 06:05:10 +00005141 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005142 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5143 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005144 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5145 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005146 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5147 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005148 Subtarget->hasXMMInt(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005149 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005150 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005151 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005152
5153 // Is it a vector logical left shift?
5154 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005155 X86::isZeroNode(Op.getOperand(0)) &&
5156 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005157 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005158 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005159 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005160 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005161 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005162 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005163
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005164 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005165 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005166
Chris Lattner19f79692008-03-08 22:59:52 +00005167 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5168 // is a non-constant being inserted into an element other than the low one,
5169 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5170 // movd/movss) to move this into the low element, then shuffle it into
5171 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005172 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005173 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005174
Evan Cheng0db9fe62006-04-25 20:13:52 +00005175 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005176 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005177 Subtarget->hasXMMInt(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005178 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005179 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005180 MaskVec.push_back(i == Idx ? 0 : 1);
5181 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005182 }
5183 }
5184
Chris Lattner67f453a2008-03-09 05:42:06 +00005185 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005186 if (Values.size() == 1) {
5187 if (EVTBits == 32) {
5188 // Instead of a shuffle like this:
5189 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5190 // Check if it's possible to issue this instead.
5191 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5192 unsigned Idx = CountTrailingZeros_32(NonZeros);
5193 SDValue Item = Op.getOperand(Idx);
5194 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5195 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5196 }
Dan Gohman475871a2008-07-27 21:46:04 +00005197 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005198 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005199
Dan Gohmana3941172007-07-24 22:55:08 +00005200 // A vector full of immediates; various special cases are already
5201 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005202 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005203 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005204
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005205 // For AVX-length vectors, build the individual 128-bit pieces and use
5206 // shuffles to put them in place.
5207 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5208 SmallVector<SDValue, 32> V;
5209 for (unsigned i = 0; i < NumElems; ++i)
5210 V.push_back(Op.getOperand(i));
5211
5212 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5213
5214 // Build both the lower and upper subvector.
5215 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5216 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5217 NumElems/2);
5218
5219 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005220 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5221 DAG.getConstant(0, MVT::i32), DAG, dl);
5222 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005223 DAG, dl);
5224 }
5225
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005226 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005227 if (EVTBits == 64) {
5228 if (NumNonZero == 1) {
5229 // One half is zero or undef.
5230 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005231 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005232 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005233 return getShuffleVectorZeroOrUndef(V2, Idx, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005234 Subtarget->hasXMMInt(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005235 }
Dan Gohman475871a2008-07-27 21:46:04 +00005236 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005237 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005238
5239 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005240 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005241 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005242 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005243 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005244 }
5245
Bill Wendling826f36f2007-03-28 00:57:11 +00005246 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005247 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005248 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005249 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005250 }
5251
5252 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005253 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005254 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005255 if (NumElems == 4 && NumZero > 0) {
5256 for (unsigned i = 0; i < 4; ++i) {
5257 bool isZero = !(NonZeros & (1 << i));
5258 if (isZero)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005259 V[i] = getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005260 else
Dale Johannesenace16102009-02-03 19:33:06 +00005261 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005262 }
5263
5264 for (unsigned i = 0; i < 2; ++i) {
5265 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5266 default: break;
5267 case 0:
5268 V[i] = V[i*2]; // Must be a zero vector.
5269 break;
5270 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005271 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005272 break;
5273 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005274 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005275 break;
5276 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005277 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005278 break;
5279 }
5280 }
5281
Nate Begeman9008ca62009-04-27 18:41:29 +00005282 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005283 bool Reverse = (NonZeros & 0x3) == 2;
5284 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005285 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005286 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5287 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005288 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5289 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005290 }
5291
Nate Begemanfdea31a2010-03-24 20:49:50 +00005292 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5293 // Check for a build vector of consecutive loads.
5294 for (unsigned i = 0; i < NumElems; ++i)
5295 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005296
Nate Begemanfdea31a2010-03-24 20:49:50 +00005297 // Check for elements which are consecutive loads.
5298 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5299 if (LD.getNode())
5300 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005301
5302 // For SSE 4.1, use insertps to put the high elements into the low element.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005303 if (getSubtarget()->hasSSE41() || getSubtarget()->hasAVX()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005304 SDValue Result;
5305 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5306 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5307 else
5308 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005309
Chris Lattner24faf612010-08-28 17:59:08 +00005310 for (unsigned i = 1; i < NumElems; ++i) {
5311 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5312 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005313 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005314 }
5315 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005316 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005317
Chris Lattner6e80e442010-08-28 17:15:43 +00005318 // Otherwise, expand into a number of unpckl*, start by extending each of
5319 // our (non-undef) elements to the full vector width with the element in the
5320 // bottom slot of the vector (which generates no code for SSE).
5321 for (unsigned i = 0; i < NumElems; ++i) {
5322 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5323 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5324 else
5325 V[i] = DAG.getUNDEF(VT);
5326 }
5327
5328 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005329 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5330 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5331 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005332 unsigned EltStride = NumElems >> 1;
5333 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005334 for (unsigned i = 0; i < EltStride; ++i) {
5335 // If V[i+EltStride] is undef and this is the first round of mixing,
5336 // then it is safe to just drop this shuffle: V[i] is already in the
5337 // right place, the one element (since it's the first round) being
5338 // inserted as undef can be dropped. This isn't safe for successive
5339 // rounds because they will permute elements within both vectors.
5340 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5341 EltStride == NumElems/2)
5342 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005343
Chris Lattner6e80e442010-08-28 17:15:43 +00005344 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005345 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005346 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005347 }
5348 return V[0];
5349 }
Dan Gohman475871a2008-07-27 21:46:04 +00005350 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005351}
5352
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005353// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5354// them in a MMX register. This is better than doing a stack convert.
5355static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005356 DebugLoc dl = Op.getDebugLoc();
5357 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005358
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005359 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5360 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5361 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005362 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005363 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5364 InVec = Op.getOperand(1);
5365 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5366 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005367 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005368 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5369 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5370 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005371 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005372 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5373 Mask[0] = 0; Mask[1] = 2;
5374 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5375 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005376 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005377}
5378
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005379// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5380// to create 256-bit vectors from two other 128-bit ones.
5381static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5382 DebugLoc dl = Op.getDebugLoc();
5383 EVT ResVT = Op.getValueType();
5384
5385 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5386
5387 SDValue V1 = Op.getOperand(0);
5388 SDValue V2 = Op.getOperand(1);
5389 unsigned NumElems = ResVT.getVectorNumElements();
5390
5391 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5392 DAG.getConstant(0, MVT::i32), DAG, dl);
5393 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5394 DAG, dl);
5395}
5396
5397SDValue
5398X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005399 EVT ResVT = Op.getValueType();
5400
5401 assert(Op.getNumOperands() == 2);
5402 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5403 "Unsupported CONCAT_VECTORS for value type");
5404
5405 // We support concatenate two MMX registers and place them in a MMX register.
5406 // This is better than doing a stack convert.
5407 if (ResVT.is128BitVector())
5408 return LowerMMXCONCAT_VECTORS(Op, DAG);
5409
5410 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5411 // from two other 128-bit ones.
5412 return LowerAVXCONCAT_VECTORS(Op, DAG);
5413}
5414
Nate Begemanb9a47b82009-02-23 08:49:38 +00005415// v8i16 shuffles - Prefer shuffles in the following order:
5416// 1. [all] pshuflw, pshufhw, optional move
5417// 2. [ssse3] 1 x pshufb
5418// 3. [ssse3] 2 x pshufb + 1 x por
5419// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005420SDValue
5421X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5422 SelectionDAG &DAG) const {
5423 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005424 SDValue V1 = SVOp->getOperand(0);
5425 SDValue V2 = SVOp->getOperand(1);
5426 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005427 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005428
Nate Begemanb9a47b82009-02-23 08:49:38 +00005429 // Determine if more than 1 of the words in each of the low and high quadwords
5430 // of the result come from the same quadword of one of the two inputs. Undef
5431 // mask values count as coming from any quadword, for better codegen.
5432 SmallVector<unsigned, 4> LoQuad(4);
5433 SmallVector<unsigned, 4> HiQuad(4);
5434 BitVector InputQuads(4);
5435 for (unsigned i = 0; i < 8; ++i) {
5436 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005437 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005438 MaskVals.push_back(EltIdx);
5439 if (EltIdx < 0) {
5440 ++Quad[0];
5441 ++Quad[1];
5442 ++Quad[2];
5443 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005444 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005445 }
5446 ++Quad[EltIdx / 4];
5447 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005448 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005449
Nate Begemanb9a47b82009-02-23 08:49:38 +00005450 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005451 unsigned MaxQuad = 1;
5452 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005453 if (LoQuad[i] > MaxQuad) {
5454 BestLoQuad = i;
5455 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005456 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005457 }
5458
Nate Begemanb9a47b82009-02-23 08:49:38 +00005459 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005460 MaxQuad = 1;
5461 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005462 if (HiQuad[i] > MaxQuad) {
5463 BestHiQuad = i;
5464 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005465 }
5466 }
5467
Nate Begemanb9a47b82009-02-23 08:49:38 +00005468 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005469 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005470 // single pshufb instruction is necessary. If There are more than 2 input
5471 // quads, disable the next transformation since it does not help SSSE3.
5472 bool V1Used = InputQuads[0] || InputQuads[1];
5473 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005474 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005475 if (InputQuads.count() == 2 && V1Used && V2Used) {
5476 BestLoQuad = InputQuads.find_first();
5477 BestHiQuad = InputQuads.find_next(BestLoQuad);
5478 }
5479 if (InputQuads.count() > 2) {
5480 BestLoQuad = -1;
5481 BestHiQuad = -1;
5482 }
5483 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005484
Nate Begemanb9a47b82009-02-23 08:49:38 +00005485 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5486 // the shuffle mask. If a quad is scored as -1, that means that it contains
5487 // words from all 4 input quadwords.
5488 SDValue NewV;
5489 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005490 SmallVector<int, 8> MaskV;
5491 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5492 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005493 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005494 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5495 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5496 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005497
Nate Begemanb9a47b82009-02-23 08:49:38 +00005498 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5499 // source words for the shuffle, to aid later transformations.
5500 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005501 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005502 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005503 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005504 if (idx != (int)i)
5505 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005506 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005507 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005508 AllWordsInNewV = false;
5509 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005510 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005511
Nate Begemanb9a47b82009-02-23 08:49:38 +00005512 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5513 if (AllWordsInNewV) {
5514 for (int i = 0; i != 8; ++i) {
5515 int idx = MaskVals[i];
5516 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005517 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005518 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005519 if ((idx != i) && idx < 4)
5520 pshufhw = false;
5521 if ((idx != i) && idx > 3)
5522 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005523 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005524 V1 = NewV;
5525 V2Used = false;
5526 BestLoQuad = 0;
5527 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005528 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005529
Nate Begemanb9a47b82009-02-23 08:49:38 +00005530 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5531 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005532 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005533 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5534 unsigned TargetMask = 0;
5535 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005536 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005537 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5538 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5539 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005540 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005541 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005542 }
Eric Christopherfd179292009-08-27 18:07:15 +00005543
Nate Begemanb9a47b82009-02-23 08:49:38 +00005544 // If we have SSSE3, and all words of the result are from 1 input vector,
5545 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5546 // is present, fall back to case 4.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005547 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005548 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005549
Nate Begemanb9a47b82009-02-23 08:49:38 +00005550 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005551 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005552 // mask, and elements that come from V1 in the V2 mask, so that the two
5553 // results can be OR'd together.
5554 bool TwoInputs = V1Used && V2Used;
5555 for (unsigned i = 0; i != 8; ++i) {
5556 int EltIdx = MaskVals[i] * 2;
5557 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005558 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5559 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005560 continue;
5561 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005562 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5563 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005564 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005565 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005566 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005567 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005568 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005569 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005570 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005571
Nate Begemanb9a47b82009-02-23 08:49:38 +00005572 // Calculate the shuffle mask for the second input, shuffle it, and
5573 // OR it with the first shuffled input.
5574 pshufbMask.clear();
5575 for (unsigned i = 0; i != 8; ++i) {
5576 int EltIdx = MaskVals[i] * 2;
5577 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005578 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5579 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005580 continue;
5581 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005582 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5583 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005584 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005585 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005586 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005587 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005588 MVT::v16i8, &pshufbMask[0], 16));
5589 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005590 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005591 }
5592
5593 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5594 // and update MaskVals with new element order.
5595 BitVector InOrder(8);
5596 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005597 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005598 for (int i = 0; i != 4; ++i) {
5599 int idx = MaskVals[i];
5600 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005601 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005602 InOrder.set(i);
5603 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005604 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005605 InOrder.set(i);
5606 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005607 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005608 }
5609 }
5610 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005611 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005612 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005613 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005614
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005615 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5616 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005617 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5618 NewV.getOperand(0),
5619 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5620 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005621 }
Eric Christopherfd179292009-08-27 18:07:15 +00005622
Nate Begemanb9a47b82009-02-23 08:49:38 +00005623 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5624 // and update MaskVals with the new element order.
5625 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005626 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005627 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005628 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005629 for (unsigned i = 4; i != 8; ++i) {
5630 int idx = MaskVals[i];
5631 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005632 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005633 InOrder.set(i);
5634 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005635 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005636 InOrder.set(i);
5637 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005638 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005639 }
5640 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005641 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005642 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005643
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005644 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
5645 (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005646 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5647 NewV.getOperand(0),
5648 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5649 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005650 }
Eric Christopherfd179292009-08-27 18:07:15 +00005651
Nate Begemanb9a47b82009-02-23 08:49:38 +00005652 // In case BestHi & BestLo were both -1, which means each quadword has a word
5653 // from each of the four input quadwords, calculate the InOrder bitvector now
5654 // before falling through to the insert/extract cleanup.
5655 if (BestLoQuad == -1 && BestHiQuad == -1) {
5656 NewV = V1;
5657 for (int i = 0; i != 8; ++i)
5658 if (MaskVals[i] < 0 || MaskVals[i] == i)
5659 InOrder.set(i);
5660 }
Eric Christopherfd179292009-08-27 18:07:15 +00005661
Nate Begemanb9a47b82009-02-23 08:49:38 +00005662 // The other elements are put in the right place using pextrw and pinsrw.
5663 for (unsigned i = 0; i != 8; ++i) {
5664 if (InOrder[i])
5665 continue;
5666 int EltIdx = MaskVals[i];
5667 if (EltIdx < 0)
5668 continue;
5669 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005670 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005671 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005672 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005673 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005674 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005675 DAG.getIntPtrConstant(i));
5676 }
5677 return NewV;
5678}
5679
5680// v16i8 shuffles - Prefer shuffles in the following order:
5681// 1. [ssse3] 1 x pshufb
5682// 2. [ssse3] 2 x pshufb + 1 x por
5683// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5684static
Nate Begeman9008ca62009-04-27 18:41:29 +00005685SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005686 SelectionDAG &DAG,
5687 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005688 SDValue V1 = SVOp->getOperand(0);
5689 SDValue V2 = SVOp->getOperand(1);
5690 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005691 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005692 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005693
Nate Begemanb9a47b82009-02-23 08:49:38 +00005694 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005695 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005696 // present, fall back to case 3.
5697 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5698 bool V1Only = true;
5699 bool V2Only = true;
5700 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005701 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005702 if (EltIdx < 0)
5703 continue;
5704 if (EltIdx < 16)
5705 V2Only = false;
5706 else
5707 V1Only = false;
5708 }
Eric Christopherfd179292009-08-27 18:07:15 +00005709
Nate Begemanb9a47b82009-02-23 08:49:38 +00005710 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005711 if (TLI.getSubtarget()->hasSSSE3() || TLI.getSubtarget()->hasAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005712 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005713
Nate Begemanb9a47b82009-02-23 08:49:38 +00005714 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005715 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005716 //
5717 // Otherwise, we have elements from both input vectors, and must zero out
5718 // elements that come from V2 in the first mask, and V1 in the second mask
5719 // so that we can OR them together.
5720 bool TwoInputs = !(V1Only || V2Only);
5721 for (unsigned i = 0; i != 16; ++i) {
5722 int EltIdx = MaskVals[i];
5723 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005724 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005725 continue;
5726 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005727 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005728 }
5729 // If all the elements are from V2, assign it to V1 and return after
5730 // building the first pshufb.
5731 if (V2Only)
5732 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005733 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005734 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005735 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005736 if (!TwoInputs)
5737 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005738
Nate Begemanb9a47b82009-02-23 08:49:38 +00005739 // Calculate the shuffle mask for the second input, shuffle it, and
5740 // OR it with the first shuffled input.
5741 pshufbMask.clear();
5742 for (unsigned i = 0; i != 16; ++i) {
5743 int EltIdx = MaskVals[i];
5744 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005745 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005746 continue;
5747 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005748 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005749 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005750 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005751 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005752 MVT::v16i8, &pshufbMask[0], 16));
5753 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005754 }
Eric Christopherfd179292009-08-27 18:07:15 +00005755
Nate Begemanb9a47b82009-02-23 08:49:38 +00005756 // No SSSE3 - Calculate in place words and then fix all out of place words
5757 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5758 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005759 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5760 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005761 SDValue NewV = V2Only ? V2 : V1;
5762 for (int i = 0; i != 8; ++i) {
5763 int Elt0 = MaskVals[i*2];
5764 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005765
Nate Begemanb9a47b82009-02-23 08:49:38 +00005766 // This word of the result is all undef, skip it.
5767 if (Elt0 < 0 && Elt1 < 0)
5768 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005769
Nate Begemanb9a47b82009-02-23 08:49:38 +00005770 // This word of the result is already in the correct place, skip it.
5771 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5772 continue;
5773 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5774 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005775
Nate Begemanb9a47b82009-02-23 08:49:38 +00005776 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5777 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5778 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005779
5780 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5781 // using a single extract together, load it and store it.
5782 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005783 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005784 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005785 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005786 DAG.getIntPtrConstant(i));
5787 continue;
5788 }
5789
Nate Begemanb9a47b82009-02-23 08:49:38 +00005790 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005791 // source byte is not also odd, shift the extracted word left 8 bits
5792 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005793 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005794 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005795 DAG.getIntPtrConstant(Elt1 / 2));
5796 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005797 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005798 DAG.getConstant(8,
5799 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005800 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005801 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5802 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005803 }
5804 // If Elt0 is defined, extract it from the appropriate source. If the
5805 // source byte is not also even, shift the extracted word right 8 bits. If
5806 // Elt1 was also defined, OR the extracted values together before
5807 // inserting them in the result.
5808 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005809 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005810 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5811 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005812 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005813 DAG.getConstant(8,
5814 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005815 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005816 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5817 DAG.getConstant(0x00FF, MVT::i16));
5818 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005819 : InsElt0;
5820 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005821 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005822 DAG.getIntPtrConstant(i));
5823 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005824 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005825}
5826
Evan Cheng7a831ce2007-12-15 03:00:47 +00005827/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005828/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005829/// done when every pair / quad of shuffle mask elements point to elements in
5830/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005831/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005832static
Nate Begeman9008ca62009-04-27 18:41:29 +00005833SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005834 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005835 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005836 SDValue V1 = SVOp->getOperand(0);
5837 SDValue V2 = SVOp->getOperand(1);
5838 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005839 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005840 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005841 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005842 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005843 case MVT::v4f32: NewVT = MVT::v2f64; break;
5844 case MVT::v4i32: NewVT = MVT::v2i64; break;
5845 case MVT::v8i16: NewVT = MVT::v4i32; break;
5846 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005847 }
5848
Nate Begeman9008ca62009-04-27 18:41:29 +00005849 int Scale = NumElems / NewWidth;
5850 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005851 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005852 int StartIdx = -1;
5853 for (int j = 0; j < Scale; ++j) {
5854 int EltIdx = SVOp->getMaskElt(i+j);
5855 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005856 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005857 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005858 StartIdx = EltIdx - (EltIdx % Scale);
5859 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005860 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005861 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005862 if (StartIdx == -1)
5863 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005864 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005865 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005866 }
5867
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005868 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5869 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005870 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005871}
5872
Evan Chengd880b972008-05-09 21:53:03 +00005873/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005874///
Owen Andersone50ed302009-08-10 22:56:29 +00005875static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005876 SDValue SrcOp, SelectionDAG &DAG,
5877 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005878 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005879 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005880 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005881 LD = dyn_cast<LoadSDNode>(SrcOp);
5882 if (!LD) {
5883 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5884 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005885 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005886 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005887 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005888 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005889 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005890 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005891 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005892 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005893 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5894 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5895 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005896 SrcOp.getOperand(0)
5897 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005898 }
5899 }
5900 }
5901
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005902 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005903 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005904 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005905 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005906}
5907
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005908/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
5909/// shuffle node referes to only one lane in the sources.
5910static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
5911 EVT VT = SVOp->getValueType(0);
5912 int NumElems = VT.getVectorNumElements();
5913 int HalfSize = NumElems/2;
5914 SmallVector<int, 16> M;
5915 SVOp->getMask(M);
5916 bool MatchA = false, MatchB = false;
5917
5918 for (int l = 0; l < NumElems*2; l += HalfSize) {
5919 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
5920 MatchA = true;
5921 break;
5922 }
5923 }
5924
5925 for (int l = 0; l < NumElems*2; l += HalfSize) {
5926 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
5927 MatchB = true;
5928 break;
5929 }
5930 }
5931
5932 return MatchA && MatchB;
5933}
5934
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005935/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
5936/// which could not be matched by any known target speficic shuffle
5937static SDValue
5938LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00005939 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
5940 // If each half of a vector shuffle node referes to only one lane in the
5941 // source vectors, extract each used 128-bit lane and shuffle them using
5942 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
5943 // the work to the legalizer.
5944 DebugLoc dl = SVOp->getDebugLoc();
5945 EVT VT = SVOp->getValueType(0);
5946 int NumElems = VT.getVectorNumElements();
5947 int HalfSize = NumElems/2;
5948
5949 // Extract the reference for each half
5950 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
5951 int FstVecOpNum = 0, SndVecOpNum = 0;
5952 for (int i = 0; i < HalfSize; ++i) {
5953 int Elt = SVOp->getMaskElt(i);
5954 if (SVOp->getMaskElt(i) < 0)
5955 continue;
5956 FstVecOpNum = Elt/NumElems;
5957 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5958 break;
5959 }
5960 for (int i = HalfSize; i < NumElems; ++i) {
5961 int Elt = SVOp->getMaskElt(i);
5962 if (SVOp->getMaskElt(i) < 0)
5963 continue;
5964 SndVecOpNum = Elt/NumElems;
5965 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
5966 break;
5967 }
5968
5969 // Extract the subvectors
5970 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
5971 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
5972 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
5973 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
5974
5975 // Generate 128-bit shuffles
5976 SmallVector<int, 16> MaskV1, MaskV2;
5977 for (int i = 0; i < HalfSize; ++i) {
5978 int Elt = SVOp->getMaskElt(i);
5979 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5980 }
5981 for (int i = HalfSize; i < NumElems; ++i) {
5982 int Elt = SVOp->getMaskElt(i);
5983 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
5984 }
5985
5986 EVT NVT = V1.getValueType();
5987 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
5988 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
5989
5990 // Concatenate the result back
5991 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
5992 DAG.getConstant(0, MVT::i32), DAG, dl);
5993 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5994 DAG, dl);
5995 }
5996
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00005997 return SDValue();
5998}
5999
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006000/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6001/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006002static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006003LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006004 SDValue V1 = SVOp->getOperand(0);
6005 SDValue V2 = SVOp->getOperand(1);
6006 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006007 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006008
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006009 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
6010
Evan Chengace3c172008-07-22 21:13:36 +00006011 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00006012 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006013 SmallVector<int, 8> Mask1(4U, -1);
6014 SmallVector<int, 8> PermMask;
6015 SVOp->getMask(PermMask);
6016
Evan Chengace3c172008-07-22 21:13:36 +00006017 unsigned NumHi = 0;
6018 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006019 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006020 int Idx = PermMask[i];
6021 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006022 Locs[i] = std::make_pair(-1, -1);
6023 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006024 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6025 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006026 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006027 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006028 NumLo++;
6029 } else {
6030 Locs[i] = std::make_pair(1, NumHi);
6031 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006032 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006033 NumHi++;
6034 }
6035 }
6036 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006037
Evan Chengace3c172008-07-22 21:13:36 +00006038 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006039 // If no more than two elements come from either vector. This can be
6040 // implemented with two shuffles. First shuffle gather the elements.
6041 // The second shuffle, which takes the first shuffle as both of its
6042 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006043 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006044
Nate Begeman9008ca62009-04-27 18:41:29 +00006045 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006046
Evan Chengace3c172008-07-22 21:13:36 +00006047 for (unsigned i = 0; i != 4; ++i) {
6048 if (Locs[i].first == -1)
6049 continue;
6050 else {
6051 unsigned Idx = (i < 2) ? 0 : 4;
6052 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006053 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006054 }
6055 }
6056
Nate Begeman9008ca62009-04-27 18:41:29 +00006057 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006058 } else if (NumLo == 3 || NumHi == 3) {
6059 // Otherwise, we must have three elements from one vector, call it X, and
6060 // one element from the other, call it Y. First, use a shufps to build an
6061 // intermediate vector with the one element from Y and the element from X
6062 // that will be in the same half in the final destination (the indexes don't
6063 // matter). Then, use a shufps to build the final vector, taking the half
6064 // containing the element from Y from the intermediate, and the other half
6065 // from X.
6066 if (NumHi == 3) {
6067 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00006068 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006069 std::swap(V1, V2);
6070 }
6071
6072 // Find the element from V2.
6073 unsigned HiIndex;
6074 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006075 int Val = PermMask[HiIndex];
6076 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006077 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006078 if (Val >= 4)
6079 break;
6080 }
6081
Nate Begeman9008ca62009-04-27 18:41:29 +00006082 Mask1[0] = PermMask[HiIndex];
6083 Mask1[1] = -1;
6084 Mask1[2] = PermMask[HiIndex^1];
6085 Mask1[3] = -1;
6086 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006087
6088 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006089 Mask1[0] = PermMask[0];
6090 Mask1[1] = PermMask[1];
6091 Mask1[2] = HiIndex & 1 ? 6 : 4;
6092 Mask1[3] = HiIndex & 1 ? 4 : 6;
6093 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006094 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006095 Mask1[0] = HiIndex & 1 ? 2 : 0;
6096 Mask1[1] = HiIndex & 1 ? 0 : 2;
6097 Mask1[2] = PermMask[2];
6098 Mask1[3] = PermMask[3];
6099 if (Mask1[2] >= 0)
6100 Mask1[2] += 4;
6101 if (Mask1[3] >= 0)
6102 Mask1[3] += 4;
6103 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006104 }
Evan Chengace3c172008-07-22 21:13:36 +00006105 }
6106
6107 // Break it into (shuffle shuffle_hi, shuffle_lo).
6108 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006109 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006110 SmallVector<int,8> LoMask(4U, -1);
6111 SmallVector<int,8> HiMask(4U, -1);
6112
6113 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006114 unsigned MaskIdx = 0;
6115 unsigned LoIdx = 0;
6116 unsigned HiIdx = 2;
6117 for (unsigned i = 0; i != 4; ++i) {
6118 if (i == 2) {
6119 MaskPtr = &HiMask;
6120 MaskIdx = 1;
6121 LoIdx = 0;
6122 HiIdx = 2;
6123 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006124 int Idx = PermMask[i];
6125 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006126 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006127 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006128 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006129 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006130 LoIdx++;
6131 } else {
6132 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006133 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006134 HiIdx++;
6135 }
6136 }
6137
Nate Begeman9008ca62009-04-27 18:41:29 +00006138 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6139 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6140 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006141 for (unsigned i = 0; i != 4; ++i) {
6142 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006143 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006144 } else {
6145 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006146 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006147 }
6148 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006149 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006150}
6151
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006152static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006153 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006154 V = V.getOperand(0);
6155 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6156 V = V.getOperand(0);
6157 if (MayFoldLoad(V))
6158 return true;
6159 return false;
6160}
6161
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006162// FIXME: the version above should always be used. Since there's
6163// a bug where several vector shuffles can't be folded because the
6164// DAG is not updated during lowering and a node claims to have two
6165// uses while it only has one, use this version, and let isel match
6166// another instruction if the load really happens to have more than
6167// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006168// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006169static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006170 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006171 V = V.getOperand(0);
6172 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6173 V = V.getOperand(0);
6174 if (ISD::isNormalLoad(V.getNode()))
6175 return true;
6176 return false;
6177}
6178
6179/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6180/// a vector extract, and if both can be later optimized into a single load.
6181/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6182/// here because otherwise a target specific shuffle node is going to be
6183/// emitted for this shuffle, and the optimization not done.
6184/// FIXME: This is probably not the best approach, but fix the problem
6185/// until the right path is decided.
6186static
6187bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6188 const TargetLowering &TLI) {
6189 EVT VT = V.getValueType();
6190 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6191
6192 // Be sure that the vector shuffle is present in a pattern like this:
6193 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6194 if (!V.hasOneUse())
6195 return false;
6196
6197 SDNode *N = *V.getNode()->use_begin();
6198 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6199 return false;
6200
6201 SDValue EltNo = N->getOperand(1);
6202 if (!isa<ConstantSDNode>(EltNo))
6203 return false;
6204
6205 // If the bit convert changed the number of elements, it is unsafe
6206 // to examine the mask.
6207 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006208 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006209 EVT SrcVT = V.getOperand(0).getValueType();
6210 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6211 return false;
6212 V = V.getOperand(0);
6213 HasShuffleIntoBitcast = true;
6214 }
6215
6216 // Select the input vector, guarding against out of range extract vector.
6217 unsigned NumElems = VT.getVectorNumElements();
6218 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6219 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6220 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6221
6222 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006223 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006224 V = V.getOperand(0);
6225
6226 if (ISD::isNormalLoad(V.getNode())) {
6227 // Is the original load suitable?
6228 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6229
6230 // FIXME: avoid the multi-use bug that is preventing lots of
6231 // of foldings to be detected, this is still wrong of course, but
6232 // give the temporary desired behavior, and if it happens that
6233 // the load has real more uses, during isel it will not fold, and
6234 // will generate poor code.
6235 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6236 return false;
6237
6238 if (!HasShuffleIntoBitcast)
6239 return true;
6240
6241 // If there's a bitcast before the shuffle, check if the load type and
6242 // alignment is valid.
6243 unsigned Align = LN0->getAlignment();
6244 unsigned NewAlign =
6245 TLI.getTargetData()->getABITypeAlignment(
6246 VT.getTypeForEVT(*DAG.getContext()));
6247
6248 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6249 return false;
6250 }
6251
6252 return true;
6253}
6254
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006255static
Evan Cheng835580f2010-10-07 20:50:20 +00006256SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6257 EVT VT = Op.getValueType();
6258
6259 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006260 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6261 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006262 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6263 V1, DAG));
6264}
6265
6266static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006267SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006268 bool HasXMMInt) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006269 SDValue V1 = Op.getOperand(0);
6270 SDValue V2 = Op.getOperand(1);
6271 EVT VT = Op.getValueType();
6272
6273 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6274
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006275 if (HasXMMInt && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006276 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6277
Evan Cheng0899f5c2011-08-31 02:05:24 +00006278 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6279 return DAG.getNode(ISD::BITCAST, dl, VT,
6280 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6281 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6282 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006283}
6284
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006285static
6286SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6287 SDValue V1 = Op.getOperand(0);
6288 SDValue V2 = Op.getOperand(1);
6289 EVT VT = Op.getValueType();
6290
6291 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6292 "unsupported shuffle type");
6293
6294 if (V2.getOpcode() == ISD::UNDEF)
6295 V2 = V1;
6296
6297 // v4i32 or v4f32
6298 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6299}
6300
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006301static inline unsigned getSHUFPOpcode(EVT VT) {
6302 switch(VT.getSimpleVT().SimpleTy) {
6303 case MVT::v8i32: // Use fp unit for int unpack.
6304 case MVT::v8f32:
6305 case MVT::v4i32: // Use fp unit for int unpack.
6306 case MVT::v4f32: return X86ISD::SHUFPS;
6307 case MVT::v4i64: // Use fp unit for int unpack.
6308 case MVT::v4f64:
6309 case MVT::v2i64: // Use fp unit for int unpack.
6310 case MVT::v2f64: return X86ISD::SHUFPD;
6311 default:
6312 llvm_unreachable("Unknown type for shufp*");
6313 }
6314 return 0;
6315}
6316
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006317static
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006318SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasXMMInt) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006319 SDValue V1 = Op.getOperand(0);
6320 SDValue V2 = Op.getOperand(1);
6321 EVT VT = Op.getValueType();
6322 unsigned NumElems = VT.getVectorNumElements();
6323
6324 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6325 // operand of these instructions is only memory, so check if there's a
6326 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6327 // same masks.
6328 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006329
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006330 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006331 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006332 CanFoldLoad = true;
6333
6334 // When V1 is a load, it can be folded later into a store in isel, example:
6335 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6336 // turns into:
6337 // (MOVLPSmr addr:$src1, VR128:$src2)
6338 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006339 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006340 CanFoldLoad = true;
6341
Eric Christopher893a8822011-02-20 05:04:42 +00006342 // Both of them can't be memory operations though.
6343 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
6344 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00006345
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006346 if (CanFoldLoad) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006347 if (HasXMMInt && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006348 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6349
6350 if (NumElems == 4)
6351 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
6352 }
6353
6354 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6355 // movl and movlp will both match v2i64, but v2i64 is never matched by
6356 // movl earlier because we make it strict to avoid messing with the movlp load
6357 // folding logic (see the code above getMOVLP call). Match it here then,
6358 // this is horrible, but will stay like this until we move all shuffle
6359 // matching to x86 specific nodes. Note that for the 1st condition all
6360 // types are matched with movsd.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006361 if (HasXMMInt) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006362 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6363 // as to remove this logic from here, as much as possible
6364 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006365 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006366 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006367 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006368
6369 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6370
6371 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006372 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006373 X86::getShuffleSHUFImmediate(SVOp), DAG);
6374}
6375
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006376static inline unsigned getUNPCKLOpcode(EVT VT) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006377 switch(VT.getSimpleVT().SimpleTy) {
6378 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
6379 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006380 case MVT::v4f32: return X86ISD::UNPCKLPS;
6381 case MVT::v2f64: return X86ISD::UNPCKLPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006382 case MVT::v8i32: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006383 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006384 case MVT::v4i64: // Use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006385 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006386 case MVT::v16i8: return X86ISD::PUNPCKLBW;
6387 case MVT::v8i16: return X86ISD::PUNPCKLWD;
6388 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006389 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006390 }
6391 return 0;
6392}
6393
6394static inline unsigned getUNPCKHOpcode(EVT VT) {
6395 switch(VT.getSimpleVT().SimpleTy) {
6396 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
6397 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
6398 case MVT::v4f32: return X86ISD::UNPCKHPS;
6399 case MVT::v2f64: return X86ISD::UNPCKHPD;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006400 case MVT::v8i32: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006401 case MVT::v8f32: return X86ISD::VUNPCKHPSY;
Bruno Cardoso Lopescde4a1a2011-08-09 22:18:37 +00006402 case MVT::v4i64: // Use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006403 case MVT::v4f64: return X86ISD::VUNPCKHPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006404 case MVT::v16i8: return X86ISD::PUNPCKHBW;
6405 case MVT::v8i16: return X86ISD::PUNPCKHWD;
6406 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006407 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006408 }
6409 return 0;
6410}
6411
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006412static inline unsigned getVPERMILOpcode(EVT VT) {
6413 switch(VT.getSimpleVT().SimpleTy) {
6414 case MVT::v4i32:
6415 case MVT::v4f32: return X86ISD::VPERMILPS;
6416 case MVT::v2i64:
6417 case MVT::v2f64: return X86ISD::VPERMILPD;
6418 case MVT::v8i32:
6419 case MVT::v8f32: return X86ISD::VPERMILPSY;
6420 case MVT::v4i64:
6421 case MVT::v4f64: return X86ISD::VPERMILPDY;
6422 default:
6423 llvm_unreachable("Unknown type for vpermil");
6424 }
6425 return 0;
6426}
6427
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006428/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
6429/// a vbroadcast node. The nodes are suitable whenever we can fold a load coming
6430/// from a 32 or 64 bit scalar. Update Op to the desired load to be folded.
6431static bool isVectorBroadcast(SDValue &Op) {
6432 EVT VT = Op.getValueType();
6433 bool Is256 = VT.getSizeInBits() == 256;
6434
6435 assert((VT.getSizeInBits() == 128 || Is256) &&
6436 "Unsupported type for vbroadcast node");
6437
6438 SDValue V = Op;
6439 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
6440 V = V.getOperand(0);
6441
6442 if (Is256 && !(V.hasOneUse() &&
6443 V.getOpcode() == ISD::INSERT_SUBVECTOR &&
6444 V.getOperand(0).getOpcode() == ISD::UNDEF))
6445 return false;
6446
6447 if (Is256)
6448 V = V.getOperand(1);
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006449
6450 if (!V.hasOneUse())
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006451 return false;
6452
6453 // Check the source scalar_to_vector type. 256-bit broadcasts are
6454 // supported for 32/64-bit sizes, while 128-bit ones are only supported
6455 // for 32-bit scalars.
Bruno Cardoso Lopesa39ccdb2011-09-01 18:15:06 +00006456 if (V.getOpcode() != ISD::SCALAR_TO_VECTOR)
6457 return false;
6458
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006459 unsigned ScalarSize = V.getOperand(0).getValueType().getSizeInBits();
6460 if (ScalarSize != 32 && ScalarSize != 64)
6461 return false;
6462 if (!Is256 && ScalarSize == 64)
6463 return false;
6464
6465 V = V.getOperand(0);
6466 if (!MayFoldLoad(V))
6467 return false;
6468
6469 // Return the load node
6470 Op = V;
6471 return true;
6472}
6473
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006474static
6475SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006476 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006477 const X86Subtarget *Subtarget) {
6478 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6479 EVT VT = Op.getValueType();
6480 DebugLoc dl = Op.getDebugLoc();
6481 SDValue V1 = Op.getOperand(0);
6482 SDValue V2 = Op.getOperand(1);
6483
6484 if (isZeroShuffle(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006485 return getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006486
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006487 // Handle splat operations
6488 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006489 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006490 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006491 // Special case, this is the only place now where it's allowed to return
6492 // a vector_shuffle operation without using a target specific node, because
6493 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6494 // this be moved to DAGCombine instead?
6495 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006496 return Op;
6497
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006498 // Use vbroadcast whenever the splat comes from a foldable load
6499 if (Subtarget->hasAVX() && isVectorBroadcast(V1))
6500 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, V1);
6501
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006502 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006503 if ((Size == 128 && NumElem <= 4) ||
6504 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006505 return SDValue();
6506
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006507 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006508 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006509 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006510
6511 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6512 // do it!
6513 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6514 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6515 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006516 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006517 } else if ((VT == MVT::v4i32 ||
6518 (VT == MVT::v4f32 && Subtarget->hasXMMInt()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006519 // FIXME: Figure out a cleaner way to do this.
6520 // Try to make use of movq to zero out the top part.
6521 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6522 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6523 if (NewOp.getNode()) {
6524 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6525 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6526 DAG, Subtarget, dl);
6527 }
6528 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6529 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6530 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6531 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6532 DAG, Subtarget, dl);
6533 }
6534 }
6535 return SDValue();
6536}
6537
Dan Gohman475871a2008-07-27 21:46:04 +00006538SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006539X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006540 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006541 SDValue V1 = Op.getOperand(0);
6542 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006543 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006544 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006545 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006546 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006547 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
6548 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006549 bool V1IsSplat = false;
6550 bool V2IsSplat = false;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006551 bool HasXMMInt = Subtarget->hasXMMInt();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006552 MachineFunction &MF = DAG.getMachineFunction();
6553 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006554
Dale Johannesen0488fb62010-09-30 23:57:10 +00006555 // Shuffle operations on MMX not supported.
6556 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006557 return Op;
6558
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006559 // Vector shuffle lowering takes 3 steps:
6560 //
6561 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6562 // narrowing and commutation of operands should be handled.
6563 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6564 // shuffle nodes.
6565 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6566 // so the shuffle can be broken into other shuffles and the legalizer can
6567 // try the lowering again.
6568 //
6569 // The general ideia is that no vector_shuffle operation should be left to
6570 // be matched during isel, all of them must be converted to a target specific
6571 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006572
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006573 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6574 // narrowing and commutation of operands should be handled. The actual code
6575 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006576 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006577 if (NewOp.getNode())
6578 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006579
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006580 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6581 // unpckh_undef). Only use pshufd if speed is more important than size.
6582 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006583 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006584 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006585 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006586
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006587 if (X86::isMOVDDUPMask(SVOp) &&
6588 (Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
6589 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006590 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006591
Dale Johannesen0488fb62010-09-30 23:57:10 +00006592 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006593 return getMOVHighToLow(Op, dl, DAG);
6594
6595 // Use to match splats
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006596 if (HasXMMInt && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006597 (VT == MVT::v2f64 || VT == MVT::v2i64))
6598 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
6599
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006600 if (X86::isPSHUFDMask(SVOp)) {
6601 // The actual implementation will match the mask in the if above and then
6602 // during isel it can match several different instructions, not only pshufd
6603 // as its name says, sad but true, emulate the behavior for now...
6604 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6605 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6606
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006607 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6608
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006609 if (HasXMMInt && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006610 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6611
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006612 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6613 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006614 }
Eric Christopherfd179292009-08-27 18:07:15 +00006615
Evan Chengf26ffe92008-05-29 08:22:04 +00006616 // Check if this can be converted into a logical shift.
6617 bool isLeft = false;
6618 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006619 SDValue ShVal;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006620 bool isShift = getSubtarget()->hasXMMInt() &&
6621 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006622 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006623 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006624 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006625 EVT EltVT = VT.getVectorElementType();
6626 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006627 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006628 }
Eric Christopherfd179292009-08-27 18:07:15 +00006629
Nate Begeman9008ca62009-04-27 18:41:29 +00006630 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006631 if (V1IsUndef)
6632 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00006633 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006634 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006635 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006636 if (HasXMMInt && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006637 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6638
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006639 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006640 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6641 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006642 }
Eric Christopherfd179292009-08-27 18:07:15 +00006643
Nate Begeman9008ca62009-04-27 18:41:29 +00006644 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00006645 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006646 return getMOVLowToHigh(Op, dl, DAG, HasXMMInt);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006647
Dale Johannesen0488fb62010-09-30 23:57:10 +00006648 if (X86::isMOVHLPSMask(SVOp))
6649 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006650
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006651 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006652 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006653
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006654 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006655 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006656
Dale Johannesen0488fb62010-09-30 23:57:10 +00006657 if (X86::isMOVLPMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006658 return getMOVLP(Op, dl, DAG, HasXMMInt);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006659
Nate Begeman9008ca62009-04-27 18:41:29 +00006660 if (ShouldXformToMOVHLPS(SVOp) ||
6661 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6662 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006663
Evan Chengf26ffe92008-05-29 08:22:04 +00006664 if (isShift) {
6665 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006666 EVT EltVT = VT.getVectorElementType();
6667 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006668 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006669 }
Eric Christopherfd179292009-08-27 18:07:15 +00006670
Evan Cheng9eca5e82006-10-25 21:49:50 +00006671 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006672 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6673 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006674 V1IsSplat = isSplatVector(V1.getNode());
6675 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006676
Chris Lattner8a594482007-11-25 00:24:49 +00006677 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00006678 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006679 Op = CommuteVectorShuffle(SVOp, DAG);
6680 SVOp = cast<ShuffleVectorSDNode>(Op);
6681 V1 = SVOp->getOperand(0);
6682 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006683 std::swap(V1IsSplat, V2IsSplat);
6684 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006685 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006686 }
6687
Nate Begeman9008ca62009-04-27 18:41:29 +00006688 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6689 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006690 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006691 return V1;
6692 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6693 // the instruction selector will not match, so get a canonical MOVL with
6694 // swapped operands to undo the commute.
6695 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006696 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006697
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006698 if (X86::isUNPCKLMask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006699 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006700
6701 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006702 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006703
Evan Cheng9bbbb982006-10-25 20:48:19 +00006704 if (V2IsSplat) {
6705 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006706 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006707 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006708 SDValue NewMask = NormalizeMask(SVOp, DAG);
6709 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6710 if (NSVOp != SVOp) {
6711 if (X86::isUNPCKLMask(NSVOp, true)) {
6712 return NewMask;
6713 } else if (X86::isUNPCKHMask(NSVOp, true)) {
6714 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006715 }
6716 }
6717 }
6718
Evan Cheng9eca5e82006-10-25 21:49:50 +00006719 if (Commuted) {
6720 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006721 // FIXME: this seems wrong.
6722 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6723 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006724
6725 if (X86::isUNPCKLMask(NewSVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006726 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006727
6728 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006729 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006730 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006731
Nate Begeman9008ca62009-04-27 18:41:29 +00006732 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006733 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006734 return CommuteVectorShuffle(SVOp, DAG);
6735
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006736 // The checks below are all present in isShuffleMaskLegal, but they are
6737 // inlined here right now to enable us to directly emit target specific
6738 // nodes, and remove one by one until they don't return Op anymore.
6739 SmallVector<int, 16> M;
6740 SVOp->getMask(M);
6741
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006742 if (isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006743 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6744 X86::getShufflePALIGNRImmediate(SVOp),
6745 DAG);
6746
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006747 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6748 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006749 if (VT == MVT::v2f64)
6750 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006751 if (VT == MVT::v2i64)
6752 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6753 }
6754
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006755 if (isPSHUFHWMask(M, VT))
6756 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6757 X86::getShufflePSHUFHWImmediate(SVOp),
6758 DAG);
6759
6760 if (isPSHUFLWMask(M, VT))
6761 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6762 X86::getShufflePSHUFLWImmediate(SVOp),
6763 DAG);
6764
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006765 if (isSHUFPMask(M, VT))
6766 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6767 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006768
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006769 if (X86::isUNPCKL_v_undef_Mask(SVOp))
Bruno Cardoso Lopes5f6c4402011-07-26 02:39:25 +00006770 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006771 if (X86::isUNPCKH_v_undef_Mask(SVOp))
Rafael Espindola23e31012011-07-22 18:56:05 +00006772 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006773
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006774 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006775 // Generate target specific nodes for 128 or 256-bit shuffles only
6776 // supported in the AVX instruction set.
6777 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006778
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006779 // Handle VMOVDDUPY permutations
6780 if (isMOVDDUPYMask(SVOp, Subtarget))
6781 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6782
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006783 // Handle VPERMILPS* permutations
6784 if (isVPERMILPSMask(M, VT, Subtarget))
6785 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6786 getShuffleVPERMILPSImmediate(SVOp), DAG);
6787
6788 // Handle VPERMILPD* permutations
6789 if (isVPERMILPDMask(M, VT, Subtarget))
6790 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6791 getShuffleVPERMILPDImmediate(SVOp), DAG);
6792
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00006793 // Handle VPERM2F128 permutations
6794 if (isVPERM2F128Mask(M, VT, Subtarget))
6795 return getTargetShuffleNode(X86ISD::VPERM2F128, dl, VT, V1, V2,
6796 getShuffleVPERM2F128Immediate(SVOp), DAG);
6797
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006798 // Handle VSHUFPSY permutations
6799 if (isVSHUFPSYMask(M, VT, Subtarget))
6800 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6801 getShuffleVSHUFPSYImmediate(SVOp), DAG);
6802
6803 // Handle VSHUFPDY permutations
6804 if (isVSHUFPDYMask(M, VT, Subtarget))
6805 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6806 getShuffleVSHUFPDYImmediate(SVOp), DAG);
6807
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006808 //===--------------------------------------------------------------------===//
6809 // Since no target specific shuffle was selected for this generic one,
6810 // lower it into other known shuffles. FIXME: this isn't true yet, but
6811 // this is the plan.
6812 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006813
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006814 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6815 if (VT == MVT::v8i16) {
6816 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6817 if (NewOp.getNode())
6818 return NewOp;
6819 }
6820
6821 if (VT == MVT::v16i8) {
6822 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6823 if (NewOp.getNode())
6824 return NewOp;
6825 }
6826
6827 // Handle all 128-bit wide vectors with 4 elements, and match them with
6828 // several different shuffle types.
6829 if (NumElems == 4 && VT.getSizeInBits() == 128)
6830 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6831
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006832 // Handle general 256-bit shuffles
6833 if (VT.is256BitVector())
6834 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6835
Dan Gohman475871a2008-07-27 21:46:04 +00006836 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006837}
6838
Dan Gohman475871a2008-07-27 21:46:04 +00006839SDValue
6840X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006841 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006842 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006843 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006844
6845 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6846 return SDValue();
6847
Duncan Sands83ec4b62008-06-06 12:08:01 +00006848 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006849 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006850 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006851 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006852 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006853 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006854 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006855 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6856 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6857 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006858 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6859 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006860 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006861 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006862 Op.getOperand(0)),
6863 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006864 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006865 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006866 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006867 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006868 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006869 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006870 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6871 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006872 // result has a single use which is a store or a bitcast to i32. And in
6873 // the case of a store, it's not worth it if the index is a constant 0,
6874 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006875 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006876 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006877 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006878 if ((User->getOpcode() != ISD::STORE ||
6879 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6880 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006881 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006882 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006883 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006884 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006885 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006886 Op.getOperand(0)),
6887 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006888 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006889 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006890 // ExtractPS works with constant index.
6891 if (isa<ConstantSDNode>(Op.getOperand(1)))
6892 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006893 }
Dan Gohman475871a2008-07-27 21:46:04 +00006894 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006895}
6896
6897
Dan Gohman475871a2008-07-27 21:46:04 +00006898SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006899X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6900 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006901 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006902 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006903
David Greene74a579d2011-02-10 16:57:36 +00006904 SDValue Vec = Op.getOperand(0);
6905 EVT VecVT = Vec.getValueType();
6906
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006907 // If this is a 256-bit vector result, first extract the 128-bit vector and
6908 // then extract the element from the 128-bit vector.
6909 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006910 DebugLoc dl = Op.getNode()->getDebugLoc();
6911 unsigned NumElems = VecVT.getVectorNumElements();
6912 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006913 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6914
6915 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006916 bool Upper = IdxVal >= NumElems/2;
6917 Vec = Extract128BitVector(Vec,
6918 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006919
David Greene74a579d2011-02-10 16:57:36 +00006920 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006921 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00006922 }
6923
6924 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6925
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006926 if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006927 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006928 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006929 return Res;
6930 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006931
Owen Andersone50ed302009-08-10 22:56:29 +00006932 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006933 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006934 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006935 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006936 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006937 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006938 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006939 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6940 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006941 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006942 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006943 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006944 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006945 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006946 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006947 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006948 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006949 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006950 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006951 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006952 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006953 if (Idx == 0)
6954 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006955
Evan Cheng0db9fe62006-04-25 20:13:52 +00006956 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006957 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006958 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006959 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006960 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006961 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006962 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006963 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006964 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6965 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6966 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006967 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006968 if (Idx == 0)
6969 return Op;
6970
6971 // UNPCKHPD the element to the lowest double word, then movsd.
6972 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6973 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006974 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006975 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006976 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006977 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006978 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006979 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006980 }
6981
Dan Gohman475871a2008-07-27 21:46:04 +00006982 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006983}
6984
Dan Gohman475871a2008-07-27 21:46:04 +00006985SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006986X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6987 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006988 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006989 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006990 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006991
Dan Gohman475871a2008-07-27 21:46:04 +00006992 SDValue N0 = Op.getOperand(0);
6993 SDValue N1 = Op.getOperand(1);
6994 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006995
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006996 if (VT.getSizeInBits() == 256)
6997 return SDValue();
6998
Dan Gohman8a55ce42009-09-23 21:02:20 +00006999 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007000 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007001 unsigned Opc;
7002 if (VT == MVT::v8i16)
7003 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007004 else if (VT == MVT::v16i8)
7005 Opc = X86ISD::PINSRB;
7006 else
7007 Opc = X86ISD::PINSRB;
7008
Nate Begeman14d12ca2008-02-11 04:19:36 +00007009 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7010 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007011 if (N1.getValueType() != MVT::i32)
7012 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7013 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007014 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007015 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007016 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007017 // Bits [7:6] of the constant are the source select. This will always be
7018 // zero here. The DAG Combiner may combine an extract_elt index into these
7019 // bits. For example (insert (extract, 3), 2) could be matched by putting
7020 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007021 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007022 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007023 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007024 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007025 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007026 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007027 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007028 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007029 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007030 // PINSR* works with constant index.
7031 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007032 }
Dan Gohman475871a2008-07-27 21:46:04 +00007033 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007034}
7035
Dan Gohman475871a2008-07-27 21:46:04 +00007036SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007037X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007038 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007039 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007040
David Greene6b381262011-02-09 15:32:06 +00007041 DebugLoc dl = Op.getDebugLoc();
7042 SDValue N0 = Op.getOperand(0);
7043 SDValue N1 = Op.getOperand(1);
7044 SDValue N2 = Op.getOperand(2);
7045
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007046 // If this is a 256-bit vector result, first extract the 128-bit vector,
7047 // insert the element into the extracted half and then place it back.
7048 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007049 if (!isa<ConstantSDNode>(N2))
7050 return SDValue();
7051
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007052 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007053 unsigned NumElems = VT.getVectorNumElements();
7054 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007055 bool Upper = IdxVal >= NumElems/2;
7056 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
7057 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007058
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007059 // Insert the element into the desired half.
7060 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
7061 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00007062
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007063 // Insert the changed part back to the 256-bit vector
7064 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007065 }
7066
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007067 if (Subtarget->hasSSE41() || Subtarget->hasAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007068 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7069
Dan Gohman8a55ce42009-09-23 21:02:20 +00007070 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007071 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007072
Dan Gohman8a55ce42009-09-23 21:02:20 +00007073 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007074 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7075 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007076 if (N1.getValueType() != MVT::i32)
7077 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7078 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007079 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007080 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007081 }
Dan Gohman475871a2008-07-27 21:46:04 +00007082 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007083}
7084
Dan Gohman475871a2008-07-27 21:46:04 +00007085SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007086X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007087 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007088 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007089 EVT OpVT = Op.getValueType();
7090
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007091 // If this is a 256-bit vector result, first insert into a 128-bit
7092 // vector and then insert into the 256-bit vector.
7093 if (OpVT.getSizeInBits() > 128) {
7094 // Insert into a 128-bit vector.
7095 EVT VT128 = EVT::getVectorVT(*Context,
7096 OpVT.getVectorElementType(),
7097 OpVT.getVectorNumElements() / 2);
7098
7099 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7100
7101 // Insert the 128-bit vector.
7102 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7103 DAG.getConstant(0, MVT::i32),
7104 DAG, dl);
7105 }
7106
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007107 if (Op.getValueType() == MVT::v1i64 &&
7108 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007109 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007110
Owen Anderson825b72b2009-08-11 20:47:22 +00007111 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007112 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7113 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007114 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007115 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007116}
7117
David Greene91585092011-01-26 15:38:49 +00007118// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7119// a simple subregister reference or explicit instructions to grab
7120// upper bits of a vector.
7121SDValue
7122X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7123 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007124 DebugLoc dl = Op.getNode()->getDebugLoc();
7125 SDValue Vec = Op.getNode()->getOperand(0);
7126 SDValue Idx = Op.getNode()->getOperand(1);
7127
7128 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7129 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7130 return Extract128BitVector(Vec, Idx, DAG, dl);
7131 }
David Greene91585092011-01-26 15:38:49 +00007132 }
7133 return SDValue();
7134}
7135
David Greenecfe33c42011-01-26 19:13:22 +00007136// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7137// simple superregister reference or explicit instructions to insert
7138// the upper bits of a vector.
7139SDValue
7140X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7141 if (Subtarget->hasAVX()) {
7142 DebugLoc dl = Op.getNode()->getDebugLoc();
7143 SDValue Vec = Op.getNode()->getOperand(0);
7144 SDValue SubVec = Op.getNode()->getOperand(1);
7145 SDValue Idx = Op.getNode()->getOperand(2);
7146
7147 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7148 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007149 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007150 }
7151 }
7152 return SDValue();
7153}
7154
Bill Wendling056292f2008-09-16 21:48:12 +00007155// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7156// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7157// one of the above mentioned nodes. It has to be wrapped because otherwise
7158// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7159// be used to form addressing mode. These wrapped nodes will be selected
7160// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007161SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007162X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007163 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007164
Chris Lattner41621a22009-06-26 19:22:52 +00007165 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7166 // global base reg.
7167 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007168 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007169 CodeModel::Model M = getTargetMachine().getCodeModel();
7170
Chris Lattner4f066492009-07-11 20:29:19 +00007171 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007172 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007173 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007174 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007175 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007176 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007177 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007178
Evan Cheng1606e8e2009-03-13 07:51:59 +00007179 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007180 CP->getAlignment(),
7181 CP->getOffset(), OpFlag);
7182 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007183 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007184 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007185 if (OpFlag) {
7186 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007187 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007188 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007189 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007190 }
7191
7192 return Result;
7193}
7194
Dan Gohmand858e902010-04-17 15:26:15 +00007195SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007196 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007197
Chris Lattner18c59872009-06-27 04:16:01 +00007198 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7199 // global base reg.
7200 unsigned char OpFlag = 0;
7201 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007202 CodeModel::Model M = getTargetMachine().getCodeModel();
7203
Chris Lattner4f066492009-07-11 20:29:19 +00007204 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007205 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007206 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007207 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007208 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007209 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007210 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007211
Chris Lattner18c59872009-06-27 04:16:01 +00007212 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7213 OpFlag);
7214 DebugLoc DL = JT->getDebugLoc();
7215 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007216
Chris Lattner18c59872009-06-27 04:16:01 +00007217 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007218 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007219 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7220 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007221 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007222 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007223
Chris Lattner18c59872009-06-27 04:16:01 +00007224 return Result;
7225}
7226
7227SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007228X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007229 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007230
Chris Lattner18c59872009-06-27 04:16:01 +00007231 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7232 // global base reg.
7233 unsigned char OpFlag = 0;
7234 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007235 CodeModel::Model M = getTargetMachine().getCodeModel();
7236
Chris Lattner4f066492009-07-11 20:29:19 +00007237 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007238 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7239 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7240 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007241 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007242 } else if (Subtarget->isPICStyleGOT()) {
7243 OpFlag = X86II::MO_GOT;
7244 } else if (Subtarget->isPICStyleStubPIC()) {
7245 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7246 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7247 OpFlag = X86II::MO_DARWIN_NONLAZY;
7248 }
Eric Christopherfd179292009-08-27 18:07:15 +00007249
Chris Lattner18c59872009-06-27 04:16:01 +00007250 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007251
Chris Lattner18c59872009-06-27 04:16:01 +00007252 DebugLoc DL = Op.getDebugLoc();
7253 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007254
7255
Chris Lattner18c59872009-06-27 04:16:01 +00007256 // With PIC, the address is actually $g + Offset.
7257 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007258 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007259 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7260 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007261 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007262 Result);
7263 }
Eric Christopherfd179292009-08-27 18:07:15 +00007264
Eli Friedman586272d2011-08-11 01:48:05 +00007265 // For symbols that require a load from a stub to get the address, emit the
7266 // load.
7267 if (isGlobalStubReference(OpFlag))
7268 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
7269 MachinePointerInfo::getGOT(), false, false, 0);
7270
Chris Lattner18c59872009-06-27 04:16:01 +00007271 return Result;
7272}
7273
Dan Gohman475871a2008-07-27 21:46:04 +00007274SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007275X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007276 // Create the TargetBlockAddressAddress node.
7277 unsigned char OpFlags =
7278 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007279 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007280 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007281 DebugLoc dl = Op.getDebugLoc();
7282 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7283 /*isTarget=*/true, OpFlags);
7284
Dan Gohmanf705adb2009-10-30 01:28:02 +00007285 if (Subtarget->isPICStyleRIPRel() &&
7286 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007287 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7288 else
7289 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007290
Dan Gohman29cbade2009-11-20 23:18:13 +00007291 // With PIC, the address is actually $g + Offset.
7292 if (isGlobalRelativeToPICBase(OpFlags)) {
7293 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7294 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7295 Result);
7296 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007297
7298 return Result;
7299}
7300
7301SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007302X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007303 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007304 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007305 // Create the TargetGlobalAddress node, folding in the constant
7306 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007307 unsigned char OpFlags =
7308 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007309 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007310 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007311 if (OpFlags == X86II::MO_NO_FLAG &&
7312 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007313 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007314 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007315 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007316 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007317 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007318 }
Eric Christopherfd179292009-08-27 18:07:15 +00007319
Chris Lattner4f066492009-07-11 20:29:19 +00007320 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007321 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007322 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7323 else
7324 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007325
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007326 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007327 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007328 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7329 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007330 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007331 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007332
Chris Lattner36c25012009-07-10 07:34:39 +00007333 // For globals that require a load from a stub to get the address, emit the
7334 // load.
7335 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007336 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007337 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007338
Dan Gohman6520e202008-10-18 02:06:02 +00007339 // If there was a non-zero offset that we didn't fold, create an explicit
7340 // addition for it.
7341 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007342 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007343 DAG.getConstant(Offset, getPointerTy()));
7344
Evan Cheng0db9fe62006-04-25 20:13:52 +00007345 return Result;
7346}
7347
Evan Chengda43bcf2008-09-24 00:05:32 +00007348SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007349X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007350 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007351 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007352 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007353}
7354
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007355static SDValue
7356GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007357 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007358 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007359 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007360 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007361 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007362 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007363 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007364 GA->getOffset(),
7365 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007366 if (InFlag) {
7367 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007368 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007369 } else {
7370 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007371 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007372 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007373
7374 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007375 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007376
Rafael Espindola15f1b662009-04-24 12:59:40 +00007377 SDValue Flag = Chain.getValue(1);
7378 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007379}
7380
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007381// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007382static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007383LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007384 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007385 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007386 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7387 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007388 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007389 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007390 InFlag = Chain.getValue(1);
7391
Chris Lattnerb903bed2009-06-26 21:20:29 +00007392 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007393}
7394
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007395// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007396static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007397LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007398 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007399 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7400 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007401}
7402
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007403// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7404// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007405static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007406 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007407 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007408 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007409
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007410 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7411 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7412 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007413
Michael J. Spencerec38de22010-10-10 22:04:20 +00007414 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007415 DAG.getIntPtrConstant(0),
7416 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007417
Chris Lattnerb903bed2009-06-26 21:20:29 +00007418 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007419 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7420 // initialexec.
7421 unsigned WrapperKind = X86ISD::Wrapper;
7422 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007423 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007424 } else if (is64Bit) {
7425 assert(model == TLSModel::InitialExec);
7426 OperandFlags = X86II::MO_GOTTPOFF;
7427 WrapperKind = X86ISD::WrapperRIP;
7428 } else {
7429 assert(model == TLSModel::InitialExec);
7430 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007431 }
Eric Christopherfd179292009-08-27 18:07:15 +00007432
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007433 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7434 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007435 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007436 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007437 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007438 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007439
Rafael Espindola9a580232009-02-27 13:37:18 +00007440 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007441 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00007442 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007443
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007444 // The address of the thread local variable is the add of the thread
7445 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007446 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007447}
7448
Dan Gohman475871a2008-07-27 21:46:04 +00007449SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007450X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007451
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007452 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007453 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007454
Eric Christopher30ef0e52010-06-03 04:07:48 +00007455 if (Subtarget->isTargetELF()) {
7456 // TODO: implement the "local dynamic" model
7457 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007458
Eric Christopher30ef0e52010-06-03 04:07:48 +00007459 // If GV is an alias then use the aliasee for determining
7460 // thread-localness.
7461 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7462 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007463
7464 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007465 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007466
Eric Christopher30ef0e52010-06-03 04:07:48 +00007467 switch (model) {
7468 case TLSModel::GeneralDynamic:
7469 case TLSModel::LocalDynamic: // not implemented
7470 if (Subtarget->is64Bit())
7471 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7472 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007473
Eric Christopher30ef0e52010-06-03 04:07:48 +00007474 case TLSModel::InitialExec:
7475 case TLSModel::LocalExec:
7476 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7477 Subtarget->is64Bit());
7478 }
7479 } else if (Subtarget->isTargetDarwin()) {
7480 // Darwin only has one model of TLS. Lower to that.
7481 unsigned char OpFlag = 0;
7482 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7483 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007484
Eric Christopher30ef0e52010-06-03 04:07:48 +00007485 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7486 // global base reg.
7487 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7488 !Subtarget->is64Bit();
7489 if (PIC32)
7490 OpFlag = X86II::MO_TLVP_PIC_BASE;
7491 else
7492 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007493 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007494 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007495 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007496 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007497 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007498
Eric Christopher30ef0e52010-06-03 04:07:48 +00007499 // With PIC32, the address is actually $g + Offset.
7500 if (PIC32)
7501 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7502 DAG.getNode(X86ISD::GlobalBaseReg,
7503 DebugLoc(), getPointerTy()),
7504 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007505
Eric Christopher30ef0e52010-06-03 04:07:48 +00007506 // Lowering the machine isd will make sure everything is in the right
7507 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007508 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007509 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007510 SDValue Args[] = { Chain, Offset };
7511 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007512
Eric Christopher30ef0e52010-06-03 04:07:48 +00007513 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7514 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7515 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007516
Eric Christopher30ef0e52010-06-03 04:07:48 +00007517 // And our return value (tls address) is in the standard call return value
7518 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007519 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
7520 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007521 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007522
Eric Christopher30ef0e52010-06-03 04:07:48 +00007523 assert(false &&
7524 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007525
Torok Edwinc23197a2009-07-14 16:55:14 +00007526 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007527 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007528}
7529
Evan Cheng0db9fe62006-04-25 20:13:52 +00007530
Nadav Rotem43012222011-05-11 08:12:09 +00007531/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007532/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007533SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007534 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007535 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007536 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007537 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007538 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007539 SDValue ShOpLo = Op.getOperand(0);
7540 SDValue ShOpHi = Op.getOperand(1);
7541 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007542 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007543 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007544 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007545
Dan Gohman475871a2008-07-27 21:46:04 +00007546 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007547 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007548 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7549 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007550 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007551 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7552 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007553 }
Evan Chenge3413162006-01-09 18:33:28 +00007554
Owen Anderson825b72b2009-08-11 20:47:22 +00007555 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7556 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007557 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007558 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007559
Dan Gohman475871a2008-07-27 21:46:04 +00007560 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007561 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007562 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7563 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007564
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007565 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007566 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7567 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007568 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007569 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7570 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007571 }
7572
Dan Gohman475871a2008-07-27 21:46:04 +00007573 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007574 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007575}
Evan Chenga3195e82006-01-12 22:54:21 +00007576
Dan Gohmand858e902010-04-17 15:26:15 +00007577SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7578 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007579 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007580
Dale Johannesen0488fb62010-09-30 23:57:10 +00007581 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007582 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007583
Owen Anderson825b72b2009-08-11 20:47:22 +00007584 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007585 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007586
Eli Friedman36df4992009-05-27 00:47:34 +00007587 // These are really Legal; return the operand so the caller accepts it as
7588 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007589 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007590 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007591 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007592 Subtarget->is64Bit()) {
7593 return Op;
7594 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007595
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007596 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007597 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007598 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007599 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007600 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007601 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007602 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007603 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007604 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007605 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7606}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007607
Owen Andersone50ed302009-08-10 22:56:29 +00007608SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007609 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007610 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007611 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007612 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007613 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007614 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007615 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007616 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007617 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007618 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007619
Chris Lattner492a43e2010-09-22 01:28:21 +00007620 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007621
Stuart Hastings84be9582011-06-02 15:57:11 +00007622 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7623 MachineMemOperand *MMO;
7624 if (FI) {
7625 int SSFI = FI->getIndex();
7626 MMO =
7627 DAG.getMachineFunction()
7628 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7629 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7630 } else {
7631 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7632 StackSlot = StackSlot.getOperand(1);
7633 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007634 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007635 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7636 X86ISD::FILD, DL,
7637 Tys, Ops, array_lengthof(Ops),
7638 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007639
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007640 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007641 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007642 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007643
7644 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7645 // shouldn't be necessary except that RFP cannot be live across
7646 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007647 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007648 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7649 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007650 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007651 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007652 SDValue Ops[] = {
7653 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7654 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007655 MachineMemOperand *MMO =
7656 DAG.getMachineFunction()
7657 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007658 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007659
Chris Lattner492a43e2010-09-22 01:28:21 +00007660 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7661 Ops, array_lengthof(Ops),
7662 Op.getValueType(), MMO);
7663 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007664 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007665 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007666 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007667
Evan Cheng0db9fe62006-04-25 20:13:52 +00007668 return Result;
7669}
7670
Bill Wendling8b8a6362009-01-17 03:56:04 +00007671// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007672SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7673 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007674 // This algorithm is not obvious. Here it is in C code, more or less:
7675 /*
7676 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7677 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7678 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007679
Bill Wendling8b8a6362009-01-17 03:56:04 +00007680 // Copy ints to xmm registers.
7681 __m128i xh = _mm_cvtsi32_si128( hi );
7682 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007683
Bill Wendling8b8a6362009-01-17 03:56:04 +00007684 // Combine into low half of a single xmm register.
7685 __m128i x = _mm_unpacklo_epi32( xh, xl );
7686 __m128d d;
7687 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007688
Bill Wendling8b8a6362009-01-17 03:56:04 +00007689 // Merge in appropriate exponents to give the integer bits the right
7690 // magnitude.
7691 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007692
Bill Wendling8b8a6362009-01-17 03:56:04 +00007693 // Subtract away the biases to deal with the IEEE-754 double precision
7694 // implicit 1.
7695 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007696
Bill Wendling8b8a6362009-01-17 03:56:04 +00007697 // All conversions up to here are exact. The correctly rounded result is
7698 // calculated using the current rounding mode using the following
7699 // horizontal add.
7700 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7701 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7702 // store doesn't really need to be here (except
7703 // maybe to zero the other double)
7704 return sd;
7705 }
7706 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007707
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007708 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007709 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007710
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007711 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007712 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007713 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7714 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7715 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7716 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007717 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007718 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007719
Bill Wendling8b8a6362009-01-17 03:56:04 +00007720 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007721 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007722 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007723 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007724 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007725 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007726 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007727
Owen Anderson825b72b2009-08-11 20:47:22 +00007728 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7729 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007730 Op.getOperand(0),
7731 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007732 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7733 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007734 Op.getOperand(0),
7735 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007736 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7737 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007738 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007739 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007740 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007741 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007742 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007743 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007744 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007745 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007746
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007747 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007748 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007749 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7750 DAG.getUNDEF(MVT::v2f64), ShufMask);
7751 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7752 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007753 DAG.getIntPtrConstant(0));
7754}
7755
Bill Wendling8b8a6362009-01-17 03:56:04 +00007756// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007757SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7758 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007759 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007760 // FP constant to bias correct the final result.
7761 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007762 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007763
7764 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007765 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007766 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007767
Eli Friedmanf3704762011-08-29 21:15:46 +00007768 // Zero out the upper parts of the register.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00007769 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasXMMInt(),
7770 DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007771
Owen Anderson825b72b2009-08-11 20:47:22 +00007772 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007773 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007774 DAG.getIntPtrConstant(0));
7775
7776 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007777 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007778 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007779 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007780 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007781 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007782 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007783 MVT::v2f64, Bias)));
7784 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007785 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007786 DAG.getIntPtrConstant(0));
7787
7788 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007789 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007790
7791 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007792 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007793
Owen Anderson825b72b2009-08-11 20:47:22 +00007794 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007795 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007796 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007797 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007798 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007799 }
7800
7801 // Handle final rounding.
7802 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007803}
7804
Dan Gohmand858e902010-04-17 15:26:15 +00007805SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7806 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007807 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007808 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007809
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007810 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007811 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7812 // the optimization here.
7813 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007814 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007815
Owen Andersone50ed302009-08-10 22:56:29 +00007816 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007817 EVT DstVT = Op.getValueType();
7818 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007819 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007820 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007821 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007822
7823 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007824 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007825 if (SrcVT == MVT::i32) {
7826 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7827 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7828 getPointerTy(), StackSlot, WordOff);
7829 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007830 StackSlot, MachinePointerInfo(),
7831 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007832 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007833 OffsetSlot, MachinePointerInfo(),
7834 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007835 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7836 return Fild;
7837 }
7838
7839 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7840 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007841 StackSlot, MachinePointerInfo(),
7842 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007843 // For i64 source, we need to add the appropriate power of 2 if the input
7844 // was negative. This is the same as the optimization in
7845 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7846 // we must be careful to do the computation in x87 extended precision, not
7847 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007848 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7849 MachineMemOperand *MMO =
7850 DAG.getMachineFunction()
7851 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7852 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007853
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007854 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7855 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007856 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7857 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007858
7859 APInt FF(32, 0x5F800000ULL);
7860
7861 // Check whether the sign bit is set.
7862 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7863 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7864 ISD::SETLT);
7865
7866 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7867 SDValue FudgePtr = DAG.getConstantPool(
7868 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7869 getPointerTy());
7870
7871 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7872 SDValue Zero = DAG.getIntPtrConstant(0);
7873 SDValue Four = DAG.getIntPtrConstant(4);
7874 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7875 Zero, Four);
7876 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7877
7878 // Load the value out, extending it from f32 to f80.
7879 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007880 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007881 FudgePtr, MachinePointerInfo::getConstantPool(),
7882 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007883 // Extend everything to 80 bits to force it to be done on x87.
7884 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7885 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007886}
7887
Dan Gohman475871a2008-07-27 21:46:04 +00007888std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007889FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007890 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007891
Owen Andersone50ed302009-08-10 22:56:29 +00007892 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007893
7894 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007895 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7896 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007897 }
7898
Owen Anderson825b72b2009-08-11 20:47:22 +00007899 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7900 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007901 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007902
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007903 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007904 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007905 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007906 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007907 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007908 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007909 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007910 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007911
Evan Cheng87c89352007-10-15 20:11:21 +00007912 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7913 // stack slot.
7914 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007915 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007916 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007917 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007918
Michael J. Spencerec38de22010-10-10 22:04:20 +00007919
7920
Evan Cheng0db9fe62006-04-25 20:13:52 +00007921 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007922 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007923 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007924 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7925 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7926 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007927 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007928
Dan Gohman475871a2008-07-27 21:46:04 +00007929 SDValue Chain = DAG.getEntryNode();
7930 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007931 EVT TheVT = Op.getOperand(0).getValueType();
7932 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007933 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007934 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007935 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007936 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007937 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007938 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007939 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007940 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007941
Chris Lattner492a43e2010-09-22 01:28:21 +00007942 MachineMemOperand *MMO =
7943 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7944 MachineMemOperand::MOLoad, MemSize, MemSize);
7945 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7946 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007947 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007948 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007949 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7950 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007951
Chris Lattner07290932010-09-22 01:05:16 +00007952 MachineMemOperand *MMO =
7953 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7954 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007955
Evan Cheng0db9fe62006-04-25 20:13:52 +00007956 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007957 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007958 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7959 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007960
Chris Lattner27a6c732007-11-24 07:07:01 +00007961 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007962}
7963
Dan Gohmand858e902010-04-17 15:26:15 +00007964SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7965 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007966 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007967 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007968
Eli Friedman948e95a2009-05-23 09:59:16 +00007969 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007970 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007971 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7972 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007973
Chris Lattner27a6c732007-11-24 07:07:01 +00007974 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007975 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007976 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007977}
7978
Dan Gohmand858e902010-04-17 15:26:15 +00007979SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7980 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007981 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7982 SDValue FIST = Vals.first, StackSlot = Vals.second;
7983 assert(FIST.getNode() && "Unexpected failure");
7984
7985 // Load the result.
7986 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007987 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007988}
7989
Dan Gohmand858e902010-04-17 15:26:15 +00007990SDValue X86TargetLowering::LowerFABS(SDValue Op,
7991 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007992 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007993 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007994 EVT VT = Op.getValueType();
7995 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007996 if (VT.isVector())
7997 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007998 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007999 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008000 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00008001 CV.push_back(C);
8002 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008003 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008004 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00008005 CV.push_back(C);
8006 CV.push_back(C);
8007 CV.push_back(C);
8008 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008009 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008010 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008011 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008012 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008013 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008014 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008015 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008016}
8017
Dan Gohmand858e902010-04-17 15:26:15 +00008018SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008019 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008020 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008021 EVT VT = Op.getValueType();
8022 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00008023 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008024 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008025 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008026 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008027 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00008028 CV.push_back(C);
8029 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008030 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008031 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00008032 CV.push_back(C);
8033 CV.push_back(C);
8034 CV.push_back(C);
8035 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008036 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008037 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008038 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008039 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008040 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008041 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008042 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008043 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008044 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008045 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008046 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008047 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008048 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008049 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00008050 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008051}
8052
Dan Gohmand858e902010-04-17 15:26:15 +00008053SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008054 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008055 SDValue Op0 = Op.getOperand(0);
8056 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008057 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008058 EVT VT = Op.getValueType();
8059 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008060
8061 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008062 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008063 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008064 SrcVT = VT;
8065 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008066 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008067 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008068 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008069 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008070 }
8071
8072 // At this point the operands and the result should have the same
8073 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008074
Evan Cheng68c47cb2007-01-05 07:55:56 +00008075 // First get the sign bit of second operand.
8076 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008077 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008078 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8079 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008080 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008081 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8082 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8083 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8084 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008085 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008086 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008087 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008088 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008089 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008090 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008091 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008092
8093 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008094 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008095 // Op0 is MVT::f32, Op1 is MVT::f64.
8096 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8097 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8098 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008099 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008100 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008101 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008102 }
8103
Evan Cheng73d6cf12007-01-05 21:37:56 +00008104 // Clear first operand sign bit.
8105 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008106 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008107 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8108 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008109 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008110 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8111 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8112 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8113 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008114 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008115 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008116 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008117 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008118 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00008119 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008120 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008121
8122 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008123 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008124}
8125
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008126SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8127 SDValue N0 = Op.getOperand(0);
8128 DebugLoc dl = Op.getDebugLoc();
8129 EVT VT = Op.getValueType();
8130
8131 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8132 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8133 DAG.getConstant(1, VT));
8134 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8135}
8136
Dan Gohman076aee32009-03-04 19:44:21 +00008137/// Emit nodes that will be selected as "test Op0,Op0", or something
8138/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008139SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008140 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008141 DebugLoc dl = Op.getDebugLoc();
8142
Dan Gohman31125812009-03-07 01:58:32 +00008143 // CF and OF aren't always set the way we want. Determine which
8144 // of these we need.
8145 bool NeedCF = false;
8146 bool NeedOF = false;
8147 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008148 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008149 case X86::COND_A: case X86::COND_AE:
8150 case X86::COND_B: case X86::COND_BE:
8151 NeedCF = true;
8152 break;
8153 case X86::COND_G: case X86::COND_GE:
8154 case X86::COND_L: case X86::COND_LE:
8155 case X86::COND_O: case X86::COND_NO:
8156 NeedOF = true;
8157 break;
Dan Gohman31125812009-03-07 01:58:32 +00008158 }
8159
Dan Gohman076aee32009-03-04 19:44:21 +00008160 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008161 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8162 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008163 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8164 // Emit a CMP with 0, which is the TEST pattern.
8165 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8166 DAG.getConstant(0, Op.getValueType()));
8167
8168 unsigned Opcode = 0;
8169 unsigned NumOperands = 0;
8170 switch (Op.getNode()->getOpcode()) {
8171 case ISD::ADD:
8172 // Due to an isel shortcoming, be conservative if this add is likely to be
8173 // selected as part of a load-modify-store instruction. When the root node
8174 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8175 // uses of other nodes in the match, such as the ADD in this case. This
8176 // leads to the ADD being left around and reselected, with the result being
8177 // two adds in the output. Alas, even if none our users are stores, that
8178 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8179 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8180 // climbing the DAG back to the root, and it doesn't seem to be worth the
8181 // effort.
8182 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00008183 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008184 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
8185 goto default_case;
8186
8187 if (ConstantSDNode *C =
8188 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8189 // An add of one will be selected as an INC.
8190 if (C->getAPIntValue() == 1) {
8191 Opcode = X86ISD::INC;
8192 NumOperands = 1;
8193 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008194 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008195
8196 // An add of negative one (subtract of one) will be selected as a DEC.
8197 if (C->getAPIntValue().isAllOnesValue()) {
8198 Opcode = X86ISD::DEC;
8199 NumOperands = 1;
8200 break;
8201 }
Dan Gohman076aee32009-03-04 19:44:21 +00008202 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008203
8204 // Otherwise use a regular EFLAGS-setting add.
8205 Opcode = X86ISD::ADD;
8206 NumOperands = 2;
8207 break;
8208 case ISD::AND: {
8209 // If the primary and result isn't used, don't bother using X86ISD::AND,
8210 // because a TEST instruction will be better.
8211 bool NonFlagUse = false;
8212 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8213 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8214 SDNode *User = *UI;
8215 unsigned UOpNo = UI.getOperandNo();
8216 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8217 // Look pass truncate.
8218 UOpNo = User->use_begin().getOperandNo();
8219 User = *User->use_begin();
8220 }
8221
8222 if (User->getOpcode() != ISD::BRCOND &&
8223 User->getOpcode() != ISD::SETCC &&
8224 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8225 NonFlagUse = true;
8226 break;
8227 }
Dan Gohman076aee32009-03-04 19:44:21 +00008228 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008229
8230 if (!NonFlagUse)
8231 break;
8232 }
8233 // FALL THROUGH
8234 case ISD::SUB:
8235 case ISD::OR:
8236 case ISD::XOR:
8237 // Due to the ISEL shortcoming noted above, be conservative if this op is
8238 // likely to be selected as part of a load-modify-store instruction.
8239 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8240 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8241 if (UI->getOpcode() == ISD::STORE)
8242 goto default_case;
8243
8244 // Otherwise use a regular EFLAGS-setting instruction.
8245 switch (Op.getNode()->getOpcode()) {
8246 default: llvm_unreachable("unexpected operator!");
8247 case ISD::SUB: Opcode = X86ISD::SUB; break;
8248 case ISD::OR: Opcode = X86ISD::OR; break;
8249 case ISD::XOR: Opcode = X86ISD::XOR; break;
8250 case ISD::AND: Opcode = X86ISD::AND; break;
8251 }
8252
8253 NumOperands = 2;
8254 break;
8255 case X86ISD::ADD:
8256 case X86ISD::SUB:
8257 case X86ISD::INC:
8258 case X86ISD::DEC:
8259 case X86ISD::OR:
8260 case X86ISD::XOR:
8261 case X86ISD::AND:
8262 return SDValue(Op.getNode(), 1);
8263 default:
8264 default_case:
8265 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008266 }
8267
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008268 if (Opcode == 0)
8269 // Emit a CMP with 0, which is the TEST pattern.
8270 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8271 DAG.getConstant(0, Op.getValueType()));
8272
8273 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8274 SmallVector<SDValue, 4> Ops;
8275 for (unsigned i = 0; i != NumOperands; ++i)
8276 Ops.push_back(Op.getOperand(i));
8277
8278 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8279 DAG.ReplaceAllUsesWith(Op, New);
8280 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008281}
8282
8283/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8284/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008285SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008286 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008287 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8288 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008289 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008290
8291 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008292 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008293}
8294
Evan Chengd40d03e2010-01-06 19:38:29 +00008295/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8296/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008297SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8298 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008299 SDValue Op0 = And.getOperand(0);
8300 SDValue Op1 = And.getOperand(1);
8301 if (Op0.getOpcode() == ISD::TRUNCATE)
8302 Op0 = Op0.getOperand(0);
8303 if (Op1.getOpcode() == ISD::TRUNCATE)
8304 Op1 = Op1.getOperand(0);
8305
Evan Chengd40d03e2010-01-06 19:38:29 +00008306 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008307 if (Op1.getOpcode() == ISD::SHL)
8308 std::swap(Op0, Op1);
8309 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008310 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8311 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008312 // If we looked past a truncate, check that it's only truncating away
8313 // known zeros.
8314 unsigned BitWidth = Op0.getValueSizeInBits();
8315 unsigned AndBitWidth = And.getValueSizeInBits();
8316 if (BitWidth > AndBitWidth) {
8317 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8318 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8319 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8320 return SDValue();
8321 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008322 LHS = Op1;
8323 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008324 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008325 } else if (Op1.getOpcode() == ISD::Constant) {
8326 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
8327 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00008328 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
8329 LHS = AndLHS.getOperand(0);
8330 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008331 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008332 }
Evan Cheng0488db92007-09-25 01:57:46 +00008333
Evan Chengd40d03e2010-01-06 19:38:29 +00008334 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008335 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008336 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008337 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008338 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008339 // Also promote i16 to i32 for performance / code size reason.
8340 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008341 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008342 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008343
Evan Chengd40d03e2010-01-06 19:38:29 +00008344 // If the operand types disagree, extend the shift amount to match. Since
8345 // BT ignores high bits (like shifts) we can use anyextend.
8346 if (LHS.getValueType() != RHS.getValueType())
8347 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008348
Evan Chengd40d03e2010-01-06 19:38:29 +00008349 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8350 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8351 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8352 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008353 }
8354
Evan Cheng54de3ea2010-01-05 06:52:31 +00008355 return SDValue();
8356}
8357
Dan Gohmand858e902010-04-17 15:26:15 +00008358SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008359
8360 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8361
Evan Cheng54de3ea2010-01-05 06:52:31 +00008362 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8363 SDValue Op0 = Op.getOperand(0);
8364 SDValue Op1 = Op.getOperand(1);
8365 DebugLoc dl = Op.getDebugLoc();
8366 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8367
8368 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008369 // Lower (X & (1 << N)) == 0 to BT(X, N).
8370 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8371 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008372 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008373 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008374 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008375 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8376 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8377 if (NewSetCC.getNode())
8378 return NewSetCC;
8379 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008380
Chris Lattner481eebc2010-12-19 21:23:48 +00008381 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8382 // these.
8383 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008384 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008385 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8386 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008387
Chris Lattner481eebc2010-12-19 21:23:48 +00008388 // If the input is a setcc, then reuse the input setcc or use a new one with
8389 // the inverted condition.
8390 if (Op0.getOpcode() == X86ISD::SETCC) {
8391 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8392 bool Invert = (CC == ISD::SETNE) ^
8393 cast<ConstantSDNode>(Op1)->isNullValue();
8394 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008395
Evan Cheng2c755ba2010-02-27 07:36:59 +00008396 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008397 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8398 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8399 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008400 }
8401
Evan Chenge5b51ac2010-04-17 06:13:15 +00008402 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008403 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008404 if (X86CC == X86::COND_INVALID)
8405 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008406
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008407 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008408 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008409 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008410}
8411
Craig Topper89af15e2011-09-18 08:03:58 +00008412// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008413// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008414static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008415 EVT VT = Op.getValueType();
8416
Duncan Sands28b77e92011-09-06 19:07:46 +00008417 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008418 "Unsupported value type for operation");
8419
8420 int NumElems = VT.getVectorNumElements();
8421 DebugLoc dl = Op.getDebugLoc();
8422 SDValue CC = Op.getOperand(2);
8423 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8424 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8425
8426 // Extract the LHS vectors
8427 SDValue LHS = Op.getOperand(0);
8428 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8429 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8430
8431 // Extract the RHS vectors
8432 SDValue RHS = Op.getOperand(1);
8433 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8434 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8435
8436 // Issue the operation on the smaller types and concatenate the result back
8437 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8438 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8439 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8440 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8441 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8442}
8443
8444
Dan Gohmand858e902010-04-17 15:26:15 +00008445SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008446 SDValue Cond;
8447 SDValue Op0 = Op.getOperand(0);
8448 SDValue Op1 = Op.getOperand(1);
8449 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008450 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008451 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8452 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008453 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008454
8455 if (isFP) {
8456 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008457 EVT EltVT = Op0.getValueType().getVectorElementType();
8458 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8459
8460 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008461 bool Swap = false;
8462
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008463 // SSE Condition code mapping:
8464 // 0 - EQ
8465 // 1 - LT
8466 // 2 - LE
8467 // 3 - UNORD
8468 // 4 - NEQ
8469 // 5 - NLT
8470 // 6 - NLE
8471 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008472 switch (SetCCOpcode) {
8473 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008474 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008475 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008476 case ISD::SETOGT:
8477 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008478 case ISD::SETLT:
8479 case ISD::SETOLT: SSECC = 1; break;
8480 case ISD::SETOGE:
8481 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008482 case ISD::SETLE:
8483 case ISD::SETOLE: SSECC = 2; break;
8484 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008485 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008486 case ISD::SETNE: SSECC = 4; break;
8487 case ISD::SETULE: Swap = true;
8488 case ISD::SETUGE: SSECC = 5; break;
8489 case ISD::SETULT: Swap = true;
8490 case ISD::SETUGT: SSECC = 6; break;
8491 case ISD::SETO: SSECC = 7; break;
8492 }
8493 if (Swap)
8494 std::swap(Op0, Op1);
8495
Nate Begemanfb8ead02008-07-25 19:05:58 +00008496 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008497 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008498 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008499 SDValue UNORD, EQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008500 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8501 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008502 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008503 }
8504 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008505 SDValue ORD, NEQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008506 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8507 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008508 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008509 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008510 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008511 }
8512 // Handle all other FP comparisons here.
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008513 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008514 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008515
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008516 // Break 256-bit integer vector compare into smaller ones.
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008517 if (!isFP && VT.getSizeInBits() == 256)
Craig Topper89af15e2011-09-18 08:03:58 +00008518 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008519
Nate Begeman30a0de92008-07-17 16:51:19 +00008520 // We are handling one of the integer comparisons here. Since SSE only has
8521 // GT and EQ comparisons for integer, swapping operands and multiple
8522 // operations may be required for some comparisons.
8523 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8524 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008525
Owen Anderson825b72b2009-08-11 20:47:22 +00008526 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008527 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008528 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008529 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008530 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8531 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008532 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008533
Nate Begeman30a0de92008-07-17 16:51:19 +00008534 switch (SetCCOpcode) {
8535 default: break;
8536 case ISD::SETNE: Invert = true;
8537 case ISD::SETEQ: Opc = EQOpc; break;
8538 case ISD::SETLT: Swap = true;
8539 case ISD::SETGT: Opc = GTOpc; break;
8540 case ISD::SETGE: Swap = true;
8541 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8542 case ISD::SETULT: Swap = true;
8543 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8544 case ISD::SETUGE: Swap = true;
8545 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8546 }
8547 if (Swap)
8548 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008549
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008550 // Check that the operation in question is available (most are plain SSE2,
8551 // but PCMPGTQ and PCMPEQQ have different requirements).
8552 if (Opc == X86ISD::PCMPGTQ && !Subtarget->hasSSE42() && !Subtarget->hasAVX())
8553 return SDValue();
8554 if (Opc == X86ISD::PCMPEQQ && !Subtarget->hasSSE41() && !Subtarget->hasAVX())
8555 return SDValue();
8556
Nate Begeman30a0de92008-07-17 16:51:19 +00008557 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8558 // bits of the inputs before performing those operations.
8559 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008560 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008561 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8562 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008563 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008564 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8565 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008566 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8567 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008568 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008569
Dale Johannesenace16102009-02-03 19:33:06 +00008570 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008571
8572 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008573 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008574 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008575
Nate Begeman30a0de92008-07-17 16:51:19 +00008576 return Result;
8577}
Evan Cheng0488db92007-09-25 01:57:46 +00008578
Evan Cheng370e5342008-12-03 08:38:43 +00008579// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008580static bool isX86LogicalCmp(SDValue Op) {
8581 unsigned Opc = Op.getNode()->getOpcode();
8582 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8583 return true;
8584 if (Op.getResNo() == 1 &&
8585 (Opc == X86ISD::ADD ||
8586 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008587 Opc == X86ISD::ADC ||
8588 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008589 Opc == X86ISD::SMUL ||
8590 Opc == X86ISD::UMUL ||
8591 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008592 Opc == X86ISD::DEC ||
8593 Opc == X86ISD::OR ||
8594 Opc == X86ISD::XOR ||
8595 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008596 return true;
8597
Chris Lattner9637d5b2010-12-05 07:49:54 +00008598 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8599 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008600
Dan Gohman076aee32009-03-04 19:44:21 +00008601 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008602}
8603
Chris Lattnera2b56002010-12-05 01:23:24 +00008604static bool isZero(SDValue V) {
8605 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8606 return C && C->isNullValue();
8607}
8608
Chris Lattner96908b12010-12-05 02:00:51 +00008609static bool isAllOnes(SDValue V) {
8610 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8611 return C && C->isAllOnesValue();
8612}
8613
Dan Gohmand858e902010-04-17 15:26:15 +00008614SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008615 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008616 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008617 SDValue Op1 = Op.getOperand(1);
8618 SDValue Op2 = Op.getOperand(2);
8619 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008620 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008621
Dan Gohman1a492952009-10-20 16:22:37 +00008622 if (Cond.getOpcode() == ISD::SETCC) {
8623 SDValue NewCond = LowerSETCC(Cond, DAG);
8624 if (NewCond.getNode())
8625 Cond = NewCond;
8626 }
Evan Cheng734503b2006-09-11 02:19:56 +00008627
Chris Lattnera2b56002010-12-05 01:23:24 +00008628 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008629 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008630 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008631 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008632 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008633 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8634 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008635 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008636
Chris Lattnera2b56002010-12-05 01:23:24 +00008637 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008638
8639 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008640 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8641 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008642
8643 SDValue CmpOp0 = Cmp.getOperand(0);
8644 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8645 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008646
Chris Lattner96908b12010-12-05 02:00:51 +00008647 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008648 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8649 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008650
Chris Lattner96908b12010-12-05 02:00:51 +00008651 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8652 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008653
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008654 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008655 if (N2C == 0 || !N2C->isNullValue())
8656 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8657 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008658 }
8659 }
8660
Chris Lattnera2b56002010-12-05 01:23:24 +00008661 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008662 if (Cond.getOpcode() == ISD::AND &&
8663 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8664 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008665 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008666 Cond = Cond.getOperand(0);
8667 }
8668
Evan Cheng3f41d662007-10-08 22:16:29 +00008669 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8670 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008671 if (Cond.getOpcode() == X86ISD::SETCC ||
8672 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008673 CC = Cond.getOperand(0);
8674
Dan Gohman475871a2008-07-27 21:46:04 +00008675 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008676 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008677 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008678
Evan Cheng3f41d662007-10-08 22:16:29 +00008679 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008680 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008681 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008682 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008683
Chris Lattnerd1980a52009-03-12 06:52:53 +00008684 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8685 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008686 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008687 addTest = false;
8688 }
8689 }
8690
8691 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008692 // Look pass the truncate.
8693 if (Cond.getOpcode() == ISD::TRUNCATE)
8694 Cond = Cond.getOperand(0);
8695
8696 // We know the result of AND is compared against zero. Try to match
8697 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008698 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008699 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008700 if (NewSetCC.getNode()) {
8701 CC = NewSetCC.getOperand(0);
8702 Cond = NewSetCC.getOperand(1);
8703 addTest = false;
8704 }
8705 }
8706 }
8707
8708 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008709 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008710 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008711 }
8712
Benjamin Kramere915ff32010-12-22 23:09:28 +00008713 // a < b ? -1 : 0 -> RES = ~setcc_carry
8714 // a < b ? 0 : -1 -> RES = setcc_carry
8715 // a >= b ? -1 : 0 -> RES = setcc_carry
8716 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8717 if (Cond.getOpcode() == X86ISD::CMP) {
8718 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8719
8720 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8721 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8722 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8723 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8724 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8725 return DAG.getNOT(DL, Res, Res.getValueType());
8726 return Res;
8727 }
8728 }
8729
Evan Cheng0488db92007-09-25 01:57:46 +00008730 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8731 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008732 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008733 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008734 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008735}
8736
Evan Cheng370e5342008-12-03 08:38:43 +00008737// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8738// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8739// from the AND / OR.
8740static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8741 Opc = Op.getOpcode();
8742 if (Opc != ISD::OR && Opc != ISD::AND)
8743 return false;
8744 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8745 Op.getOperand(0).hasOneUse() &&
8746 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8747 Op.getOperand(1).hasOneUse());
8748}
8749
Evan Cheng961d6d42009-02-02 08:19:07 +00008750// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8751// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008752static bool isXor1OfSetCC(SDValue Op) {
8753 if (Op.getOpcode() != ISD::XOR)
8754 return false;
8755 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8756 if (N1C && N1C->getAPIntValue() == 1) {
8757 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8758 Op.getOperand(0).hasOneUse();
8759 }
8760 return false;
8761}
8762
Dan Gohmand858e902010-04-17 15:26:15 +00008763SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008764 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008765 SDValue Chain = Op.getOperand(0);
8766 SDValue Cond = Op.getOperand(1);
8767 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008768 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008769 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00008770
Dan Gohman1a492952009-10-20 16:22:37 +00008771 if (Cond.getOpcode() == ISD::SETCC) {
8772 SDValue NewCond = LowerSETCC(Cond, DAG);
8773 if (NewCond.getNode())
8774 Cond = NewCond;
8775 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008776#if 0
8777 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008778 else if (Cond.getOpcode() == X86ISD::ADD ||
8779 Cond.getOpcode() == X86ISD::SUB ||
8780 Cond.getOpcode() == X86ISD::SMUL ||
8781 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008782 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008783#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008784
Evan Chengad9c0a32009-12-15 00:53:42 +00008785 // Look pass (and (setcc_carry (cmp ...)), 1).
8786 if (Cond.getOpcode() == ISD::AND &&
8787 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8788 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008789 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008790 Cond = Cond.getOperand(0);
8791 }
8792
Evan Cheng3f41d662007-10-08 22:16:29 +00008793 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8794 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00008795 if (Cond.getOpcode() == X86ISD::SETCC ||
8796 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008797 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008798
Dan Gohman475871a2008-07-27 21:46:04 +00008799 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008800 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00008801 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00008802 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00008803 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008804 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00008805 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00008806 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008807 default: break;
8808 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00008809 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00008810 // These can only come from an arithmetic instruction with overflow,
8811 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00008812 Cond = Cond.getNode()->getOperand(1);
8813 addTest = false;
8814 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008815 }
Evan Cheng0488db92007-09-25 01:57:46 +00008816 }
Evan Cheng370e5342008-12-03 08:38:43 +00008817 } else {
8818 unsigned CondOpc;
8819 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8820 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00008821 if (CondOpc == ISD::OR) {
8822 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8823 // two branches instead of an explicit OR instruction with a
8824 // separate test.
8825 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008826 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00008827 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008828 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008829 Chain, Dest, CC, Cmp);
8830 CC = Cond.getOperand(1).getOperand(0);
8831 Cond = Cmp;
8832 addTest = false;
8833 }
8834 } else { // ISD::AND
8835 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8836 // two branches instead of an explicit AND instruction with a
8837 // separate test. However, we only do this if this block doesn't
8838 // have a fall-through edge, because this requires an explicit
8839 // jmp when the condition is false.
8840 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00008841 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00008842 Op.getNode()->hasOneUse()) {
8843 X86::CondCode CCode =
8844 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8845 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008846 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00008847 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00008848 // Look for an unconditional branch following this conditional branch.
8849 // We need this because we need to reverse the successors in order
8850 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00008851 if (User->getOpcode() == ISD::BR) {
8852 SDValue FalseBB = User->getOperand(1);
8853 SDNode *NewBR =
8854 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00008855 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00008856 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00008857 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00008858
Dale Johannesene4d209d2009-02-03 20:21:25 +00008859 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00008860 Chain, Dest, CC, Cmp);
8861 X86::CondCode CCode =
8862 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8863 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008864 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00008865 Cond = Cmp;
8866 addTest = false;
8867 }
8868 }
Dan Gohman279c22e2008-10-21 03:29:32 +00008869 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00008870 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8871 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8872 // It should be transformed during dag combiner except when the condition
8873 // is set by a arithmetics with overflow node.
8874 X86::CondCode CCode =
8875 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8876 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00008877 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00008878 Cond = Cond.getOperand(0).getOperand(1);
8879 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00008880 }
Evan Cheng0488db92007-09-25 01:57:46 +00008881 }
8882
8883 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008884 // Look pass the truncate.
8885 if (Cond.getOpcode() == ISD::TRUNCATE)
8886 Cond = Cond.getOperand(0);
8887
8888 // We know the result of AND is compared against zero. Try to match
8889 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008890 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008891 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8892 if (NewSetCC.getNode()) {
8893 CC = NewSetCC.getOperand(0);
8894 Cond = NewSetCC.getOperand(1);
8895 addTest = false;
8896 }
8897 }
8898 }
8899
8900 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008901 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008902 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008903 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008904 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00008905 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00008906}
8907
Anton Korobeynikove060b532007-04-17 19:34:00 +00008908
8909// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8910// Calls to _alloca is needed to probe the stack when allocating more than 4k
8911// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8912// that the guard pages used by the OS virtual memory manager are allocated in
8913// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00008914SDValue
8915X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008916 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008917 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
8918 EnableSegmentedStacks) &&
8919 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00008920 "are being used");
8921 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008922 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008923
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008924 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008925 SDValue Chain = Op.getOperand(0);
8926 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008927 // FIXME: Ensure alignment here
8928
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008929 bool Is64Bit = Subtarget->is64Bit();
8930 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008931
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008932 if (EnableSegmentedStacks) {
8933 MachineFunction &MF = DAG.getMachineFunction();
8934 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008935
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008936 if (Is64Bit) {
8937 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00008938 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008939 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008940
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008941 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
8942 I != E; I++)
8943 if (I->hasNestAttr())
8944 report_fatal_error("Cannot use segmented stacks with functions that "
8945 "have nested arguments.");
8946 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008947
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008948 const TargetRegisterClass *AddrRegClass =
8949 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
8950 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
8951 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
8952 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
8953 DAG.getRegister(Vreg, SPTy));
8954 SDValue Ops1[2] = { Value, Chain };
8955 return DAG.getMergeValues(Ops1, 2, dl);
8956 } else {
8957 SDValue Flag;
8958 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008959
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008960 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
8961 Flag = Chain.getValue(1);
8962 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008963
Rafael Espindola151ab3e2011-08-30 19:47:04 +00008964 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
8965 Flag = Chain.getValue(1);
8966
8967 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
8968
8969 SDValue Ops1[2] = { Chain.getValue(0), Chain };
8970 return DAG.getMergeValues(Ops1, 2, dl);
8971 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008972}
8973
Dan Gohmand858e902010-04-17 15:26:15 +00008974SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008975 MachineFunction &MF = DAG.getMachineFunction();
8976 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8977
Dan Gohman69de1932008-02-06 22:27:42 +00008978 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008979 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008980
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008981 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008982 // vastart just stores the address of the VarArgsFrameIndex slot into the
8983 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008984 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8985 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008986 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8987 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008988 }
8989
8990 // __va_list_tag:
8991 // gp_offset (0 - 6 * 8)
8992 // fp_offset (48 - 48 + 8 * 16)
8993 // overflow_arg_area (point to parameters coming in memory).
8994 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008995 SmallVector<SDValue, 8> MemOps;
8996 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008997 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008998 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008999 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9000 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009001 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009002 MemOps.push_back(Store);
9003
9004 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009005 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009006 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009007 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009008 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9009 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009010 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009011 MemOps.push_back(Store);
9012
9013 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009014 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009015 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009016 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9017 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009018 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9019 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009020 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009021 MemOps.push_back(Store);
9022
9023 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009024 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009025 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009026 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9027 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009028 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9029 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009030 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009031 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009032 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009033}
9034
Dan Gohmand858e902010-04-17 15:26:15 +00009035SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009036 assert(Subtarget->is64Bit() &&
9037 "LowerVAARG only handles 64-bit va_arg!");
9038 assert((Subtarget->isTargetLinux() ||
9039 Subtarget->isTargetDarwin()) &&
9040 "Unhandled target in LowerVAARG");
9041 assert(Op.getNode()->getNumOperands() == 4);
9042 SDValue Chain = Op.getOperand(0);
9043 SDValue SrcPtr = Op.getOperand(1);
9044 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9045 unsigned Align = Op.getConstantOperandVal(3);
9046 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009047
Dan Gohman320afb82010-10-12 18:00:49 +00009048 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009049 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009050 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9051 uint8_t ArgMode;
9052
9053 // Decide which area this value should be read from.
9054 // TODO: Implement the AMD64 ABI in its entirety. This simple
9055 // selection mechanism works only for the basic types.
9056 if (ArgVT == MVT::f80) {
9057 llvm_unreachable("va_arg for f80 not yet implemented");
9058 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9059 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9060 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9061 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9062 } else {
9063 llvm_unreachable("Unhandled argument type in LowerVAARG");
9064 }
9065
9066 if (ArgMode == 2) {
9067 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00009068 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009069 !(DAG.getMachineFunction()
9070 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009071 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009072 }
9073
9074 // Insert VAARG_64 node into the DAG
9075 // VAARG_64 returns two values: Variable Argument Address, Chain
9076 SmallVector<SDValue, 11> InstOps;
9077 InstOps.push_back(Chain);
9078 InstOps.push_back(SrcPtr);
9079 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9080 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9081 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9082 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9083 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9084 VTs, &InstOps[0], InstOps.size(),
9085 MVT::i64,
9086 MachinePointerInfo(SV),
9087 /*Align=*/0,
9088 /*Volatile=*/false,
9089 /*ReadMem=*/true,
9090 /*WriteMem=*/true);
9091 Chain = VAARG.getValue(1);
9092
9093 // Load the next argument and return it
9094 return DAG.getLoad(ArgVT, dl,
9095 Chain,
9096 VAARG,
9097 MachinePointerInfo(),
9098 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009099}
9100
Dan Gohmand858e902010-04-17 15:26:15 +00009101SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009102 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009103 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009104 SDValue Chain = Op.getOperand(0);
9105 SDValue DstPtr = Op.getOperand(1);
9106 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009107 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9108 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009109 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009110
Chris Lattnere72f2022010-09-21 05:40:29 +00009111 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009112 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009113 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009114 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009115}
9116
Dan Gohman475871a2008-07-27 21:46:04 +00009117SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009118X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009119 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009120 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009121 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009122 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009123 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009124 case Intrinsic::x86_sse_comieq_ss:
9125 case Intrinsic::x86_sse_comilt_ss:
9126 case Intrinsic::x86_sse_comile_ss:
9127 case Intrinsic::x86_sse_comigt_ss:
9128 case Intrinsic::x86_sse_comige_ss:
9129 case Intrinsic::x86_sse_comineq_ss:
9130 case Intrinsic::x86_sse_ucomieq_ss:
9131 case Intrinsic::x86_sse_ucomilt_ss:
9132 case Intrinsic::x86_sse_ucomile_ss:
9133 case Intrinsic::x86_sse_ucomigt_ss:
9134 case Intrinsic::x86_sse_ucomige_ss:
9135 case Intrinsic::x86_sse_ucomineq_ss:
9136 case Intrinsic::x86_sse2_comieq_sd:
9137 case Intrinsic::x86_sse2_comilt_sd:
9138 case Intrinsic::x86_sse2_comile_sd:
9139 case Intrinsic::x86_sse2_comigt_sd:
9140 case Intrinsic::x86_sse2_comige_sd:
9141 case Intrinsic::x86_sse2_comineq_sd:
9142 case Intrinsic::x86_sse2_ucomieq_sd:
9143 case Intrinsic::x86_sse2_ucomilt_sd:
9144 case Intrinsic::x86_sse2_ucomile_sd:
9145 case Intrinsic::x86_sse2_ucomigt_sd:
9146 case Intrinsic::x86_sse2_ucomige_sd:
9147 case Intrinsic::x86_sse2_ucomineq_sd: {
9148 unsigned Opc = 0;
9149 ISD::CondCode CC = ISD::SETCC_INVALID;
9150 switch (IntNo) {
9151 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009152 case Intrinsic::x86_sse_comieq_ss:
9153 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009154 Opc = X86ISD::COMI;
9155 CC = ISD::SETEQ;
9156 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009157 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009158 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009159 Opc = X86ISD::COMI;
9160 CC = ISD::SETLT;
9161 break;
9162 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009163 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009164 Opc = X86ISD::COMI;
9165 CC = ISD::SETLE;
9166 break;
9167 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009168 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009169 Opc = X86ISD::COMI;
9170 CC = ISD::SETGT;
9171 break;
9172 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009173 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009174 Opc = X86ISD::COMI;
9175 CC = ISD::SETGE;
9176 break;
9177 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009178 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009179 Opc = X86ISD::COMI;
9180 CC = ISD::SETNE;
9181 break;
9182 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009183 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009184 Opc = X86ISD::UCOMI;
9185 CC = ISD::SETEQ;
9186 break;
9187 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009188 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009189 Opc = X86ISD::UCOMI;
9190 CC = ISD::SETLT;
9191 break;
9192 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009193 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009194 Opc = X86ISD::UCOMI;
9195 CC = ISD::SETLE;
9196 break;
9197 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009198 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009199 Opc = X86ISD::UCOMI;
9200 CC = ISD::SETGT;
9201 break;
9202 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009203 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009204 Opc = X86ISD::UCOMI;
9205 CC = ISD::SETGE;
9206 break;
9207 case Intrinsic::x86_sse_ucomineq_ss:
9208 case Intrinsic::x86_sse2_ucomineq_sd:
9209 Opc = X86ISD::UCOMI;
9210 CC = ISD::SETNE;
9211 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009212 }
Evan Cheng734503b2006-09-11 02:19:56 +00009213
Dan Gohman475871a2008-07-27 21:46:04 +00009214 SDValue LHS = Op.getOperand(1);
9215 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009216 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009217 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009218 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9219 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9220 DAG.getConstant(X86CC, MVT::i8), Cond);
9221 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009222 }
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009223 // Arithmetic intrinsics.
9224 case Intrinsic::x86_sse3_hadd_ps:
9225 case Intrinsic::x86_sse3_hadd_pd:
9226 case Intrinsic::x86_avx_hadd_ps_256:
9227 case Intrinsic::x86_avx_hadd_pd_256:
9228 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9229 Op.getOperand(1), Op.getOperand(2));
9230 case Intrinsic::x86_sse3_hsub_ps:
9231 case Intrinsic::x86_sse3_hsub_pd:
9232 case Intrinsic::x86_avx_hsub_ps_256:
9233 case Intrinsic::x86_avx_hsub_pd_256:
9234 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9235 Op.getOperand(1), Op.getOperand(2));
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009236 // ptest and testp intrinsics. The intrinsic these come from are designed to
9237 // return an integer value, not just an instruction so lower it to the ptest
9238 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009239 case Intrinsic::x86_sse41_ptestz:
9240 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009241 case Intrinsic::x86_sse41_ptestnzc:
9242 case Intrinsic::x86_avx_ptestz_256:
9243 case Intrinsic::x86_avx_ptestc_256:
9244 case Intrinsic::x86_avx_ptestnzc_256:
9245 case Intrinsic::x86_avx_vtestz_ps:
9246 case Intrinsic::x86_avx_vtestc_ps:
9247 case Intrinsic::x86_avx_vtestnzc_ps:
9248 case Intrinsic::x86_avx_vtestz_pd:
9249 case Intrinsic::x86_avx_vtestc_pd:
9250 case Intrinsic::x86_avx_vtestnzc_pd:
9251 case Intrinsic::x86_avx_vtestz_ps_256:
9252 case Intrinsic::x86_avx_vtestc_ps_256:
9253 case Intrinsic::x86_avx_vtestnzc_ps_256:
9254 case Intrinsic::x86_avx_vtestz_pd_256:
9255 case Intrinsic::x86_avx_vtestc_pd_256:
9256 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9257 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009258 unsigned X86CC = 0;
9259 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009260 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009261 case Intrinsic::x86_avx_vtestz_ps:
9262 case Intrinsic::x86_avx_vtestz_pd:
9263 case Intrinsic::x86_avx_vtestz_ps_256:
9264 case Intrinsic::x86_avx_vtestz_pd_256:
9265 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009266 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009267 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009268 // ZF = 1
9269 X86CC = X86::COND_E;
9270 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009271 case Intrinsic::x86_avx_vtestc_ps:
9272 case Intrinsic::x86_avx_vtestc_pd:
9273 case Intrinsic::x86_avx_vtestc_ps_256:
9274 case Intrinsic::x86_avx_vtestc_pd_256:
9275 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009276 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009277 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009278 // CF = 1
9279 X86CC = X86::COND_B;
9280 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009281 case Intrinsic::x86_avx_vtestnzc_ps:
9282 case Intrinsic::x86_avx_vtestnzc_pd:
9283 case Intrinsic::x86_avx_vtestnzc_ps_256:
9284 case Intrinsic::x86_avx_vtestnzc_pd_256:
9285 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009286 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009287 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009288 // ZF and CF = 0
9289 X86CC = X86::COND_A;
9290 break;
9291 }
Eric Christopherfd179292009-08-27 18:07:15 +00009292
Eric Christopher71c67532009-07-29 00:28:05 +00009293 SDValue LHS = Op.getOperand(1);
9294 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009295 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9296 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009297 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9298 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9299 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009300 }
Evan Cheng5759f972008-05-04 09:15:50 +00009301
9302 // Fix vector shift instructions where the last operand is a non-immediate
9303 // i32 value.
9304 case Intrinsic::x86_sse2_pslli_w:
9305 case Intrinsic::x86_sse2_pslli_d:
9306 case Intrinsic::x86_sse2_pslli_q:
9307 case Intrinsic::x86_sse2_psrli_w:
9308 case Intrinsic::x86_sse2_psrli_d:
9309 case Intrinsic::x86_sse2_psrli_q:
9310 case Intrinsic::x86_sse2_psrai_w:
9311 case Intrinsic::x86_sse2_psrai_d:
9312 case Intrinsic::x86_mmx_pslli_w:
9313 case Intrinsic::x86_mmx_pslli_d:
9314 case Intrinsic::x86_mmx_pslli_q:
9315 case Intrinsic::x86_mmx_psrli_w:
9316 case Intrinsic::x86_mmx_psrli_d:
9317 case Intrinsic::x86_mmx_psrli_q:
9318 case Intrinsic::x86_mmx_psrai_w:
9319 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009320 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009321 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009322 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009323
9324 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009325 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009326 switch (IntNo) {
9327 case Intrinsic::x86_sse2_pslli_w:
9328 NewIntNo = Intrinsic::x86_sse2_psll_w;
9329 break;
9330 case Intrinsic::x86_sse2_pslli_d:
9331 NewIntNo = Intrinsic::x86_sse2_psll_d;
9332 break;
9333 case Intrinsic::x86_sse2_pslli_q:
9334 NewIntNo = Intrinsic::x86_sse2_psll_q;
9335 break;
9336 case Intrinsic::x86_sse2_psrli_w:
9337 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9338 break;
9339 case Intrinsic::x86_sse2_psrli_d:
9340 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9341 break;
9342 case Intrinsic::x86_sse2_psrli_q:
9343 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9344 break;
9345 case Intrinsic::x86_sse2_psrai_w:
9346 NewIntNo = Intrinsic::x86_sse2_psra_w;
9347 break;
9348 case Intrinsic::x86_sse2_psrai_d:
9349 NewIntNo = Intrinsic::x86_sse2_psra_d;
9350 break;
9351 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009352 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009353 switch (IntNo) {
9354 case Intrinsic::x86_mmx_pslli_w:
9355 NewIntNo = Intrinsic::x86_mmx_psll_w;
9356 break;
9357 case Intrinsic::x86_mmx_pslli_d:
9358 NewIntNo = Intrinsic::x86_mmx_psll_d;
9359 break;
9360 case Intrinsic::x86_mmx_pslli_q:
9361 NewIntNo = Intrinsic::x86_mmx_psll_q;
9362 break;
9363 case Intrinsic::x86_mmx_psrli_w:
9364 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9365 break;
9366 case Intrinsic::x86_mmx_psrli_d:
9367 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9368 break;
9369 case Intrinsic::x86_mmx_psrli_q:
9370 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9371 break;
9372 case Intrinsic::x86_mmx_psrai_w:
9373 NewIntNo = Intrinsic::x86_mmx_psra_w;
9374 break;
9375 case Intrinsic::x86_mmx_psrai_d:
9376 NewIntNo = Intrinsic::x86_mmx_psra_d;
9377 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009378 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009379 }
9380 break;
9381 }
9382 }
Mon P Wangefa42202009-09-03 19:56:25 +00009383
9384 // The vector shift intrinsics with scalars uses 32b shift amounts but
9385 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9386 // to be zero.
9387 SDValue ShOps[4];
9388 ShOps[0] = ShAmt;
9389 ShOps[1] = DAG.getConstant(0, MVT::i32);
9390 if (ShAmtVT == MVT::v4i32) {
9391 ShOps[2] = DAG.getUNDEF(MVT::i32);
9392 ShOps[3] = DAG.getUNDEF(MVT::i32);
9393 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9394 } else {
9395 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009396// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009397 }
9398
Owen Andersone50ed302009-08-10 22:56:29 +00009399 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009400 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009401 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009402 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009403 Op.getOperand(1), ShAmt);
9404 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009405 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009406}
Evan Cheng72261582005-12-20 06:22:03 +00009407
Dan Gohmand858e902010-04-17 15:26:15 +00009408SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9409 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009410 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9411 MFI->setReturnAddressIsTaken(true);
9412
Bill Wendling64e87322009-01-16 19:25:27 +00009413 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009414 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009415
9416 if (Depth > 0) {
9417 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9418 SDValue Offset =
9419 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009420 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009421 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009422 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009423 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00009424 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009425 }
9426
9427 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009428 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009429 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00009430 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009431}
9432
Dan Gohmand858e902010-04-17 15:26:15 +00009433SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009434 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9435 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009436
Owen Andersone50ed302009-08-10 22:56:29 +00009437 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009438 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009439 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9440 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009441 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009442 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009443 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9444 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00009445 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009446 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009447}
9448
Dan Gohman475871a2008-07-27 21:46:04 +00009449SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009450 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009451 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009452}
9453
Dan Gohmand858e902010-04-17 15:26:15 +00009454SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009455 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009456 SDValue Chain = Op.getOperand(0);
9457 SDValue Offset = Op.getOperand(1);
9458 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009459 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009460
Dan Gohmand8816272010-08-11 18:14:00 +00009461 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9462 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9463 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009464 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009465
Dan Gohmand8816272010-08-11 18:14:00 +00009466 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9467 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009468 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009469 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9470 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009471 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009472 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009473
Dale Johannesene4d209d2009-02-03 20:21:25 +00009474 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009475 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009476 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009477}
9478
Duncan Sands4a544a72011-09-06 13:37:06 +00009479SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9480 SelectionDAG &DAG) const {
9481 return Op.getOperand(0);
9482}
9483
9484SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9485 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009486 SDValue Root = Op.getOperand(0);
9487 SDValue Trmp = Op.getOperand(1); // trampoline
9488 SDValue FPtr = Op.getOperand(2); // nested function
9489 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009490 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009491
Dan Gohman69de1932008-02-06 22:27:42 +00009492 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009493
9494 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009495 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009496
9497 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009498 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9499 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009500
Evan Cheng0e6a0522011-07-18 20:57:22 +00009501 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9502 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009503
9504 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9505
9506 // Load the pointer to the nested function into R11.
9507 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009508 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009509 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009510 Addr, MachinePointerInfo(TrmpAddr),
9511 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009512
Owen Anderson825b72b2009-08-11 20:47:22 +00009513 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9514 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009515 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9516 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009517 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009518
9519 // Load the 'nest' parameter value into R10.
9520 // R10 is specified in X86CallingConv.td
9521 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009522 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9523 DAG.getConstant(10, MVT::i64));
9524 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009525 Addr, MachinePointerInfo(TrmpAddr, 10),
9526 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009527
Owen Anderson825b72b2009-08-11 20:47:22 +00009528 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9529 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009530 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9531 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009532 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009533
9534 // Jump to the nested function.
9535 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009536 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9537 DAG.getConstant(20, MVT::i64));
9538 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009539 Addr, MachinePointerInfo(TrmpAddr, 20),
9540 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009541
9542 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009543 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9544 DAG.getConstant(22, MVT::i64));
9545 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009546 MachinePointerInfo(TrmpAddr, 22),
9547 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009548
Duncan Sands4a544a72011-09-06 13:37:06 +00009549 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009550 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009551 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009552 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009553 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009554 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009555
9556 switch (CC) {
9557 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009558 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009559 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009560 case CallingConv::X86_StdCall: {
9561 // Pass 'nest' parameter in ECX.
9562 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009563 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009564
9565 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009566 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009567 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009568
Chris Lattner58d74912008-03-12 17:45:29 +00009569 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009570 unsigned InRegCount = 0;
9571 unsigned Idx = 1;
9572
9573 for (FunctionType::param_iterator I = FTy->param_begin(),
9574 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009575 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009576 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009577 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009578
9579 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009580 report_fatal_error("Nest register in use - reduce number of inreg"
9581 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009582 }
9583 }
9584 break;
9585 }
9586 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009587 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009588 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009589 // Pass 'nest' parameter in EAX.
9590 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009591 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009592 break;
9593 }
9594
Dan Gohman475871a2008-07-27 21:46:04 +00009595 SDValue OutChains[4];
9596 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009597
Owen Anderson825b72b2009-08-11 20:47:22 +00009598 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9599 DAG.getConstant(10, MVT::i32));
9600 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009601
Chris Lattnera62fe662010-02-05 19:20:30 +00009602 // This is storing the opcode for MOV32ri.
9603 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009604 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009605 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009606 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009607 Trmp, MachinePointerInfo(TrmpAddr),
9608 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009609
Owen Anderson825b72b2009-08-11 20:47:22 +00009610 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9611 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009612 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9613 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009614 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009615
Chris Lattnera62fe662010-02-05 19:20:30 +00009616 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00009617 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9618 DAG.getConstant(5, MVT::i32));
9619 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009620 MachinePointerInfo(TrmpAddr, 5),
9621 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009622
Owen Anderson825b72b2009-08-11 20:47:22 +00009623 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9624 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009625 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
9626 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00009627 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009628
Duncan Sands4a544a72011-09-06 13:37:06 +00009629 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009630 }
9631}
9632
Dan Gohmand858e902010-04-17 15:26:15 +00009633SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9634 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009635 /*
9636 The rounding mode is in bits 11:10 of FPSR, and has the following
9637 settings:
9638 00 Round to nearest
9639 01 Round to -inf
9640 10 Round to +inf
9641 11 Round to 0
9642
9643 FLT_ROUNDS, on the other hand, expects the following:
9644 -1 Undefined
9645 0 Round to 0
9646 1 Round to nearest
9647 2 Round to +inf
9648 3 Round to -inf
9649
9650 To perform the conversion, we do:
9651 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9652 */
9653
9654 MachineFunction &MF = DAG.getMachineFunction();
9655 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00009656 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009657 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009658 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00009659 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009660
9661 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00009662 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00009663 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009664
Michael J. Spencerec38de22010-10-10 22:04:20 +00009665
Chris Lattner2156b792010-09-22 01:11:26 +00009666 MachineMemOperand *MMO =
9667 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
9668 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009669
Chris Lattner2156b792010-09-22 01:11:26 +00009670 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
9671 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
9672 DAG.getVTList(MVT::Other),
9673 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009674
9675 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00009676 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00009677 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009678
9679 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00009680 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00009681 DAG.getNode(ISD::SRL, DL, MVT::i16,
9682 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009683 CWD, DAG.getConstant(0x800, MVT::i16)),
9684 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00009685 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00009686 DAG.getNode(ISD::SRL, DL, MVT::i16,
9687 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00009688 CWD, DAG.getConstant(0x400, MVT::i16)),
9689 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009690
Dan Gohman475871a2008-07-27 21:46:04 +00009691 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00009692 DAG.getNode(ISD::AND, DL, MVT::i16,
9693 DAG.getNode(ISD::ADD, DL, MVT::i16,
9694 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009695 DAG.getConstant(1, MVT::i16)),
9696 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009697
9698
Duncan Sands83ec4b62008-06-06 12:08:01 +00009699 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00009700 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009701}
9702
Dan Gohmand858e902010-04-17 15:26:15 +00009703SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009704 EVT VT = Op.getValueType();
9705 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009706 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009707 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009708
9709 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009710 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00009711 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00009712 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009713 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009714 }
Evan Cheng18efe262007-12-14 02:13:44 +00009715
Evan Cheng152804e2007-12-14 08:30:15 +00009716 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009717 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009718 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009719
9720 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009721 SDValue Ops[] = {
9722 Op,
9723 DAG.getConstant(NumBits+NumBits-1, OpVT),
9724 DAG.getConstant(X86::COND_E, MVT::i8),
9725 Op.getValue(1)
9726 };
9727 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009728
9729 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00009730 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00009731
Owen Anderson825b72b2009-08-11 20:47:22 +00009732 if (VT == MVT::i8)
9733 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009734 return Op;
9735}
9736
Dan Gohmand858e902010-04-17 15:26:15 +00009737SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009738 EVT VT = Op.getValueType();
9739 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009740 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009741 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00009742
9743 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009744 if (VT == MVT::i8) {
9745 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00009746 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009747 }
Evan Cheng152804e2007-12-14 08:30:15 +00009748
9749 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009750 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009751 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00009752
9753 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00009754 SDValue Ops[] = {
9755 Op,
9756 DAG.getConstant(NumBits, OpVT),
9757 DAG.getConstant(X86::COND_E, MVT::i8),
9758 Op.getValue(1)
9759 };
9760 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00009761
Owen Anderson825b72b2009-08-11 20:47:22 +00009762 if (VT == MVT::i8)
9763 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00009764 return Op;
9765}
9766
Craig Topper13894fa2011-08-24 06:14:18 +00009767// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
9768// ones, and then concatenate the result back.
9769static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00009770 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +00009771
9772 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
9773 "Unsupported value type for operation");
9774
9775 int NumElems = VT.getVectorNumElements();
9776 DebugLoc dl = Op.getDebugLoc();
9777 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
9778 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
9779
9780 // Extract the LHS vectors
9781 SDValue LHS = Op.getOperand(0);
9782 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
9783 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
9784
9785 // Extract the RHS vectors
9786 SDValue RHS = Op.getOperand(1);
9787 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
9788 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
9789
9790 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9791 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9792
9793 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9794 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
9795 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
9796}
9797
9798SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
9799 assert(Op.getValueType().getSizeInBits() == 256 &&
9800 Op.getValueType().isInteger() &&
9801 "Only handle AVX 256-bit vector integer operation");
9802 return Lower256IntArith(Op, DAG);
9803}
9804
9805SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
9806 assert(Op.getValueType().getSizeInBits() == 256 &&
9807 Op.getValueType().isInteger() &&
9808 "Only handle AVX 256-bit vector integer operation");
9809 return Lower256IntArith(Op, DAG);
9810}
9811
9812SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
9813 EVT VT = Op.getValueType();
9814
9815 // Decompose 256-bit ops into smaller 128-bit ops.
9816 if (VT.getSizeInBits() == 256)
9817 return Lower256IntArith(Op, DAG);
9818
Owen Anderson825b72b2009-08-11 20:47:22 +00009819 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009820 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00009821
Mon P Wangaf9b9522008-12-18 21:42:19 +00009822 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
9823 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
9824 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
9825 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
9826 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
9827 //
9828 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
9829 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
9830 // return AloBlo + AloBhi + AhiBlo;
9831
9832 SDValue A = Op.getOperand(0);
9833 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009834
Dale Johannesene4d209d2009-02-03 20:21:25 +00009835 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009836 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9837 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009838 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009839 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9840 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009841 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009842 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009843 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009844 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009845 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009846 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009847 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009848 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00009849 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009850 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009851 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9852 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009853 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009854 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9855 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009856 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9857 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009858 return Res;
9859}
9860
Nadav Rotem43012222011-05-11 08:12:09 +00009861SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
9862
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009863 EVT VT = Op.getValueType();
9864 DebugLoc dl = Op.getDebugLoc();
9865 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00009866 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009867 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009868
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00009869 if (!Subtarget->hasXMMInt())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009870 return SDValue();
9871
9872 // Decompose 256-bit shifts into smaller 128-bit shifts.
9873 if (VT.getSizeInBits() == 256) {
9874 int NumElems = VT.getVectorNumElements();
9875 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9876 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9877
9878 // Extract the two vectors
9879 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
9880 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
9881 DAG, dl);
9882
9883 // Recreate the shift amount vectors
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009884 SDValue Amt1, Amt2;
9885 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
9886 // Constant shift amount
9887 SmallVector<SDValue, 4> Amt1Csts;
9888 SmallVector<SDValue, 4> Amt2Csts;
9889 for (int i = 0; i < NumElems/2; ++i)
9890 Amt1Csts.push_back(Amt->getOperand(i));
9891 for (int i = NumElems/2; i < NumElems; ++i)
9892 Amt2Csts.push_back(Amt->getOperand(i));
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009893
Bruno Cardoso Lopes0dd80b02011-08-17 22:12:20 +00009894 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9895 &Amt1Csts[0], NumElems/2);
9896 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
9897 &Amt2Csts[0], NumElems/2);
9898 } else {
9899 // Variable shift amount
9900 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
9901 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
9902 DAG, dl);
9903 }
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00009904
9905 // Issue new vector shifts for the smaller types
9906 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
9907 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
9908
9909 // Concatenate the result back
9910 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
9911 }
Nate Begeman51409212010-07-28 00:21:48 +00009912
Nadav Rotem43012222011-05-11 08:12:09 +00009913 // Optimize shl/srl/sra with constant shift amount.
9914 if (isSplatVector(Amt.getNode())) {
9915 SDValue SclrAmt = Amt->getOperand(0);
9916 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
9917 uint64_t ShiftAmt = C->getZExtValue();
9918
9919 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
9920 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9921 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9922 R, DAG.getConstant(ShiftAmt, MVT::i32));
9923
9924 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
9925 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9926 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9927 R, DAG.getConstant(ShiftAmt, MVT::i32));
9928
9929 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
9930 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9931 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9932 R, DAG.getConstant(ShiftAmt, MVT::i32));
9933
9934 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
9935 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9936 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9937 R, DAG.getConstant(ShiftAmt, MVT::i32));
9938
9939 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
9940 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9941 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
9942 R, DAG.getConstant(ShiftAmt, MVT::i32));
9943
9944 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
9945 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9946 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
9947 R, DAG.getConstant(ShiftAmt, MVT::i32));
9948
9949 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
9950 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9951 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
9952 R, DAG.getConstant(ShiftAmt, MVT::i32));
9953
9954 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
9955 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9956 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
9957 R, DAG.getConstant(ShiftAmt, MVT::i32));
9958 }
9959 }
9960
9961 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00009962 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009963 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9964 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9965 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
9966
9967 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009968
Nate Begeman51409212010-07-28 00:21:48 +00009969 std::vector<Constant*> CV(4, CI);
9970 Constant *C = ConstantVector::get(CV);
9971 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9972 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009973 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009974 false, false, 16);
9975
9976 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009977 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00009978 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
9979 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
9980 }
Nadav Rotem43012222011-05-11 08:12:09 +00009981 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00009982 // a = a << 5;
9983 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9984 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9985 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
9986
9987 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
9988 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
9989
9990 std::vector<Constant*> CVM1(16, CM1);
9991 std::vector<Constant*> CVM2(16, CM2);
9992 Constant *C = ConstantVector::get(CVM1);
9993 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9994 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009995 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00009996 false, false, 16);
9997
9998 // r = pblendv(r, psllw(r & (char16)15, 4), a);
9999 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10000 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10001 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10002 DAG.getConstant(4, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010003 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010004 // a += a
10005 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010006
Nate Begeman51409212010-07-28 00:21:48 +000010007 C = ConstantVector::get(CVM2);
10008 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10009 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010010 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010011 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010012
Nate Begeman51409212010-07-28 00:21:48 +000010013 // r = pblendv(r, psllw(r & (char16)63, 2), a);
10014 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10015 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10016 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10017 DAG.getConstant(2, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010018 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010019 // a += a
10020 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010021
Nate Begeman51409212010-07-28 00:21:48 +000010022 // return pblendv(r, r+r, a);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010023 R = DAG.getNode(ISD::VSELECT, dl, VT, Op,
10024 R, DAG.getNode(ISD::ADD, dl, VT, R, R));
Nate Begeman51409212010-07-28 00:21:48 +000010025 return R;
10026 }
10027 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010028}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010029
Dan Gohmand858e902010-04-17 15:26:15 +000010030SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010031 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10032 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010033 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10034 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010035 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010036 SDValue LHS = N->getOperand(0);
10037 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010038 unsigned BaseOp = 0;
10039 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010040 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010041 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010042 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010043 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010044 // A subtract of one will be selected as a INC. Note that INC doesn't
10045 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010046 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10047 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010048 BaseOp = X86ISD::INC;
10049 Cond = X86::COND_O;
10050 break;
10051 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010052 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010053 Cond = X86::COND_O;
10054 break;
10055 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010056 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010057 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010058 break;
10059 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010060 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10061 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010062 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10063 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010064 BaseOp = X86ISD::DEC;
10065 Cond = X86::COND_O;
10066 break;
10067 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010068 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010069 Cond = X86::COND_O;
10070 break;
10071 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010072 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010073 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010074 break;
10075 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010076 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010077 Cond = X86::COND_O;
10078 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010079 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10080 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10081 MVT::i32);
10082 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010083
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010084 SDValue SetCC =
10085 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10086 DAG.getConstant(X86::COND_O, MVT::i32),
10087 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010088
Dan Gohman6e5fda22011-07-22 18:45:15 +000010089 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010090 }
Bill Wendling74c37652008-12-09 22:08:41 +000010091 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010092
Bill Wendling61edeb52008-12-02 01:06:39 +000010093 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010094 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010095 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010096
Bill Wendling61edeb52008-12-02 01:06:39 +000010097 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010098 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10099 DAG.getConstant(Cond, MVT::i32),
10100 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010101
Dan Gohman6e5fda22011-07-22 18:45:15 +000010102 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010103}
10104
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010105SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10106 DebugLoc dl = Op.getDebugLoc();
10107 SDNode* Node = Op.getNode();
10108 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
10109 EVT VT = Node->getValueType(0);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010110 if (Subtarget->hasXMMInt() && VT.isVector()) {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010111 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10112 ExtraVT.getScalarType().getSizeInBits();
10113 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10114
10115 unsigned SHLIntrinsicsID = 0;
10116 unsigned SRAIntrinsicsID = 0;
10117 switch (VT.getSimpleVT().SimpleTy) {
10118 default:
10119 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010120 case MVT::v4i32: {
10121 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10122 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10123 break;
10124 }
10125 case MVT::v8i16: {
10126 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10127 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10128 break;
10129 }
10130 }
10131
10132 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10133 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
10134 Node->getOperand(0), ShAmt);
10135
10136 // In case of 1 bit sext, no need to shr
10137 if (ExtraVT.getScalarType().getSizeInBits() == 1) return Tmp1;
10138
Nadav Rotema7934dd2011-10-10 19:31:45 +000010139 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10140 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10141 Tmp1, ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010142 }
10143
10144 return SDValue();
10145}
10146
10147
Eric Christopher9a9d2752010-07-22 02:48:34 +000010148SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10149 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010150
Eric Christopher77ed1352011-07-08 00:04:56 +000010151 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10152 // There isn't any reason to disable it if the target processor supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010153 if (!Subtarget->hasXMMInt() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010154 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010155 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010156 SDValue Ops[] = {
10157 DAG.getRegister(X86::ESP, MVT::i32), // Base
10158 DAG.getTargetConstant(1, MVT::i8), // Scale
10159 DAG.getRegister(0, MVT::i32), // Index
10160 DAG.getTargetConstant(0, MVT::i32), // Disp
10161 DAG.getRegister(0, MVT::i32), // Segment.
10162 Zero,
10163 Chain
10164 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010165 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010166 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10167 array_lengthof(Ops));
10168 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010169 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010170
Eric Christopher9a9d2752010-07-22 02:48:34 +000010171 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010172 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010173 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010174
Chris Lattner132929a2010-08-14 17:26:09 +000010175 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10176 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10177 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10178 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010179
Chris Lattner132929a2010-08-14 17:26:09 +000010180 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10181 if (!Op1 && !Op2 && !Op3 && Op4)
10182 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010183
Chris Lattner132929a2010-08-14 17:26:09 +000010184 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10185 if (Op1 && !Op2 && !Op3 && !Op4)
10186 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010187
10188 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010189 // (MFENCE)>;
10190 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010191}
10192
Eli Friedman14648462011-07-27 22:21:52 +000010193SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10194 SelectionDAG &DAG) const {
10195 DebugLoc dl = Op.getDebugLoc();
10196 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10197 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10198 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10199 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10200
10201 // The only fence that needs an instruction is a sequentially-consistent
10202 // cross-thread fence.
10203 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10204 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10205 // no-sse2). There isn't any reason to disable it if the target processor
10206 // supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010207 if (Subtarget->hasXMMInt() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010208 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10209
10210 SDValue Chain = Op.getOperand(0);
10211 SDValue Zero = DAG.getConstant(0, MVT::i32);
10212 SDValue Ops[] = {
10213 DAG.getRegister(X86::ESP, MVT::i32), // Base
10214 DAG.getTargetConstant(1, MVT::i8), // Scale
10215 DAG.getRegister(0, MVT::i32), // Index
10216 DAG.getTargetConstant(0, MVT::i32), // Disp
10217 DAG.getRegister(0, MVT::i32), // Segment.
10218 Zero,
10219 Chain
10220 };
10221 SDNode *Res =
10222 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10223 array_lengthof(Ops));
10224 return SDValue(Res, 0);
10225 }
10226
10227 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10228 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10229}
10230
10231
Dan Gohmand858e902010-04-17 15:26:15 +000010232SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010233 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010234 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010235 unsigned Reg = 0;
10236 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010237 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010238 default:
10239 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010240 case MVT::i8: Reg = X86::AL; size = 1; break;
10241 case MVT::i16: Reg = X86::AX; size = 2; break;
10242 case MVT::i32: Reg = X86::EAX; size = 4; break;
10243 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010244 assert(Subtarget->is64Bit() && "Node not type legal!");
10245 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010246 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010247 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010248 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010249 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010250 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010251 Op.getOperand(1),
10252 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010253 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010254 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010255 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010256 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10257 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10258 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010259 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010260 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010261 return cpOut;
10262}
10263
Duncan Sands1607f052008-12-01 11:39:25 +000010264SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010265 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010266 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010267 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010268 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010269 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010270 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010271 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10272 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010273 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010274 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10275 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010276 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010277 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010278 rdx.getValue(1)
10279 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010280 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010281}
10282
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010283SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010284 SelectionDAG &DAG) const {
10285 EVT SrcVT = Op.getOperand(0).getValueType();
10286 EVT DstVT = Op.getValueType();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010287 assert(Subtarget->is64Bit() && !Subtarget->hasXMMInt() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010288 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010289 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010290 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010291 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010292 // i64 <=> MMX conversions are Legal.
10293 if (SrcVT==MVT::i64 && DstVT.isVector())
10294 return Op;
10295 if (DstVT==MVT::i64 && SrcVT.isVector())
10296 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010297 // MMX <=> MMX conversions are Legal.
10298 if (SrcVT.isVector() && DstVT.isVector())
10299 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010300 // All other conversions need to be expanded.
10301 return SDValue();
10302}
Chris Lattner5b856542010-12-20 00:59:46 +000010303
Dan Gohmand858e902010-04-17 15:26:15 +000010304SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010305 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010306 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010307 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010308 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010309 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010310 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010311 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010312 Node->getOperand(0),
10313 Node->getOperand(1), negOp,
10314 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010315 cast<AtomicSDNode>(Node)->getAlignment(),
10316 cast<AtomicSDNode>(Node)->getOrdering(),
10317 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010318}
10319
Eli Friedman327236c2011-08-24 20:50:09 +000010320static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10321 SDNode *Node = Op.getNode();
10322 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010323 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010324
10325 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010326 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10327 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10328 // (The only way to get a 16-byte store is cmpxchg16b)
10329 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10330 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10331 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010332 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10333 cast<AtomicSDNode>(Node)->getMemoryVT(),
10334 Node->getOperand(0),
10335 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010336 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010337 cast<AtomicSDNode>(Node)->getOrdering(),
10338 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010339 return Swap.getValue(1);
10340 }
10341 // Other atomic stores have a simple pattern.
10342 return Op;
10343}
10344
Chris Lattner5b856542010-12-20 00:59:46 +000010345static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10346 EVT VT = Op.getNode()->getValueType(0);
10347
10348 // Let legalize expand this if it isn't a legal type yet.
10349 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10350 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010351
Chris Lattner5b856542010-12-20 00:59:46 +000010352 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010353
Chris Lattner5b856542010-12-20 00:59:46 +000010354 unsigned Opc;
10355 bool ExtraOp = false;
10356 switch (Op.getOpcode()) {
10357 default: assert(0 && "Invalid code");
10358 case ISD::ADDC: Opc = X86ISD::ADD; break;
10359 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10360 case ISD::SUBC: Opc = X86ISD::SUB; break;
10361 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10362 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010363
Chris Lattner5b856542010-12-20 00:59:46 +000010364 if (!ExtraOp)
10365 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10366 Op.getOperand(1));
10367 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10368 Op.getOperand(1), Op.getOperand(2));
10369}
10370
Evan Cheng0db9fe62006-04-25 20:13:52 +000010371/// LowerOperation - Provide custom lowering hooks for some operations.
10372///
Dan Gohmand858e902010-04-17 15:26:15 +000010373SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010374 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010375 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010376 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010377 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010378 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010379 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10380 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010381 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010382 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010383 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010384 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10385 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10386 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010387 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010388 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010389 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10390 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10391 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010392 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010393 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010394 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010395 case ISD::SHL_PARTS:
10396 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010397 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010398 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010399 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010400 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010401 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010402 case ISD::FABS: return LowerFABS(Op, DAG);
10403 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010404 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010405 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010406 case ISD::SETCC: return LowerSETCC(Op, DAG);
10407 case ISD::SELECT: return LowerSELECT(Op, DAG);
10408 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010409 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010410 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010411 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010412 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010413 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010414 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10415 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010416 case ISD::FRAME_TO_ARGS_OFFSET:
10417 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010418 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010419 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010420 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10421 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010422 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010423 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10424 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010425 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010426 case ISD::SRA:
10427 case ISD::SRL:
10428 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010429 case ISD::SADDO:
10430 case ISD::UADDO:
10431 case ISD::SSUBO:
10432 case ISD::USUBO:
10433 case ISD::SMULO:
10434 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010435 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010436 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010437 case ISD::ADDC:
10438 case ISD::ADDE:
10439 case ISD::SUBC:
10440 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010441 case ISD::ADD: return LowerADD(Op, DAG);
10442 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010443 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010444}
10445
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010446static void ReplaceATOMIC_LOAD(SDNode *Node,
10447 SmallVectorImpl<SDValue> &Results,
10448 SelectionDAG &DAG) {
10449 DebugLoc dl = Node->getDebugLoc();
10450 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10451
10452 // Convert wide load -> cmpxchg8b/cmpxchg16b
10453 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10454 // (The only way to get a 16-byte load is cmpxchg16b)
10455 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010456 SDValue Zero = DAG.getConstant(0, VT);
10457 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010458 Node->getOperand(0),
10459 Node->getOperand(1), Zero, Zero,
10460 cast<AtomicSDNode>(Node)->getMemOperand(),
10461 cast<AtomicSDNode>(Node)->getOrdering(),
10462 cast<AtomicSDNode>(Node)->getSynchScope());
10463 Results.push_back(Swap.getValue(0));
10464 Results.push_back(Swap.getValue(1));
10465}
10466
Duncan Sands1607f052008-12-01 11:39:25 +000010467void X86TargetLowering::
10468ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010469 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010470 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010471 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +000010472 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000010473
10474 SDValue Chain = Node->getOperand(0);
10475 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010476 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010477 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000010478 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010479 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000010480 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000010481 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000010482 SDValue Result =
10483 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10484 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000010485 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000010486 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010487 Results.push_back(Result.getValue(2));
10488}
10489
Duncan Sands126d9072008-07-04 11:47:58 +000010490/// ReplaceNodeResults - Replace a node with an illegal result type
10491/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000010492void X86TargetLowering::ReplaceNodeResults(SDNode *N,
10493 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000010494 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010495 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000010496 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000010497 default:
Duncan Sands1607f052008-12-01 11:39:25 +000010498 assert(false && "Do not know how to custom type legalize this operation!");
10499 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010500 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000010501 case ISD::ADDC:
10502 case ISD::ADDE:
10503 case ISD::SUBC:
10504 case ISD::SUBE:
10505 // We don't want to expand or promote these.
10506 return;
Duncan Sands1607f052008-12-01 11:39:25 +000010507 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000010508 std::pair<SDValue,SDValue> Vals =
10509 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000010510 SDValue FIST = Vals.first, StackSlot = Vals.second;
10511 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000010512 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000010513 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000010514 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
10515 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000010516 }
10517 return;
10518 }
10519 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010520 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010521 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010522 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010523 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000010524 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000010525 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000010526 eax.getValue(2));
10527 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10528 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000010529 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010530 Results.push_back(edx.getValue(1));
10531 return;
10532 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010533 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000010534 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010535 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000010536 bool Regs64bit = T == MVT::i128;
10537 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000010538 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010539 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10540 DAG.getConstant(0, HalfT));
10541 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
10542 DAG.getConstant(1, HalfT));
10543 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
10544 Regs64bit ? X86::RAX : X86::EAX,
10545 cpInL, SDValue());
10546 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
10547 Regs64bit ? X86::RDX : X86::EDX,
10548 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010549 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000010550 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10551 DAG.getConstant(0, HalfT));
10552 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
10553 DAG.getConstant(1, HalfT));
10554 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
10555 Regs64bit ? X86::RBX : X86::EBX,
10556 swapInL, cpInH.getValue(1));
10557 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
10558 Regs64bit ? X86::RCX : X86::ECX,
10559 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000010560 SDValue Ops[] = { swapInH.getValue(0),
10561 N->getOperand(1),
10562 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010563 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010564 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000010565 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
10566 X86ISD::LCMPXCHG8_DAG;
10567 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000010568 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000010569 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
10570 Regs64bit ? X86::RAX : X86::EAX,
10571 HalfT, Result.getValue(1));
10572 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
10573 Regs64bit ? X86::RDX : X86::EDX,
10574 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000010575 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000010576 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000010577 Results.push_back(cpOutH.getValue(1));
10578 return;
10579 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010580 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000010581 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10582 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010583 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000010584 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10585 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010586 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000010587 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10588 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010589 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000010590 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10591 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010592 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000010593 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10594 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010595 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000010596 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10597 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010598 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000010599 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10600 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010601 case ISD::ATOMIC_LOAD:
10602 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000010603 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000010604}
10605
Evan Cheng72261582005-12-20 06:22:03 +000010606const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10607 switch (Opcode) {
10608 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000010609 case X86ISD::BSF: return "X86ISD::BSF";
10610 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000010611 case X86ISD::SHLD: return "X86ISD::SHLD";
10612 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000010613 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010614 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000010615 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000010616 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000010617 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000010618 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000010619 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10620 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10621 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000010622 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000010623 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000010624 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000010625 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000010626 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000010627 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000010628 case X86ISD::COMI: return "X86ISD::COMI";
10629 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000010630 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000010631 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000010632 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
10633 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000010634 case X86ISD::CMOV: return "X86ISD::CMOV";
10635 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000010636 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000010637 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
10638 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000010639 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000010640 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000010641 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010642 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000010643 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000010644 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
10645 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000010646 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000010647 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000010648 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Nate Begemanb65c1752010-12-17 22:55:37 +000010649 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
10650 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
10651 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Evan Cheng8ca29322006-11-10 21:43:37 +000010652 case X86ISD::FMAX: return "X86ISD::FMAX";
10653 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000010654 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
10655 case X86ISD::FRCP: return "X86ISD::FRCP";
Duncan Sands17470be2011-09-22 20:15:48 +000010656 case X86ISD::FHADD: return "X86ISD::FHADD";
10657 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010658 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000010659 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010660 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000010661 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010662 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000010663 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
10664 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010665 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
10666 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
10667 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
10668 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
10669 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
10670 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000010671 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
10672 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000010673 case X86ISD::VSHL: return "X86ISD::VSHL";
10674 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000010675 case X86ISD::CMPPD: return "X86ISD::CMPPD";
10676 case X86ISD::CMPPS: return "X86ISD::CMPPS";
10677 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
10678 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
10679 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
10680 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
10681 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
10682 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
10683 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
10684 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010685 case X86ISD::ADD: return "X86ISD::ADD";
10686 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000010687 case X86ISD::ADC: return "X86ISD::ADC";
10688 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000010689 case X86ISD::SMUL: return "X86ISD::SMUL";
10690 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000010691 case X86ISD::INC: return "X86ISD::INC";
10692 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000010693 case X86ISD::OR: return "X86ISD::OR";
10694 case X86ISD::XOR: return "X86ISD::XOR";
10695 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +000010696 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000010697 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010698 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010699 case X86ISD::PALIGN: return "X86ISD::PALIGN";
10700 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
10701 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
10702 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
10703 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
10704 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
10705 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
10706 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
10707 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010708 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000010709 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010710 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000010711 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
10712 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010713 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
10714 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
10715 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
10716 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
10717 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
10718 case X86ISD::MOVSD: return "X86ISD::MOVSD";
10719 case X86ISD::MOVSS: return "X86ISD::MOVSS";
10720 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
10721 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +000010722 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000010723 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
10724 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
10725 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
10726 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
10727 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
10728 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
10729 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
10730 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
10731 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
10732 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000010733 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000010734 case X86ISD::VPERMILPS: return "X86ISD::VPERMILPS";
10735 case X86ISD::VPERMILPSY: return "X86ISD::VPERMILPSY";
10736 case X86ISD::VPERMILPD: return "X86ISD::VPERMILPD";
10737 case X86ISD::VPERMILPDY: return "X86ISD::VPERMILPDY";
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000010738 case X86ISD::VPERM2F128: return "X86ISD::VPERM2F128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000010739 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000010740 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010741 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000010742 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000010743 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000010744 }
10745}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010746
Chris Lattnerc9addb72007-03-30 23:15:24 +000010747// isLegalAddressingMode - Return true if the addressing mode represented
10748// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000010749bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010750 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000010751 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010752 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000010753 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000010754
Chris Lattnerc9addb72007-03-30 23:15:24 +000010755 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010756 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010757 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000010758
Chris Lattnerc9addb72007-03-30 23:15:24 +000010759 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000010760 unsigned GVFlags =
10761 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010762
Chris Lattnerdfed4132009-07-10 07:38:24 +000010763 // If a reference to this global requires an extra load, we can't fold it.
10764 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000010765 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010766
Chris Lattnerdfed4132009-07-10 07:38:24 +000010767 // If BaseGV requires a register for the PIC base, we cannot also have a
10768 // BaseReg specified.
10769 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000010770 return false;
Evan Cheng52787842007-08-01 23:46:47 +000010771
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010772 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000010773 if ((M != CodeModel::Small || R != Reloc::Static) &&
10774 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000010775 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000010776 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010777
Chris Lattnerc9addb72007-03-30 23:15:24 +000010778 switch (AM.Scale) {
10779 case 0:
10780 case 1:
10781 case 2:
10782 case 4:
10783 case 8:
10784 // These scales always work.
10785 break;
10786 case 3:
10787 case 5:
10788 case 9:
10789 // These scales are formed with basereg+scalereg. Only accept if there is
10790 // no basereg yet.
10791 if (AM.HasBaseReg)
10792 return false;
10793 break;
10794 default: // Other stuff never works.
10795 return false;
10796 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010797
Chris Lattnerc9addb72007-03-30 23:15:24 +000010798 return true;
10799}
10800
10801
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010802bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010803 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000010804 return false;
Evan Chenge127a732007-10-29 07:57:50 +000010805 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
10806 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010807 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000010808 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010809 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000010810}
10811
Owen Andersone50ed302009-08-10 22:56:29 +000010812bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010813 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010814 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010815 unsigned NumBits1 = VT1.getSizeInBits();
10816 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000010817 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010818 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000010819 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000010820}
Evan Cheng2bd122c2007-10-26 01:56:11 +000010821
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010822bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010823 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010824 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010825}
10826
Owen Andersone50ed302009-08-10 22:56:29 +000010827bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000010828 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000010829 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000010830}
10831
Owen Andersone50ed302009-08-10 22:56:29 +000010832bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000010833 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000010834 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000010835}
10836
Evan Cheng60c07e12006-07-05 22:17:51 +000010837/// isShuffleMaskLegal - Targets can use this to indicate that they only
10838/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
10839/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
10840/// are assumed to be legal.
10841bool
Eric Christopherfd179292009-08-27 18:07:15 +000010842X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000010843 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000010844 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000010845 if (VT.getSizeInBits() == 64)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010846 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX());
Nate Begeman9008ca62009-04-27 18:41:29 +000010847
Nate Begemana09008b2009-10-19 02:17:23 +000010848 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000010849 return (VT.getVectorNumElements() == 2 ||
10850 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
10851 isMOVLMask(M, VT) ||
10852 isSHUFPMask(M, VT) ||
10853 isPSHUFDMask(M, VT) ||
10854 isPSHUFHWMask(M, VT) ||
10855 isPSHUFLWMask(M, VT) ||
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010856 isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000010857 isUNPCKLMask(M, VT) ||
10858 isUNPCKHMask(M, VT) ||
10859 isUNPCKL_v_undef_Mask(M, VT) ||
10860 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010861}
10862
Dan Gohman7d8143f2008-04-09 20:09:42 +000010863bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000010864X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000010865 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000010866 unsigned NumElts = VT.getVectorNumElements();
10867 // FIXME: This collection of masks seems suspect.
10868 if (NumElts == 2)
10869 return true;
10870 if (NumElts == 4 && VT.getSizeInBits() == 128) {
10871 return (isMOVLMask(Mask, VT) ||
10872 isCommutedMOVLMask(Mask, VT, true) ||
10873 isSHUFPMask(Mask, VT) ||
10874 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000010875 }
10876 return false;
10877}
10878
10879//===----------------------------------------------------------------------===//
10880// X86 Scheduler Hooks
10881//===----------------------------------------------------------------------===//
10882
Mon P Wang63307c32008-05-05 19:05:59 +000010883// private utility function
10884MachineBasicBlock *
10885X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
10886 MachineBasicBlock *MBB,
10887 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010888 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010889 unsigned LoadOpc,
10890 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010891 unsigned notOpc,
10892 unsigned EAXreg,
10893 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010894 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000010895 // For the atomic bitwise operator, we generate
10896 // thisMBB:
10897 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000010898 // ld t1 = [bitinstr.addr]
10899 // op t2 = t1, [bitinstr.val]
10900 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000010901 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
10902 // bz newMBB
10903 // fallthrough -->nextMBB
10904 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10905 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010906 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000010907 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000010908
Mon P Wang63307c32008-05-05 19:05:59 +000010909 /// First build the CFG
10910 MachineFunction *F = MBB->getParent();
10911 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000010912 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10913 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10914 F->insert(MBBIter, newMBB);
10915 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010916
Dan Gohman14152b42010-07-06 20:24:04 +000010917 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10918 nextMBB->splice(nextMBB->begin(), thisMBB,
10919 llvm::next(MachineBasicBlock::iterator(bInstr)),
10920 thisMBB->end());
10921 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010922
Mon P Wang63307c32008-05-05 19:05:59 +000010923 // Update thisMBB to fall through to newMBB
10924 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010925
Mon P Wang63307c32008-05-05 19:05:59 +000010926 // newMBB jumps to itself and fall through to nextMBB
10927 newMBB->addSuccessor(nextMBB);
10928 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000010929
Mon P Wang63307c32008-05-05 19:05:59 +000010930 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010931 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000010932 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000010933 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000010934 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010935 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000010936 int numArgs = bInstr->getNumOperands() - 1;
10937 for (int i=0; i < numArgs; ++i)
10938 argOpers[i] = &bInstr->getOperand(i+1);
10939
10940 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010941 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000010942 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000010943
Dale Johannesen140be2d2008-08-19 18:47:28 +000010944 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010945 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000010946 for (int i=0; i <= lastAddrIndx; ++i)
10947 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010948
Dale Johannesen140be2d2008-08-19 18:47:28 +000010949 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010950 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010951 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010952 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010953 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010954 tt = t1;
10955
Dale Johannesen140be2d2008-08-19 18:47:28 +000010956 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000010957 assert((argOpers[valArgIndx]->isReg() ||
10958 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010959 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000010960 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000010961 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010962 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010963 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010964 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000010965 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010966
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010967 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000010968 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010969
Dale Johannesene4d209d2009-02-03 20:21:25 +000010970 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000010971 for (int i=0; i <= lastAddrIndx; ++i)
10972 (*MIB).addOperand(*argOpers[i]);
10973 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000010974 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010975 (*MIB).setMemRefs(bInstr->memoperands_begin(),
10976 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000010977
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010978 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000010979 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010980
Mon P Wang63307c32008-05-05 19:05:59 +000010981 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010982 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010983
Dan Gohman14152b42010-07-06 20:24:04 +000010984 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010985 return nextMBB;
10986}
10987
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000010988// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000010989MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010990X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
10991 MachineBasicBlock *MBB,
10992 unsigned regOpcL,
10993 unsigned regOpcH,
10994 unsigned immOpcL,
10995 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000010996 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010997 // For the atomic bitwise operator, we generate
10998 // thisMBB (instructions are in pairs, except cmpxchg8b)
10999 // ld t1,t2 = [bitinstr.addr]
11000 // newMBB:
11001 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11002 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011003 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011004 // mov ECX, EBX <- t5, t6
11005 // mov EAX, EDX <- t1, t2
11006 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11007 // mov t3, t4 <- EAX, EDX
11008 // bz newMBB
11009 // result in out1, out2
11010 // fallthrough -->nextMBB
11011
11012 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11013 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011014 const unsigned NotOpc = X86::NOT32r;
11015 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11016 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11017 MachineFunction::iterator MBBIter = MBB;
11018 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011019
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011020 /// First build the CFG
11021 MachineFunction *F = MBB->getParent();
11022 MachineBasicBlock *thisMBB = MBB;
11023 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11024 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11025 F->insert(MBBIter, newMBB);
11026 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011027
Dan Gohman14152b42010-07-06 20:24:04 +000011028 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11029 nextMBB->splice(nextMBB->begin(), thisMBB,
11030 llvm::next(MachineBasicBlock::iterator(bInstr)),
11031 thisMBB->end());
11032 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011033
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011034 // Update thisMBB to fall through to newMBB
11035 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011036
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011037 // newMBB jumps to itself and fall through to nextMBB
11038 newMBB->addSuccessor(nextMBB);
11039 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011040
Dale Johannesene4d209d2009-02-03 20:21:25 +000011041 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011042 // Insert instructions into newMBB based on incoming instruction
11043 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011044 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011045 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011046 MachineOperand& dest1Oper = bInstr->getOperand(0);
11047 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011048 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11049 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011050 argOpers[i] = &bInstr->getOperand(i+2);
11051
Dan Gohman71ea4e52010-05-14 21:01:44 +000011052 // We use some of the operands multiple times, so conservatively just
11053 // clear any kill flags that might be present.
11054 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11055 argOpers[i]->setIsKill(false);
11056 }
11057
Evan Chengad5b52f2010-01-08 19:14:57 +000011058 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011059 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011060
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011061 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011062 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011063 for (int i=0; i <= lastAddrIndx; ++i)
11064 (*MIB).addOperand(*argOpers[i]);
11065 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011066 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011067 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011068 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011069 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011070 MachineOperand newOp3 = *(argOpers[3]);
11071 if (newOp3.isImm())
11072 newOp3.setImm(newOp3.getImm()+4);
11073 else
11074 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011075 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011076 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011077
11078 // t3/4 are defined later, at the bottom of the loop
11079 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11080 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011081 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011082 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011083 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011084 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11085
Evan Cheng306b4ca2010-01-08 23:41:50 +000011086 // The subsequent operations should be using the destination registers of
11087 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011088 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011089 t1 = F->getRegInfo().createVirtualRegister(RC);
11090 t2 = F->getRegInfo().createVirtualRegister(RC);
11091 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11092 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011093 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011094 t1 = dest1Oper.getReg();
11095 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011096 }
11097
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011098 int valArgIndx = lastAddrIndx + 1;
11099 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011100 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011101 "invalid operand");
11102 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11103 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011104 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011105 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011106 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011107 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011108 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011109 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011110 (*MIB).addOperand(*argOpers[valArgIndx]);
11111 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011112 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011113 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011114 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011115 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011116 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011117 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011118 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011119 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011120 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011121 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011122
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011123 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011124 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011125 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011126 MIB.addReg(t2);
11127
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011128 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011129 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011130 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011131 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011132
Dale Johannesene4d209d2009-02-03 20:21:25 +000011133 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011134 for (int i=0; i <= lastAddrIndx; ++i)
11135 (*MIB).addOperand(*argOpers[i]);
11136
11137 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011138 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11139 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011140
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011141 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011142 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011143 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011144 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011145
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011146 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011147 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011148
Dan Gohman14152b42010-07-06 20:24:04 +000011149 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011150 return nextMBB;
11151}
11152
11153// private utility function
11154MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011155X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11156 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011157 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011158 // For the atomic min/max operator, we generate
11159 // thisMBB:
11160 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011161 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011162 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011163 // cmp t1, t2
11164 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011165 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011166 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11167 // bz newMBB
11168 // fallthrough -->nextMBB
11169 //
11170 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11171 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011172 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011173 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011174
Mon P Wang63307c32008-05-05 19:05:59 +000011175 /// First build the CFG
11176 MachineFunction *F = MBB->getParent();
11177 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011178 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11179 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11180 F->insert(MBBIter, newMBB);
11181 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011182
Dan Gohman14152b42010-07-06 20:24:04 +000011183 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11184 nextMBB->splice(nextMBB->begin(), thisMBB,
11185 llvm::next(MachineBasicBlock::iterator(mInstr)),
11186 thisMBB->end());
11187 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011188
Mon P Wang63307c32008-05-05 19:05:59 +000011189 // Update thisMBB to fall through to newMBB
11190 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011191
Mon P Wang63307c32008-05-05 19:05:59 +000011192 // newMBB jumps to newMBB and fall through to nextMBB
11193 newMBB->addSuccessor(nextMBB);
11194 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011195
Dale Johannesene4d209d2009-02-03 20:21:25 +000011196 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011197 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011198 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011199 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011200 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011201 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011202 int numArgs = mInstr->getNumOperands() - 1;
11203 for (int i=0; i < numArgs; ++i)
11204 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011205
Mon P Wang63307c32008-05-05 19:05:59 +000011206 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011207 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011208 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011209
Mon P Wangab3e7472008-05-05 22:56:23 +000011210 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011211 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011212 for (int i=0; i <= lastAddrIndx; ++i)
11213 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011214
Mon P Wang63307c32008-05-05 19:05:59 +000011215 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011216 assert((argOpers[valArgIndx]->isReg() ||
11217 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011218 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011219
11220 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011221 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011222 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011223 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011224 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011225 (*MIB).addOperand(*argOpers[valArgIndx]);
11226
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011227 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011228 MIB.addReg(t1);
11229
Dale Johannesene4d209d2009-02-03 20:21:25 +000011230 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011231 MIB.addReg(t1);
11232 MIB.addReg(t2);
11233
11234 // Generate movc
11235 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011236 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011237 MIB.addReg(t2);
11238 MIB.addReg(t1);
11239
11240 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011241 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011242 for (int i=0; i <= lastAddrIndx; ++i)
11243 (*MIB).addOperand(*argOpers[i]);
11244 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011245 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011246 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11247 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011248
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011249 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011250 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011251
Mon P Wang63307c32008-05-05 19:05:59 +000011252 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011253 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011254
Dan Gohman14152b42010-07-06 20:24:04 +000011255 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011256 return nextMBB;
11257}
11258
Eric Christopherf83a5de2009-08-27 18:08:16 +000011259// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011260// or XMM0_V32I8 in AVX all of this code can be replaced with that
11261// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011262MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011263X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011264 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011265 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
11266 "Target must have SSE4.2 or AVX features enabled");
11267
Eric Christopherb120ab42009-08-18 22:50:32 +000011268 DebugLoc dl = MI->getDebugLoc();
11269 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011270 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011271 if (!Subtarget->hasAVX()) {
11272 if (memArg)
11273 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11274 else
11275 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11276 } else {
11277 if (memArg)
11278 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11279 else
11280 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11281 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011282
Eric Christopher41c902f2010-11-30 08:20:21 +000011283 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011284 for (unsigned i = 0; i < numArgs; ++i) {
11285 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011286 if (!(Op.isReg() && Op.isImplicit()))
11287 MIB.addOperand(Op);
11288 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011289 BuildMI(*BB, MI, dl,
11290 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11291 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011292 .addReg(X86::XMM0);
11293
Dan Gohman14152b42010-07-06 20:24:04 +000011294 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011295 return BB;
11296}
11297
11298MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011299X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011300 DebugLoc dl = MI->getDebugLoc();
11301 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011302
Eric Christopher228232b2010-11-30 07:20:12 +000011303 // Address into RAX/EAX, other two args into ECX, EDX.
11304 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11305 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11306 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11307 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011308 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011309
Eric Christopher228232b2010-11-30 07:20:12 +000011310 unsigned ValOps = X86::AddrNumOperands;
11311 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11312 .addReg(MI->getOperand(ValOps).getReg());
11313 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11314 .addReg(MI->getOperand(ValOps+1).getReg());
11315
11316 // The instruction doesn't actually take any operands though.
11317 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011318
Eric Christopher228232b2010-11-30 07:20:12 +000011319 MI->eraseFromParent(); // The pseudo is gone now.
11320 return BB;
11321}
11322
11323MachineBasicBlock *
11324X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011325 DebugLoc dl = MI->getDebugLoc();
11326 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011327
Eric Christopher228232b2010-11-30 07:20:12 +000011328 // First arg in ECX, the second in EAX.
11329 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11330 .addReg(MI->getOperand(0).getReg());
11331 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11332 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011333
Eric Christopher228232b2010-11-30 07:20:12 +000011334 // The instruction doesn't actually take any operands though.
11335 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011336
Eric Christopher228232b2010-11-30 07:20:12 +000011337 MI->eraseFromParent(); // The pseudo is gone now.
11338 return BB;
11339}
11340
11341MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011342X86TargetLowering::EmitVAARG64WithCustomInserter(
11343 MachineInstr *MI,
11344 MachineBasicBlock *MBB) const {
11345 // Emit va_arg instruction on X86-64.
11346
11347 // Operands to this pseudo-instruction:
11348 // 0 ) Output : destination address (reg)
11349 // 1-5) Input : va_list address (addr, i64mem)
11350 // 6 ) ArgSize : Size (in bytes) of vararg type
11351 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11352 // 8 ) Align : Alignment of type
11353 // 9 ) EFLAGS (implicit-def)
11354
11355 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11356 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11357
11358 unsigned DestReg = MI->getOperand(0).getReg();
11359 MachineOperand &Base = MI->getOperand(1);
11360 MachineOperand &Scale = MI->getOperand(2);
11361 MachineOperand &Index = MI->getOperand(3);
11362 MachineOperand &Disp = MI->getOperand(4);
11363 MachineOperand &Segment = MI->getOperand(5);
11364 unsigned ArgSize = MI->getOperand(6).getImm();
11365 unsigned ArgMode = MI->getOperand(7).getImm();
11366 unsigned Align = MI->getOperand(8).getImm();
11367
11368 // Memory Reference
11369 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11370 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11371 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11372
11373 // Machine Information
11374 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11375 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11376 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11377 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11378 DebugLoc DL = MI->getDebugLoc();
11379
11380 // struct va_list {
11381 // i32 gp_offset
11382 // i32 fp_offset
11383 // i64 overflow_area (address)
11384 // i64 reg_save_area (address)
11385 // }
11386 // sizeof(va_list) = 24
11387 // alignment(va_list) = 8
11388
11389 unsigned TotalNumIntRegs = 6;
11390 unsigned TotalNumXMMRegs = 8;
11391 bool UseGPOffset = (ArgMode == 1);
11392 bool UseFPOffset = (ArgMode == 2);
11393 unsigned MaxOffset = TotalNumIntRegs * 8 +
11394 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11395
11396 /* Align ArgSize to a multiple of 8 */
11397 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11398 bool NeedsAlign = (Align > 8);
11399
11400 MachineBasicBlock *thisMBB = MBB;
11401 MachineBasicBlock *overflowMBB;
11402 MachineBasicBlock *offsetMBB;
11403 MachineBasicBlock *endMBB;
11404
11405 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11406 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11407 unsigned OffsetReg = 0;
11408
11409 if (!UseGPOffset && !UseFPOffset) {
11410 // If we only pull from the overflow region, we don't create a branch.
11411 // We don't need to alter control flow.
11412 OffsetDestReg = 0; // unused
11413 OverflowDestReg = DestReg;
11414
11415 offsetMBB = NULL;
11416 overflowMBB = thisMBB;
11417 endMBB = thisMBB;
11418 } else {
11419 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11420 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11421 // If not, pull from overflow_area. (branch to overflowMBB)
11422 //
11423 // thisMBB
11424 // | .
11425 // | .
11426 // offsetMBB overflowMBB
11427 // | .
11428 // | .
11429 // endMBB
11430
11431 // Registers for the PHI in endMBB
11432 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11433 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11434
11435 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11436 MachineFunction *MF = MBB->getParent();
11437 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11438 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11439 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11440
11441 MachineFunction::iterator MBBIter = MBB;
11442 ++MBBIter;
11443
11444 // Insert the new basic blocks
11445 MF->insert(MBBIter, offsetMBB);
11446 MF->insert(MBBIter, overflowMBB);
11447 MF->insert(MBBIter, endMBB);
11448
11449 // Transfer the remainder of MBB and its successor edges to endMBB.
11450 endMBB->splice(endMBB->begin(), thisMBB,
11451 llvm::next(MachineBasicBlock::iterator(MI)),
11452 thisMBB->end());
11453 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11454
11455 // Make offsetMBB and overflowMBB successors of thisMBB
11456 thisMBB->addSuccessor(offsetMBB);
11457 thisMBB->addSuccessor(overflowMBB);
11458
11459 // endMBB is a successor of both offsetMBB and overflowMBB
11460 offsetMBB->addSuccessor(endMBB);
11461 overflowMBB->addSuccessor(endMBB);
11462
11463 // Load the offset value into a register
11464 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11465 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
11466 .addOperand(Base)
11467 .addOperand(Scale)
11468 .addOperand(Index)
11469 .addDisp(Disp, UseFPOffset ? 4 : 0)
11470 .addOperand(Segment)
11471 .setMemRefs(MMOBegin, MMOEnd);
11472
11473 // Check if there is enough room left to pull this argument.
11474 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
11475 .addReg(OffsetReg)
11476 .addImm(MaxOffset + 8 - ArgSizeA8);
11477
11478 // Branch to "overflowMBB" if offset >= max
11479 // Fall through to "offsetMBB" otherwise
11480 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
11481 .addMBB(overflowMBB);
11482 }
11483
11484 // In offsetMBB, emit code to use the reg_save_area.
11485 if (offsetMBB) {
11486 assert(OffsetReg != 0);
11487
11488 // Read the reg_save_area address.
11489 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
11490 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
11491 .addOperand(Base)
11492 .addOperand(Scale)
11493 .addOperand(Index)
11494 .addDisp(Disp, 16)
11495 .addOperand(Segment)
11496 .setMemRefs(MMOBegin, MMOEnd);
11497
11498 // Zero-extend the offset
11499 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
11500 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
11501 .addImm(0)
11502 .addReg(OffsetReg)
11503 .addImm(X86::sub_32bit);
11504
11505 // Add the offset to the reg_save_area to get the final address.
11506 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
11507 .addReg(OffsetReg64)
11508 .addReg(RegSaveReg);
11509
11510 // Compute the offset for the next argument
11511 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
11512 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
11513 .addReg(OffsetReg)
11514 .addImm(UseFPOffset ? 16 : 8);
11515
11516 // Store it back into the va_list.
11517 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
11518 .addOperand(Base)
11519 .addOperand(Scale)
11520 .addOperand(Index)
11521 .addDisp(Disp, UseFPOffset ? 4 : 0)
11522 .addOperand(Segment)
11523 .addReg(NextOffsetReg)
11524 .setMemRefs(MMOBegin, MMOEnd);
11525
11526 // Jump to endMBB
11527 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
11528 .addMBB(endMBB);
11529 }
11530
11531 //
11532 // Emit code to use overflow area
11533 //
11534
11535 // Load the overflow_area address into a register.
11536 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
11537 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
11538 .addOperand(Base)
11539 .addOperand(Scale)
11540 .addOperand(Index)
11541 .addDisp(Disp, 8)
11542 .addOperand(Segment)
11543 .setMemRefs(MMOBegin, MMOEnd);
11544
11545 // If we need to align it, do so. Otherwise, just copy the address
11546 // to OverflowDestReg.
11547 if (NeedsAlign) {
11548 // Align the overflow address
11549 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
11550 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
11551
11552 // aligned_addr = (addr + (align-1)) & ~(align-1)
11553 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
11554 .addReg(OverflowAddrReg)
11555 .addImm(Align-1);
11556
11557 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
11558 .addReg(TmpReg)
11559 .addImm(~(uint64_t)(Align-1));
11560 } else {
11561 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
11562 .addReg(OverflowAddrReg);
11563 }
11564
11565 // Compute the next overflow address after this argument.
11566 // (the overflow address should be kept 8-byte aligned)
11567 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
11568 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
11569 .addReg(OverflowDestReg)
11570 .addImm(ArgSizeA8);
11571
11572 // Store the new overflow address.
11573 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
11574 .addOperand(Base)
11575 .addOperand(Scale)
11576 .addOperand(Index)
11577 .addDisp(Disp, 8)
11578 .addOperand(Segment)
11579 .addReg(NextAddrReg)
11580 .setMemRefs(MMOBegin, MMOEnd);
11581
11582 // If we branched, emit the PHI to the front of endMBB.
11583 if (offsetMBB) {
11584 BuildMI(*endMBB, endMBB->begin(), DL,
11585 TII->get(X86::PHI), DestReg)
11586 .addReg(OffsetDestReg).addMBB(offsetMBB)
11587 .addReg(OverflowDestReg).addMBB(overflowMBB);
11588 }
11589
11590 // Erase the pseudo instruction
11591 MI->eraseFromParent();
11592
11593 return endMBB;
11594}
11595
11596MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000011597X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11598 MachineInstr *MI,
11599 MachineBasicBlock *MBB) const {
11600 // Emit code to save XMM registers to the stack. The ABI says that the
11601 // number of registers to save is given in %al, so it's theoretically
11602 // possible to do an indirect jump trick to avoid saving all of them,
11603 // however this code takes a simpler approach and just executes all
11604 // of the stores if %al is non-zero. It's less code, and it's probably
11605 // easier on the hardware branch predictor, and stores aren't all that
11606 // expensive anyway.
11607
11608 // Create the new basic blocks. One block contains all the XMM stores,
11609 // and one block is the final destination regardless of whether any
11610 // stores were performed.
11611 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11612 MachineFunction *F = MBB->getParent();
11613 MachineFunction::iterator MBBIter = MBB;
11614 ++MBBIter;
11615 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11616 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11617 F->insert(MBBIter, XMMSaveMBB);
11618 F->insert(MBBIter, EndMBB);
11619
Dan Gohman14152b42010-07-06 20:24:04 +000011620 // Transfer the remainder of MBB and its successor edges to EndMBB.
11621 EndMBB->splice(EndMBB->begin(), MBB,
11622 llvm::next(MachineBasicBlock::iterator(MI)),
11623 MBB->end());
11624 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11625
Dan Gohmand6708ea2009-08-15 01:38:56 +000011626 // The original block will now fall through to the XMM save block.
11627 MBB->addSuccessor(XMMSaveMBB);
11628 // The XMMSaveMBB will fall through to the end block.
11629 XMMSaveMBB->addSuccessor(EndMBB);
11630
11631 // Now add the instructions.
11632 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11633 DebugLoc DL = MI->getDebugLoc();
11634
11635 unsigned CountReg = MI->getOperand(0).getReg();
11636 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11637 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11638
11639 if (!Subtarget->isTargetWin64()) {
11640 // If %al is 0, branch around the XMM save block.
11641 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011642 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011643 MBB->addSuccessor(EndMBB);
11644 }
11645
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011646 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000011647 // In the XMM save block, save all the XMM argument registers.
11648 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11649 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000011650 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000011651 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000011652 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000011653 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000011654 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011655 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000011656 .addFrameIndex(RegSaveFrameIndex)
11657 .addImm(/*Scale=*/1)
11658 .addReg(/*IndexReg=*/0)
11659 .addImm(/*Disp=*/Offset)
11660 .addReg(/*Segment=*/0)
11661 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000011662 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000011663 }
11664
Dan Gohman14152b42010-07-06 20:24:04 +000011665 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011666
11667 return EndMBB;
11668}
Mon P Wang63307c32008-05-05 19:05:59 +000011669
Evan Cheng60c07e12006-07-05 22:17:51 +000011670MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000011671X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011672 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000011673 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11674 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000011675
Chris Lattner52600972009-09-02 05:57:00 +000011676 // To "insert" a SELECT_CC instruction, we actually have to insert the
11677 // diamond control-flow pattern. The incoming instruction knows the
11678 // destination vreg to set, the condition code register to branch on, the
11679 // true/false values to select between, and a branch opcode to use.
11680 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11681 MachineFunction::iterator It = BB;
11682 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000011683
Chris Lattner52600972009-09-02 05:57:00 +000011684 // thisMBB:
11685 // ...
11686 // TrueVal = ...
11687 // cmpTY ccX, r1, r2
11688 // bCC copy1MBB
11689 // fallthrough --> copy0MBB
11690 MachineBasicBlock *thisMBB = BB;
11691 MachineFunction *F = BB->getParent();
11692 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11693 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000011694 F->insert(It, copy0MBB);
11695 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000011696
Bill Wendling730c07e2010-06-25 20:48:10 +000011697 // If the EFLAGS register isn't dead in the terminator, then claim that it's
11698 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000011699 if (!MI->killsRegister(X86::EFLAGS)) {
11700 copy0MBB->addLiveIn(X86::EFLAGS);
11701 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000011702 }
11703
Dan Gohman14152b42010-07-06 20:24:04 +000011704 // Transfer the remainder of BB and its successor edges to sinkMBB.
11705 sinkMBB->splice(sinkMBB->begin(), BB,
11706 llvm::next(MachineBasicBlock::iterator(MI)),
11707 BB->end());
11708 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11709
11710 // Add the true and fallthrough blocks as its successors.
11711 BB->addSuccessor(copy0MBB);
11712 BB->addSuccessor(sinkMBB);
11713
11714 // Create the conditional branch instruction.
11715 unsigned Opc =
11716 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
11717 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
11718
Chris Lattner52600972009-09-02 05:57:00 +000011719 // copy0MBB:
11720 // %FalseValue = ...
11721 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000011722 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000011723
Chris Lattner52600972009-09-02 05:57:00 +000011724 // sinkMBB:
11725 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
11726 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000011727 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
11728 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000011729 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
11730 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
11731
Dan Gohman14152b42010-07-06 20:24:04 +000011732 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000011733 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000011734}
11735
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011736MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011737X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
11738 bool Is64Bit) const {
11739 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11740 DebugLoc DL = MI->getDebugLoc();
11741 MachineFunction *MF = BB->getParent();
11742 const BasicBlock *LLVM_BB = BB->getBasicBlock();
11743
11744 assert(EnableSegmentedStacks);
11745
11746 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
11747 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
11748
11749 // BB:
11750 // ... [Till the alloca]
11751 // If stacklet is not large enough, jump to mallocMBB
11752 //
11753 // bumpMBB:
11754 // Allocate by subtracting from RSP
11755 // Jump to continueMBB
11756 //
11757 // mallocMBB:
11758 // Allocate by call to runtime
11759 //
11760 // continueMBB:
11761 // ...
11762 // [rest of original BB]
11763 //
11764
11765 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11766 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11767 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11768
11769 MachineRegisterInfo &MRI = MF->getRegInfo();
11770 const TargetRegisterClass *AddrRegClass =
11771 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
11772
11773 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11774 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
11775 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
11776 sizeVReg = MI->getOperand(1).getReg(),
11777 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
11778
11779 MachineFunction::iterator MBBIter = BB;
11780 ++MBBIter;
11781
11782 MF->insert(MBBIter, bumpMBB);
11783 MF->insert(MBBIter, mallocMBB);
11784 MF->insert(MBBIter, continueMBB);
11785
11786 continueMBB->splice(continueMBB->begin(), BB, llvm::next
11787 (MachineBasicBlock::iterator(MI)), BB->end());
11788 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
11789
11790 // Add code to the main basic block to check if the stack limit has been hit,
11791 // and if so, jump to mallocMBB otherwise to bumpMBB.
11792 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
11793 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), tmpSPVReg)
11794 .addReg(tmpSPVReg).addReg(sizeVReg);
11795 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
11796 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
11797 .addReg(tmpSPVReg);
11798 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
11799
11800 // bumpMBB simply decreases the stack pointer, since we know the current
11801 // stacklet has enough space.
11802 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
11803 .addReg(tmpSPVReg);
11804 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
11805 .addReg(tmpSPVReg);
11806 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11807
11808 // Calls into a routine in libgcc to allocate more space from the heap.
11809 if (Is64Bit) {
11810 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
11811 .addReg(sizeVReg);
11812 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
11813 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
11814 } else {
11815 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
11816 .addImm(12);
11817 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
11818 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
11819 .addExternalSymbol("__morestack_allocate_stack_space");
11820 }
11821
11822 if (!Is64Bit)
11823 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
11824 .addImm(16);
11825
11826 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
11827 .addReg(Is64Bit ? X86::RAX : X86::EAX);
11828 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
11829
11830 // Set up the CFG correctly.
11831 BB->addSuccessor(bumpMBB);
11832 BB->addSuccessor(mallocMBB);
11833 mallocMBB->addSuccessor(continueMBB);
11834 bumpMBB->addSuccessor(continueMBB);
11835
11836 // Take care of the PHI nodes.
11837 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
11838 MI->getOperand(0).getReg())
11839 .addReg(mallocPtrVReg).addMBB(mallocMBB)
11840 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
11841
11842 // Delete the original pseudo instruction.
11843 MI->eraseFromParent();
11844
11845 // And we're done.
11846 return continueMBB;
11847}
11848
11849MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011850X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011851 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011852 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11853 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011854
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011855 assert(!Subtarget->isTargetEnvMacho());
11856
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011857 // The lowering is pretty easy: we're just emitting the call to _alloca. The
11858 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011859
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011860 if (Subtarget->isTargetWin64()) {
11861 if (Subtarget->isTargetCygMing()) {
11862 // ___chkstk(Mingw64):
11863 // Clobbers R10, R11, RAX and EFLAGS.
11864 // Updates RSP.
11865 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11866 .addExternalSymbol("___chkstk")
11867 .addReg(X86::RAX, RegState::Implicit)
11868 .addReg(X86::RSP, RegState::Implicit)
11869 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
11870 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
11871 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11872 } else {
11873 // __chkstk(MSVCRT): does not update stack pointer.
11874 // Clobbers R10, R11 and EFLAGS.
11875 // FIXME: RAX(allocated size) might be reused and not killed.
11876 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
11877 .addExternalSymbol("__chkstk")
11878 .addReg(X86::RAX, RegState::Implicit)
11879 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11880 // RAX has the offset to subtracted from RSP.
11881 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
11882 .addReg(X86::RSP)
11883 .addReg(X86::RAX);
11884 }
11885 } else {
11886 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011887 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
11888
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000011889 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
11890 .addExternalSymbol(StackProbeSymbol)
11891 .addReg(X86::EAX, RegState::Implicit)
11892 .addReg(X86::ESP, RegState::Implicit)
11893 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
11894 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
11895 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
11896 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011897
Dan Gohman14152b42010-07-06 20:24:04 +000011898 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000011899 return BB;
11900}
Chris Lattner52600972009-09-02 05:57:00 +000011901
11902MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000011903X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
11904 MachineBasicBlock *BB) const {
11905 // This is pretty easy. We're taking the value that we received from
11906 // our load from the relocation, sticking it in either RDI (x86-64)
11907 // or EAX and doing an indirect call. The return value will then
11908 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000011909 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000011910 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000011911 DebugLoc DL = MI->getDebugLoc();
11912 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000011913
11914 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000011915 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011916
Eric Christopher30ef0e52010-06-03 04:07:48 +000011917 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000011918 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11919 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000011920 .addReg(X86::RIP)
11921 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011922 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011923 MI->getOperand(3).getTargetFlags())
11924 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000011925 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000011926 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000011927 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000011928 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11929 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000011930 .addReg(0)
11931 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011932 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000011933 MI->getOperand(3).getTargetFlags())
11934 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011935 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011936 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011937 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000011938 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11939 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000011940 .addReg(TII->getGlobalBaseReg(F))
11941 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000011942 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000011943 MI->getOperand(3).getTargetFlags())
11944 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000011945 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000011946 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011947 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011948
Dan Gohman14152b42010-07-06 20:24:04 +000011949 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000011950 return BB;
11951}
11952
11953MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000011954X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000011955 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000011956 switch (MI->getOpcode()) {
Richard Trieu23946fc2011-09-21 03:09:09 +000011957 default: assert(0 && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000011958 case X86::TAILJMPd64:
11959 case X86::TAILJMPr64:
11960 case X86::TAILJMPm64:
Richard Trieu23946fc2011-09-21 03:09:09 +000011961 assert(0 && "TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000011962 case X86::TCRETURNdi64:
11963 case X86::TCRETURNri64:
11964 case X86::TCRETURNmi64:
11965 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
11966 // On AMD64, additional defs should be added before register allocation.
11967 if (!Subtarget->isTargetWin64()) {
11968 MI->addRegisterDefined(X86::RSI);
11969 MI->addRegisterDefined(X86::RDI);
11970 MI->addRegisterDefined(X86::XMM6);
11971 MI->addRegisterDefined(X86::XMM7);
11972 MI->addRegisterDefined(X86::XMM8);
11973 MI->addRegisterDefined(X86::XMM9);
11974 MI->addRegisterDefined(X86::XMM10);
11975 MI->addRegisterDefined(X86::XMM11);
11976 MI->addRegisterDefined(X86::XMM12);
11977 MI->addRegisterDefined(X86::XMM13);
11978 MI->addRegisterDefined(X86::XMM14);
11979 MI->addRegisterDefined(X86::XMM15);
11980 }
11981 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011982 case X86::WIN_ALLOCA:
11983 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000011984 case X86::SEG_ALLOCA_32:
11985 return EmitLoweredSegAlloca(MI, BB, false);
11986 case X86::SEG_ALLOCA_64:
11987 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000011988 case X86::TLSCall_32:
11989 case X86::TLSCall_64:
11990 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000011991 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000011992 case X86::CMOV_FR32:
11993 case X86::CMOV_FR64:
11994 case X86::CMOV_V4F32:
11995 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000011996 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000011997 case X86::CMOV_V8F32:
11998 case X86::CMOV_V4F64:
11999 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012000 case X86::CMOV_GR16:
12001 case X86::CMOV_GR32:
12002 case X86::CMOV_RFP32:
12003 case X86::CMOV_RFP64:
12004 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012005 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012006
Dale Johannesen849f2142007-07-03 00:53:03 +000012007 case X86::FP32_TO_INT16_IN_MEM:
12008 case X86::FP32_TO_INT32_IN_MEM:
12009 case X86::FP32_TO_INT64_IN_MEM:
12010 case X86::FP64_TO_INT16_IN_MEM:
12011 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012012 case X86::FP64_TO_INT64_IN_MEM:
12013 case X86::FP80_TO_INT16_IN_MEM:
12014 case X86::FP80_TO_INT32_IN_MEM:
12015 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012016 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12017 DebugLoc DL = MI->getDebugLoc();
12018
Evan Cheng60c07e12006-07-05 22:17:51 +000012019 // Change the floating point control register to use "round towards zero"
12020 // mode when truncating to an integer value.
12021 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012022 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012023 addFrameReference(BuildMI(*BB, MI, DL,
12024 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012025
12026 // Load the old value of the high byte of the control word...
12027 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012028 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012029 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012030 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012031
12032 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012033 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012034 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012035
12036 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012037 addFrameReference(BuildMI(*BB, MI, DL,
12038 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012039
12040 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012041 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012042 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012043
12044 // Get the X86 opcode to use.
12045 unsigned Opc;
12046 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012047 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012048 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12049 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12050 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12051 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12052 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12053 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012054 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12055 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12056 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012057 }
12058
12059 X86AddressMode AM;
12060 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012061 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012062 AM.BaseType = X86AddressMode::RegBase;
12063 AM.Base.Reg = Op.getReg();
12064 } else {
12065 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012066 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012067 }
12068 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012069 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012070 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012071 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012072 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012073 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012074 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012075 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012076 AM.GV = Op.getGlobal();
12077 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012078 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012079 }
Dan Gohman14152b42010-07-06 20:24:04 +000012080 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012081 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012082
12083 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012084 addFrameReference(BuildMI(*BB, MI, DL,
12085 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012086
Dan Gohman14152b42010-07-06 20:24:04 +000012087 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012088 return BB;
12089 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012090 // String/text processing lowering.
12091 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012092 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012093 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12094 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012095 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012096 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12097 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012098 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012099 return EmitPCMP(MI, BB, 5, false /* in mem */);
12100 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012101 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012102 return EmitPCMP(MI, BB, 5, true /* in mem */);
12103
Eric Christopher228232b2010-11-30 07:20:12 +000012104 // Thread synchronization.
12105 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012106 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012107 case X86::MWAIT:
12108 return EmitMwait(MI, BB);
12109
Eric Christopherb120ab42009-08-18 22:50:32 +000012110 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012111 case X86::ATOMAND32:
12112 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012113 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012114 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012115 X86::NOT32r, X86::EAX,
12116 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012117 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012118 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12119 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012120 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012121 X86::NOT32r, X86::EAX,
12122 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012123 case X86::ATOMXOR32:
12124 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012125 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012126 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012127 X86::NOT32r, X86::EAX,
12128 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012129 case X86::ATOMNAND32:
12130 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012131 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012132 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012133 X86::NOT32r, X86::EAX,
12134 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012135 case X86::ATOMMIN32:
12136 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12137 case X86::ATOMMAX32:
12138 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12139 case X86::ATOMUMIN32:
12140 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12141 case X86::ATOMUMAX32:
12142 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012143
12144 case X86::ATOMAND16:
12145 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12146 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012147 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012148 X86::NOT16r, X86::AX,
12149 X86::GR16RegisterClass);
12150 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012151 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012152 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012153 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012154 X86::NOT16r, X86::AX,
12155 X86::GR16RegisterClass);
12156 case X86::ATOMXOR16:
12157 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12158 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012159 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012160 X86::NOT16r, X86::AX,
12161 X86::GR16RegisterClass);
12162 case X86::ATOMNAND16:
12163 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12164 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012165 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012166 X86::NOT16r, X86::AX,
12167 X86::GR16RegisterClass, true);
12168 case X86::ATOMMIN16:
12169 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12170 case X86::ATOMMAX16:
12171 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12172 case X86::ATOMUMIN16:
12173 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12174 case X86::ATOMUMAX16:
12175 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12176
12177 case X86::ATOMAND8:
12178 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12179 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012180 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012181 X86::NOT8r, X86::AL,
12182 X86::GR8RegisterClass);
12183 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012184 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012185 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012186 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012187 X86::NOT8r, X86::AL,
12188 X86::GR8RegisterClass);
12189 case X86::ATOMXOR8:
12190 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12191 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012192 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012193 X86::NOT8r, X86::AL,
12194 X86::GR8RegisterClass);
12195 case X86::ATOMNAND8:
12196 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12197 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012198 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012199 X86::NOT8r, X86::AL,
12200 X86::GR8RegisterClass, true);
12201 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012202 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012203 case X86::ATOMAND64:
12204 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012205 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012206 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012207 X86::NOT64r, X86::RAX,
12208 X86::GR64RegisterClass);
12209 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012210 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12211 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012212 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012213 X86::NOT64r, X86::RAX,
12214 X86::GR64RegisterClass);
12215 case X86::ATOMXOR64:
12216 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012217 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012218 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012219 X86::NOT64r, X86::RAX,
12220 X86::GR64RegisterClass);
12221 case X86::ATOMNAND64:
12222 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12223 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012224 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012225 X86::NOT64r, X86::RAX,
12226 X86::GR64RegisterClass, true);
12227 case X86::ATOMMIN64:
12228 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12229 case X86::ATOMMAX64:
12230 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12231 case X86::ATOMUMIN64:
12232 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12233 case X86::ATOMUMAX64:
12234 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012235
12236 // This group does 64-bit operations on a 32-bit host.
12237 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012238 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012239 X86::AND32rr, X86::AND32rr,
12240 X86::AND32ri, X86::AND32ri,
12241 false);
12242 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012243 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012244 X86::OR32rr, X86::OR32rr,
12245 X86::OR32ri, X86::OR32ri,
12246 false);
12247 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012248 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012249 X86::XOR32rr, X86::XOR32rr,
12250 X86::XOR32ri, X86::XOR32ri,
12251 false);
12252 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012253 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012254 X86::AND32rr, X86::AND32rr,
12255 X86::AND32ri, X86::AND32ri,
12256 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012257 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012258 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012259 X86::ADD32rr, X86::ADC32rr,
12260 X86::ADD32ri, X86::ADC32ri,
12261 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012262 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012263 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012264 X86::SUB32rr, X86::SBB32rr,
12265 X86::SUB32ri, X86::SBB32ri,
12266 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012267 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012268 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012269 X86::MOV32rr, X86::MOV32rr,
12270 X86::MOV32ri, X86::MOV32ri,
12271 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012272 case X86::VASTART_SAVE_XMM_REGS:
12273 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012274
12275 case X86::VAARG_64:
12276 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012277 }
12278}
12279
12280//===----------------------------------------------------------------------===//
12281// X86 Optimization Hooks
12282//===----------------------------------------------------------------------===//
12283
Dan Gohman475871a2008-07-27 21:46:04 +000012284void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012285 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012286 APInt &KnownZero,
12287 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012288 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012289 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012290 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012291 assert((Opc >= ISD::BUILTIN_OP_END ||
12292 Opc == ISD::INTRINSIC_WO_CHAIN ||
12293 Opc == ISD::INTRINSIC_W_CHAIN ||
12294 Opc == ISD::INTRINSIC_VOID) &&
12295 "Should use MaskedValueIsZero if you don't know whether Op"
12296 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012297
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012298 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012299 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012300 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012301 case X86ISD::ADD:
12302 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012303 case X86ISD::ADC:
12304 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012305 case X86ISD::SMUL:
12306 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012307 case X86ISD::INC:
12308 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012309 case X86ISD::OR:
12310 case X86ISD::XOR:
12311 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012312 // These nodes' second result is a boolean.
12313 if (Op.getResNo() == 0)
12314 break;
12315 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012316 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012317 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12318 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012319 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012320 case ISD::INTRINSIC_WO_CHAIN: {
12321 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
12322 unsigned NumLoBits = 0;
12323 switch (IntId) {
12324 default: break;
12325 case Intrinsic::x86_sse_movmsk_ps:
12326 case Intrinsic::x86_avx_movmsk_ps_256:
12327 case Intrinsic::x86_sse2_movmsk_pd:
12328 case Intrinsic::x86_avx_movmsk_pd_256:
12329 case Intrinsic::x86_mmx_pmovmskb:
12330 case Intrinsic::x86_sse2_pmovmskb_128: {
12331 // High bits of movmskp{s|d}, pmovmskb are known zero.
12332 switch (IntId) {
12333 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
12334 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
12335 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
12336 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
12337 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
12338 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
12339 }
12340 KnownZero = APInt::getHighBitsSet(Mask.getBitWidth(),
12341 Mask.getBitWidth() - NumLoBits);
12342 break;
12343 }
12344 }
12345 break;
12346 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012347 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012348}
Chris Lattner259e97c2006-01-31 19:43:35 +000012349
Owen Andersonbc146b02010-09-21 20:42:50 +000012350unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12351 unsigned Depth) const {
12352 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12353 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12354 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012355
Owen Andersonbc146b02010-09-21 20:42:50 +000012356 // Fallback case.
12357 return 1;
12358}
12359
Evan Cheng206ee9d2006-07-07 08:33:52 +000012360/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012361/// node is a GlobalAddress + offset.
12362bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012363 const GlobalValue* &GA,
12364 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012365 if (N->getOpcode() == X86ISD::Wrapper) {
12366 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012367 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012368 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012369 return true;
12370 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012371 }
Evan Chengad4196b2008-05-12 19:56:52 +000012372 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012373}
12374
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012375/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12376/// same as extracting the high 128-bit part of 256-bit vector and then
12377/// inserting the result into the low part of a new 256-bit vector
12378static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12379 EVT VT = SVOp->getValueType(0);
12380 int NumElems = VT.getVectorNumElements();
12381
12382 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12383 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12384 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12385 SVOp->getMaskElt(j) >= 0)
12386 return false;
12387
12388 return true;
12389}
12390
12391/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12392/// same as extracting the low 128-bit part of 256-bit vector and then
12393/// inserting the result into the high part of a new 256-bit vector
12394static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12395 EVT VT = SVOp->getValueType(0);
12396 int NumElems = VT.getVectorNumElements();
12397
12398 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12399 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12400 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12401 SVOp->getMaskElt(j) >= 0)
12402 return false;
12403
12404 return true;
12405}
12406
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012407/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12408static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12409 TargetLowering::DAGCombinerInfo &DCI) {
12410 DebugLoc dl = N->getDebugLoc();
12411 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12412 SDValue V1 = SVOp->getOperand(0);
12413 SDValue V2 = SVOp->getOperand(1);
12414 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012415 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012416
12417 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12418 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12419 //
12420 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012421 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012422 // V UNDEF BUILD_VECTOR UNDEF
12423 // \ / \ /
12424 // CONCAT_VECTOR CONCAT_VECTOR
12425 // \ /
12426 // \ /
12427 // RESULT: V + zero extended
12428 //
12429 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12430 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12431 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12432 return SDValue();
12433
12434 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12435 return SDValue();
12436
12437 // To match the shuffle mask, the first half of the mask should
12438 // be exactly the first vector, and all the rest a splat with the
12439 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012440 for (int i = 0; i < NumElems/2; ++i)
12441 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12442 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12443 return SDValue();
12444
12445 // Emit a zeroed vector and insert the desired subvector on its
12446 // first half.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000012447 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012448 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
12449 DAG.getConstant(0, MVT::i32), DAG, dl);
12450 return DCI.CombineTo(N, InsV);
12451 }
12452
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012453 //===--------------------------------------------------------------------===//
12454 // Combine some shuffles into subvector extracts and inserts:
12455 //
12456
12457 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12458 if (isShuffleHigh128VectorInsertLow(SVOp)) {
12459 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
12460 DAG, dl);
12461 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12462 V, DAG.getConstant(0, MVT::i32), DAG, dl);
12463 return DCI.CombineTo(N, InsV);
12464 }
12465
12466 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12467 if (isShuffleLow128VectorInsertHigh(SVOp)) {
12468 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
12469 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
12470 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
12471 return DCI.CombineTo(N, InsV);
12472 }
12473
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012474 return SDValue();
12475}
12476
12477/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000012478static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012479 TargetLowering::DAGCombinerInfo &DCI,
12480 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012481 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012482 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000012483
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012484 // Don't create instructions with illegal types after legalize types has run.
12485 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12486 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
12487 return SDValue();
12488
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000012489 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
12490 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
12491 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012492 return PerformShuffleCombine256(N, DAG, DCI);
12493
12494 // Only handle 128 wide vector from here on.
12495 if (VT.getSizeInBits() != 128)
12496 return SDValue();
12497
12498 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
12499 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
12500 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000012501 SmallVector<SDValue, 16> Elts;
12502 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012503 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000012504
Nate Begemanfdea31a2010-03-24 20:49:50 +000012505 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000012506}
Evan Chengd880b972008-05-09 21:53:03 +000012507
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000012508/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
12509/// generation and convert it from being a bunch of shuffles and extracts
12510/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012511static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12512 const TargetLowering &TLI) {
12513 SDValue InputVector = N->getOperand(0);
12514
12515 // Only operate on vectors of 4 elements, where the alternative shuffling
12516 // gets to be more expensive.
12517 if (InputVector.getValueType() != MVT::v4i32)
12518 return SDValue();
12519
12520 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12521 // single use which is a sign-extend or zero-extend, and all elements are
12522 // used.
12523 SmallVector<SDNode *, 4> Uses;
12524 unsigned ExtractedElements = 0;
12525 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12526 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12527 if (UI.getUse().getResNo() != InputVector.getResNo())
12528 return SDValue();
12529
12530 SDNode *Extract = *UI;
12531 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12532 return SDValue();
12533
12534 if (Extract->getValueType(0) != MVT::i32)
12535 return SDValue();
12536 if (!Extract->hasOneUse())
12537 return SDValue();
12538 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12539 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12540 return SDValue();
12541 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12542 return SDValue();
12543
12544 // Record which element was extracted.
12545 ExtractedElements |=
12546 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12547
12548 Uses.push_back(Extract);
12549 }
12550
12551 // If not all the elements were used, this may not be worthwhile.
12552 if (ExtractedElements != 15)
12553 return SDValue();
12554
12555 // Ok, we've now decided to do the transformation.
12556 DebugLoc dl = InputVector.getDebugLoc();
12557
12558 // Store the value to a temporary stack slot.
12559 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000012560 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
12561 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012562
12563 // Replace each use (extract) with a load of the appropriate element.
12564 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12565 UE = Uses.end(); UI != UE; ++UI) {
12566 SDNode *Extract = *UI;
12567
Nadav Rotem86694292011-05-17 08:31:57 +000012568 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012569 SDValue Idx = Extract->getOperand(1);
12570 unsigned EltSize =
12571 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12572 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12573 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12574
Nadav Rotem86694292011-05-17 08:31:57 +000012575 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012576 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012577
12578 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000012579 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000012580 ScalarAddr, MachinePointerInfo(),
12581 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012582
12583 // Replace the exact with the load.
12584 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12585 }
12586
12587 // The replacement was made in place; don't return anything.
12588 return SDValue();
12589}
12590
Duncan Sands6bcd2192011-09-17 16:49:39 +000012591/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
12592/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012593static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000012594 const X86Subtarget *Subtarget) {
12595 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000012596 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000012597 // Get the LHS/RHS of the select.
12598 SDValue LHS = N->getOperand(1);
12599 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000012600 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000012601
Dan Gohman670e5392009-09-21 18:03:22 +000012602 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000012603 // instructions match the semantics of the common C idiom x<y?x:y but not
12604 // x<=y?x:y, because of how they handle negative zero (which can be
12605 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000012606 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
12607 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
12608 (Subtarget->hasXMMInt() ||
12609 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012610 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012611
Chris Lattner47b4ce82009-03-11 05:48:52 +000012612 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000012613 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000012614 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12615 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012616 switch (CC) {
12617 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012618 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012619 // Converting this to a min would handle NaNs incorrectly, and swapping
12620 // the operands would cause it to handle comparisons between positive
12621 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012622 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012623 if (!UnsafeFPMath &&
12624 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12625 break;
12626 std::swap(LHS, RHS);
12627 }
Dan Gohman670e5392009-09-21 18:03:22 +000012628 Opcode = X86ISD::FMIN;
12629 break;
12630 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012631 // Converting this to a min would handle comparisons between positive
12632 // and negative zero incorrectly.
12633 if (!UnsafeFPMath &&
12634 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12635 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012636 Opcode = X86ISD::FMIN;
12637 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012638 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012639 // Converting this to a min would handle both negative zeros and NaNs
12640 // incorrectly, but we can swap the operands to fix both.
12641 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012642 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012643 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012644 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012645 Opcode = X86ISD::FMIN;
12646 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012647
Dan Gohman670e5392009-09-21 18:03:22 +000012648 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012649 // Converting this to a max would handle comparisons between positive
12650 // and negative zero incorrectly.
12651 if (!UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000012652 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012653 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012654 Opcode = X86ISD::FMAX;
12655 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000012656 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012657 // Converting this to a max would handle NaNs incorrectly, and swapping
12658 // the operands would cause it to handle comparisons between positive
12659 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012660 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000012661 if (!UnsafeFPMath &&
12662 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12663 break;
12664 std::swap(LHS, RHS);
12665 }
Dan Gohman670e5392009-09-21 18:03:22 +000012666 Opcode = X86ISD::FMAX;
12667 break;
12668 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012669 // Converting this to a max would handle both negative zeros and NaNs
12670 // incorrectly, but we can swap the operands to fix both.
12671 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012672 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012673 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012674 case ISD::SETGE:
12675 Opcode = X86ISD::FMAX;
12676 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000012677 }
Dan Gohman670e5392009-09-21 18:03:22 +000012678 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000012679 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
12680 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000012681 switch (CC) {
12682 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000012683 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012684 // Converting this to a min would handle comparisons between positive
12685 // and negative zero incorrectly, and swapping the operands would
12686 // cause it to handle NaNs incorrectly.
12687 if (!UnsafeFPMath &&
12688 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000012689 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012690 break;
12691 std::swap(LHS, RHS);
12692 }
Dan Gohman670e5392009-09-21 18:03:22 +000012693 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000012694 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012695 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000012696 // Converting this to a min would handle NaNs incorrectly.
12697 if (!UnsafeFPMath &&
12698 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
12699 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012700 Opcode = X86ISD::FMIN;
12701 break;
12702 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000012703 // Converting this to a min would handle both negative zeros and NaNs
12704 // incorrectly, but we can swap the operands to fix both.
12705 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012706 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012707 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012708 case ISD::SETGE:
12709 Opcode = X86ISD::FMIN;
12710 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012711
Dan Gohman670e5392009-09-21 18:03:22 +000012712 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000012713 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000012714 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012715 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012716 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000012717 break;
Dan Gohman670e5392009-09-21 18:03:22 +000012718 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000012719 // Converting this to a max would handle comparisons between positive
12720 // and negative zero incorrectly, and swapping the operands would
12721 // cause it to handle NaNs incorrectly.
12722 if (!UnsafeFPMath &&
12723 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000012724 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000012725 break;
12726 std::swap(LHS, RHS);
12727 }
Dan Gohman670e5392009-09-21 18:03:22 +000012728 Opcode = X86ISD::FMAX;
12729 break;
12730 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000012731 // Converting this to a max would handle both negative zeros and NaNs
12732 // incorrectly, but we can swap the operands to fix both.
12733 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000012734 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012735 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000012736 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000012737 Opcode = X86ISD::FMAX;
12738 break;
12739 }
Chris Lattner83e6c992006-10-04 06:57:07 +000012740 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012741
Chris Lattner47b4ce82009-03-11 05:48:52 +000012742 if (Opcode)
12743 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000012744 }
Eric Christopherfd179292009-08-27 18:07:15 +000012745
Chris Lattnerd1980a52009-03-12 06:52:53 +000012746 // If this is a select between two integer constants, try to do some
12747 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000012748 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
12749 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000012750 // Don't do this for crazy integer types.
12751 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
12752 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000012753 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012754 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000012755
Chris Lattnercee56e72009-03-13 05:53:31 +000012756 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000012757 // Efficiently invertible.
12758 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
12759 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
12760 isa<ConstantSDNode>(Cond.getOperand(1))))) {
12761 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000012762 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012763 }
Eric Christopherfd179292009-08-27 18:07:15 +000012764
Chris Lattnerd1980a52009-03-12 06:52:53 +000012765 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012766 if (FalseC->getAPIntValue() == 0 &&
12767 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012768 if (NeedsCondInvert) // Invert the condition if needed.
12769 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12770 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012771
Chris Lattnerd1980a52009-03-12 06:52:53 +000012772 // Zero extend the condition if needed.
12773 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012774
Chris Lattnercee56e72009-03-13 05:53:31 +000012775 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000012776 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012777 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012778 }
Eric Christopherfd179292009-08-27 18:07:15 +000012779
Chris Lattner97a29a52009-03-13 05:22:11 +000012780 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000012781 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000012782 if (NeedsCondInvert) // Invert the condition if needed.
12783 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12784 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012785
Chris Lattner97a29a52009-03-13 05:22:11 +000012786 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012787 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12788 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012789 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000012790 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000012791 }
Eric Christopherfd179292009-08-27 18:07:15 +000012792
Chris Lattnercee56e72009-03-13 05:53:31 +000012793 // Optimize cases that will turn into an LEA instruction. This requires
12794 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012795 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012796 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012797 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012798
Chris Lattnercee56e72009-03-13 05:53:31 +000012799 bool isFastMultiplier = false;
12800 if (Diff < 10) {
12801 switch ((unsigned char)Diff) {
12802 default: break;
12803 case 1: // result = add base, cond
12804 case 2: // result = lea base( , cond*2)
12805 case 3: // result = lea base(cond, cond*2)
12806 case 4: // result = lea base( , cond*4)
12807 case 5: // result = lea base(cond, cond*4)
12808 case 8: // result = lea base( , cond*8)
12809 case 9: // result = lea base(cond, cond*8)
12810 isFastMultiplier = true;
12811 break;
12812 }
12813 }
Eric Christopherfd179292009-08-27 18:07:15 +000012814
Chris Lattnercee56e72009-03-13 05:53:31 +000012815 if (isFastMultiplier) {
12816 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
12817 if (NeedsCondInvert) // Invert the condition if needed.
12818 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12819 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012820
Chris Lattnercee56e72009-03-13 05:53:31 +000012821 // Zero extend the condition if needed.
12822 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12823 Cond);
12824 // Scale the condition by the difference.
12825 if (Diff != 1)
12826 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12827 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000012828
Chris Lattnercee56e72009-03-13 05:53:31 +000012829 // Add the base if non-zero.
12830 if (FalseC->getAPIntValue() != 0)
12831 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12832 SDValue(FalseC, 0));
12833 return Cond;
12834 }
Eric Christopherfd179292009-08-27 18:07:15 +000012835 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012836 }
12837 }
Eric Christopherfd179292009-08-27 18:07:15 +000012838
Dan Gohman475871a2008-07-27 21:46:04 +000012839 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000012840}
12841
Chris Lattnerd1980a52009-03-12 06:52:53 +000012842/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
12843static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
12844 TargetLowering::DAGCombinerInfo &DCI) {
12845 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000012846
Chris Lattnerd1980a52009-03-12 06:52:53 +000012847 // If the flag operand isn't dead, don't touch this CMOV.
12848 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
12849 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000012850
Evan Chengb5a55d92011-05-24 01:48:22 +000012851 SDValue FalseOp = N->getOperand(0);
12852 SDValue TrueOp = N->getOperand(1);
12853 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
12854 SDValue Cond = N->getOperand(3);
12855 if (CC == X86::COND_E || CC == X86::COND_NE) {
12856 switch (Cond.getOpcode()) {
12857 default: break;
12858 case X86ISD::BSR:
12859 case X86ISD::BSF:
12860 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
12861 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
12862 return (CC == X86::COND_E) ? FalseOp : TrueOp;
12863 }
12864 }
12865
Chris Lattnerd1980a52009-03-12 06:52:53 +000012866 // If this is a select between two integer constants, try to do some
12867 // optimizations. Note that the operands are ordered the opposite of SELECT
12868 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000012869 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
12870 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000012871 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
12872 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000012873 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
12874 CC = X86::GetOppositeBranchCondition(CC);
12875 std::swap(TrueC, FalseC);
12876 }
Eric Christopherfd179292009-08-27 18:07:15 +000012877
Chris Lattnerd1980a52009-03-12 06:52:53 +000012878 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000012879 // This is efficient for any integer data type (including i8/i16) and
12880 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000012881 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012882 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12883 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012884
Chris Lattnerd1980a52009-03-12 06:52:53 +000012885 // Zero extend the condition if needed.
12886 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012887
Chris Lattnerd1980a52009-03-12 06:52:53 +000012888 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
12889 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000012890 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000012891 if (N->getNumValues() == 2) // Dead flag value?
12892 return DCI.CombineTo(N, Cond, SDValue());
12893 return Cond;
12894 }
Eric Christopherfd179292009-08-27 18:07:15 +000012895
Chris Lattnercee56e72009-03-13 05:53:31 +000012896 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
12897 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000012898 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012899 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12900 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000012901
Chris Lattner97a29a52009-03-13 05:22:11 +000012902 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000012903 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12904 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000012905 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12906 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000012907
Chris Lattner97a29a52009-03-13 05:22:11 +000012908 if (N->getNumValues() == 2) // Dead flag value?
12909 return DCI.CombineTo(N, Cond, SDValue());
12910 return Cond;
12911 }
Eric Christopherfd179292009-08-27 18:07:15 +000012912
Chris Lattnercee56e72009-03-13 05:53:31 +000012913 // Optimize cases that will turn into an LEA instruction. This requires
12914 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000012915 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000012916 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012917 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000012918
Chris Lattnercee56e72009-03-13 05:53:31 +000012919 bool isFastMultiplier = false;
12920 if (Diff < 10) {
12921 switch ((unsigned char)Diff) {
12922 default: break;
12923 case 1: // result = add base, cond
12924 case 2: // result = lea base( , cond*2)
12925 case 3: // result = lea base(cond, cond*2)
12926 case 4: // result = lea base( , cond*4)
12927 case 5: // result = lea base(cond, cond*4)
12928 case 8: // result = lea base( , cond*8)
12929 case 9: // result = lea base(cond, cond*8)
12930 isFastMultiplier = true;
12931 break;
12932 }
12933 }
Eric Christopherfd179292009-08-27 18:07:15 +000012934
Chris Lattnercee56e72009-03-13 05:53:31 +000012935 if (isFastMultiplier) {
12936 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000012937 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12938 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000012939 // Zero extend the condition if needed.
12940 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12941 Cond);
12942 // Scale the condition by the difference.
12943 if (Diff != 1)
12944 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12945 DAG.getConstant(Diff, Cond.getValueType()));
12946
12947 // Add the base if non-zero.
12948 if (FalseC->getAPIntValue() != 0)
12949 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12950 SDValue(FalseC, 0));
12951 if (N->getNumValues() == 2) // Dead flag value?
12952 return DCI.CombineTo(N, Cond, SDValue());
12953 return Cond;
12954 }
Eric Christopherfd179292009-08-27 18:07:15 +000012955 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000012956 }
12957 }
12958 return SDValue();
12959}
12960
12961
Evan Cheng0b0cd912009-03-28 05:57:29 +000012962/// PerformMulCombine - Optimize a single multiply with constant into two
12963/// in order to implement it with two cheaper instructions, e.g.
12964/// LEA + SHL, LEA + LEA.
12965static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
12966 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000012967 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
12968 return SDValue();
12969
Owen Andersone50ed302009-08-10 22:56:29 +000012970 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012971 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000012972 return SDValue();
12973
12974 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
12975 if (!C)
12976 return SDValue();
12977 uint64_t MulAmt = C->getZExtValue();
12978 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
12979 return SDValue();
12980
12981 uint64_t MulAmt1 = 0;
12982 uint64_t MulAmt2 = 0;
12983 if ((MulAmt % 9) == 0) {
12984 MulAmt1 = 9;
12985 MulAmt2 = MulAmt / 9;
12986 } else if ((MulAmt % 5) == 0) {
12987 MulAmt1 = 5;
12988 MulAmt2 = MulAmt / 5;
12989 } else if ((MulAmt % 3) == 0) {
12990 MulAmt1 = 3;
12991 MulAmt2 = MulAmt / 3;
12992 }
12993 if (MulAmt2 &&
12994 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
12995 DebugLoc DL = N->getDebugLoc();
12996
12997 if (isPowerOf2_64(MulAmt2) &&
12998 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
12999 // If second multiplifer is pow2, issue it first. We want the multiply by
13000 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
13001 // is an add.
13002 std::swap(MulAmt1, MulAmt2);
13003
13004 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000013005 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013006 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000013007 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000013008 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013009 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000013010 DAG.getConstant(MulAmt1, VT));
13011
Eric Christopherfd179292009-08-27 18:07:15 +000013012 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013013 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000013014 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000013015 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013016 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000013017 DAG.getConstant(MulAmt2, VT));
13018
13019 // Do not add new nodes to DAG combiner worklist.
13020 DCI.CombineTo(N, NewMul, false);
13021 }
13022 return SDValue();
13023}
13024
Evan Chengad9c0a32009-12-15 00:53:42 +000013025static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
13026 SDValue N0 = N->getOperand(0);
13027 SDValue N1 = N->getOperand(1);
13028 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13029 EVT VT = N0.getValueType();
13030
13031 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13032 // since the result of setcc_c is all zero's or all ones.
13033 if (N1C && N0.getOpcode() == ISD::AND &&
13034 N0.getOperand(1).getOpcode() == ISD::Constant) {
13035 SDValue N00 = N0.getOperand(0);
13036 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13037 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13038 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13039 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13040 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13041 APInt ShAmt = N1C->getAPIntValue();
13042 Mask = Mask.shl(ShAmt);
13043 if (Mask != 0)
13044 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13045 N00, DAG.getConstant(Mask, VT));
13046 }
13047 }
13048
13049 return SDValue();
13050}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013051
Nate Begeman740ab032009-01-26 00:52:55 +000013052/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13053/// when possible.
13054static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13055 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013056 EVT VT = N->getValueType(0);
13057 if (!VT.isVector() && VT.isInteger() &&
13058 N->getOpcode() == ISD::SHL)
13059 return PerformSHLCombine(N, DAG);
13060
Nate Begeman740ab032009-01-26 00:52:55 +000013061 // On X86 with SSE2 support, we can transform this to a vector shift if
13062 // all elements are shifted by the same amount. We can't do this in legalize
13063 // because the a constant vector is typically transformed to a constant pool
13064 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013065 if (!Subtarget->hasXMMInt())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013066 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013067
Owen Anderson825b72b2009-08-11 20:47:22 +000013068 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013069 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013070
Mon P Wang3becd092009-01-28 08:12:05 +000013071 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013072 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013073 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013074 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013075 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13076 unsigned NumElts = VT.getVectorNumElements();
13077 unsigned i = 0;
13078 for (; i != NumElts; ++i) {
13079 SDValue Arg = ShAmtOp.getOperand(i);
13080 if (Arg.getOpcode() == ISD::UNDEF) continue;
13081 BaseShAmt = Arg;
13082 break;
13083 }
13084 for (; i != NumElts; ++i) {
13085 SDValue Arg = ShAmtOp.getOperand(i);
13086 if (Arg.getOpcode() == ISD::UNDEF) continue;
13087 if (Arg != BaseShAmt) {
13088 return SDValue();
13089 }
13090 }
13091 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013092 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013093 SDValue InVec = ShAmtOp.getOperand(0);
13094 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13095 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13096 unsigned i = 0;
13097 for (; i != NumElts; ++i) {
13098 SDValue Arg = InVec.getOperand(i);
13099 if (Arg.getOpcode() == ISD::UNDEF) continue;
13100 BaseShAmt = Arg;
13101 break;
13102 }
13103 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13104 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013105 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013106 if (C->getZExtValue() == SplatIdx)
13107 BaseShAmt = InVec.getOperand(1);
13108 }
13109 }
13110 if (BaseShAmt.getNode() == 0)
13111 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13112 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013113 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013114 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013115
Mon P Wangefa42202009-09-03 19:56:25 +000013116 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013117 if (EltVT.bitsGT(MVT::i32))
13118 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13119 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013120 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013121
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013122 // The shift amount is identical so we can do a vector shift.
13123 SDValue ValOp = N->getOperand(0);
13124 switch (N->getOpcode()) {
13125 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013126 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013127 break;
13128 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013129 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013130 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013131 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013132 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013133 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013134 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013135 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013136 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013137 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013138 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013139 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013140 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013141 break;
13142 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013143 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013144 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013145 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013146 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013147 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013148 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013149 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013150 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013151 break;
13152 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013153 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013154 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013155 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013156 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013157 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013158 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013159 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013160 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013161 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013162 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013163 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013164 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013165 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013166 }
13167 return SDValue();
13168}
13169
Nate Begemanb65c1752010-12-17 22:55:37 +000013170
Stuart Hastings865f0932011-06-03 23:53:54 +000013171// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13172// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13173// and friends. Likewise for OR -> CMPNEQSS.
13174static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13175 TargetLowering::DAGCombinerInfo &DCI,
13176 const X86Subtarget *Subtarget) {
13177 unsigned opcode;
13178
13179 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13180 // we're requiring SSE2 for both.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013181 if (Subtarget->hasXMMInt() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013182 SDValue N0 = N->getOperand(0);
13183 SDValue N1 = N->getOperand(1);
13184 SDValue CMP0 = N0->getOperand(1);
13185 SDValue CMP1 = N1->getOperand(1);
13186 DebugLoc DL = N->getDebugLoc();
13187
13188 // The SETCCs should both refer to the same CMP.
13189 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13190 return SDValue();
13191
13192 SDValue CMP00 = CMP0->getOperand(0);
13193 SDValue CMP01 = CMP0->getOperand(1);
13194 EVT VT = CMP00.getValueType();
13195
13196 if (VT == MVT::f32 || VT == MVT::f64) {
13197 bool ExpectingFlags = false;
13198 // Check for any users that want flags:
13199 for (SDNode::use_iterator UI = N->use_begin(),
13200 UE = N->use_end();
13201 !ExpectingFlags && UI != UE; ++UI)
13202 switch (UI->getOpcode()) {
13203 default:
13204 case ISD::BR_CC:
13205 case ISD::BRCOND:
13206 case ISD::SELECT:
13207 ExpectingFlags = true;
13208 break;
13209 case ISD::CopyToReg:
13210 case ISD::SIGN_EXTEND:
13211 case ISD::ZERO_EXTEND:
13212 case ISD::ANY_EXTEND:
13213 break;
13214 }
13215
13216 if (!ExpectingFlags) {
13217 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13218 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13219
13220 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13221 X86::CondCode tmp = cc0;
13222 cc0 = cc1;
13223 cc1 = tmp;
13224 }
13225
13226 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13227 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13228 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13229 X86ISD::NodeType NTOperator = is64BitFP ?
13230 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13231 // FIXME: need symbolic constants for these magic numbers.
13232 // See X86ATTInstPrinter.cpp:printSSECC().
13233 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13234 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13235 DAG.getConstant(x86cc, MVT::i8));
13236 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13237 OnesOrZeroesF);
13238 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13239 DAG.getConstant(1, MVT::i32));
13240 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13241 return OneBitOfTruth;
13242 }
13243 }
13244 }
13245 }
13246 return SDValue();
13247}
13248
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013249/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13250/// so it can be folded inside ANDNP.
13251static bool CanFoldXORWithAllOnes(const SDNode *N) {
13252 EVT VT = N->getValueType(0);
13253
13254 // Match direct AllOnes for 128 and 256-bit vectors
13255 if (ISD::isBuildVectorAllOnes(N))
13256 return true;
13257
13258 // Look through a bit convert.
13259 if (N->getOpcode() == ISD::BITCAST)
13260 N = N->getOperand(0).getNode();
13261
13262 // Sometimes the operand may come from a insert_subvector building a 256-bit
13263 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013264 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013265 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13266 SDValue V1 = N->getOperand(0);
13267 SDValue V2 = N->getOperand(1);
13268
13269 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13270 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13271 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13272 ISD::isBuildVectorAllOnes(V2.getNode()))
13273 return true;
13274 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013275
13276 return false;
13277}
13278
Nate Begemanb65c1752010-12-17 22:55:37 +000013279static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13280 TargetLowering::DAGCombinerInfo &DCI,
13281 const X86Subtarget *Subtarget) {
13282 if (DCI.isBeforeLegalizeOps())
13283 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013284
Stuart Hastings865f0932011-06-03 23:53:54 +000013285 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13286 if (R.getNode())
13287 return R;
13288
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013289 // Want to form ANDNP nodes:
13290 // 1) In the hopes of then easily combining them with OR and AND nodes
13291 // to form PBLEND/PSIGN.
13292 // 2) To match ANDN packed intrinsics
Nate Begemanb65c1752010-12-17 22:55:37 +000013293 EVT VT = N->getValueType(0);
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013294 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013295 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013296
Nate Begemanb65c1752010-12-17 22:55:37 +000013297 SDValue N0 = N->getOperand(0);
13298 SDValue N1 = N->getOperand(1);
13299 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013300
Nate Begemanb65c1752010-12-17 22:55:37 +000013301 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013302 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013303 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13304 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013305 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013306
13307 // Check RHS for vnot
13308 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013309 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13310 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013311 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013312
Nate Begemanb65c1752010-12-17 22:55:37 +000013313 return SDValue();
13314}
13315
Evan Cheng760d1942010-01-04 21:22:48 +000013316static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013317 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013318 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013319 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013320 return SDValue();
13321
Stuart Hastings865f0932011-06-03 23:53:54 +000013322 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13323 if (R.getNode())
13324 return R;
13325
Evan Cheng760d1942010-01-04 21:22:48 +000013326 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000013327 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000013328 return SDValue();
13329
Evan Cheng760d1942010-01-04 21:22:48 +000013330 SDValue N0 = N->getOperand(0);
13331 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013332
Nate Begemanb65c1752010-12-17 22:55:37 +000013333 // look for psign/blend
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013334 if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013335 if (VT == MVT::v2i64) {
13336 // Canonicalize pandn to RHS
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013337 if (N0.getOpcode() == X86ISD::ANDNP)
Nate Begemanb65c1752010-12-17 22:55:37 +000013338 std::swap(N0, N1);
13339 // or (and (m, x), (pandn m, y))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013340 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
Nate Begemanb65c1752010-12-17 22:55:37 +000013341 SDValue Mask = N1.getOperand(0);
13342 SDValue X = N1.getOperand(1);
13343 SDValue Y;
13344 if (N0.getOperand(0) == Mask)
13345 Y = N0.getOperand(1);
13346 if (N0.getOperand(1) == Mask)
13347 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013348
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013349 // Check to see if the mask appeared in both the AND and ANDNP and
Nate Begemanb65c1752010-12-17 22:55:37 +000013350 if (!Y.getNode())
13351 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013352
Nate Begemanb65c1752010-12-17 22:55:37 +000013353 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
13354 if (Mask.getOpcode() != ISD::BITCAST ||
13355 X.getOpcode() != ISD::BITCAST ||
13356 Y.getOpcode() != ISD::BITCAST)
13357 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013358
Nate Begemanb65c1752010-12-17 22:55:37 +000013359 // Look through mask bitcast.
13360 Mask = Mask.getOperand(0);
13361 EVT MaskVT = Mask.getValueType();
13362
13363 // Validate that the Mask operand is a vector sra node. The sra node
13364 // will be an intrinsic.
13365 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
13366 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013367
Nate Begemanb65c1752010-12-17 22:55:37 +000013368 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
13369 // there is no psrai.b
13370 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
13371 case Intrinsic::x86_sse2_psrai_w:
13372 case Intrinsic::x86_sse2_psrai_d:
13373 break;
13374 default: return SDValue();
13375 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013376
Nate Begemanb65c1752010-12-17 22:55:37 +000013377 // Check that the SRA is all signbits.
13378 SDValue SraC = Mask.getOperand(2);
13379 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
13380 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
13381 if ((SraAmt + 1) != EltBits)
13382 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013383
Nate Begemanb65c1752010-12-17 22:55:37 +000013384 DebugLoc DL = N->getDebugLoc();
13385
13386 // Now we know we at least have a plendvb with the mask val. See if
13387 // we can form a psignb/w/d.
13388 // psign = x.type == y.type == mask.type && y = sub(0, x);
13389 X = X.getOperand(0);
13390 Y = Y.getOperand(0);
13391 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
13392 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
13393 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
13394 unsigned Opc = 0;
13395 switch (EltBits) {
13396 case 8: Opc = X86ISD::PSIGNB; break;
13397 case 16: Opc = X86ISD::PSIGNW; break;
13398 case 32: Opc = X86ISD::PSIGND; break;
13399 default: break;
13400 }
13401 if (Opc) {
13402 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
13403 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
13404 }
13405 }
13406 // PBLENDVB only available on SSE 4.1
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013407 if (!(Subtarget->hasSSE41() || Subtarget->hasAVX()))
Nate Begemanb65c1752010-12-17 22:55:37 +000013408 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013409
Nate Begemanb65c1752010-12-17 22:55:37 +000013410 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
13411 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
13412 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000013413 Mask = DAG.getNode(ISD::VSELECT, DL, MVT::v16i8, Mask, X, Y);
Nate Begemanb65c1752010-12-17 22:55:37 +000013414 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
13415 }
13416 }
13417 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013418
Nate Begemanb65c1752010-12-17 22:55:37 +000013419 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000013420 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13421 std::swap(N0, N1);
13422 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13423 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000013424 if (!N0.hasOneUse() || !N1.hasOneUse())
13425 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000013426
13427 SDValue ShAmt0 = N0.getOperand(1);
13428 if (ShAmt0.getValueType() != MVT::i8)
13429 return SDValue();
13430 SDValue ShAmt1 = N1.getOperand(1);
13431 if (ShAmt1.getValueType() != MVT::i8)
13432 return SDValue();
13433 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13434 ShAmt0 = ShAmt0.getOperand(0);
13435 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13436 ShAmt1 = ShAmt1.getOperand(0);
13437
13438 DebugLoc DL = N->getDebugLoc();
13439 unsigned Opc = X86ISD::SHLD;
13440 SDValue Op0 = N0.getOperand(0);
13441 SDValue Op1 = N1.getOperand(0);
13442 if (ShAmt0.getOpcode() == ISD::SUB) {
13443 Opc = X86ISD::SHRD;
13444 std::swap(Op0, Op1);
13445 std::swap(ShAmt0, ShAmt1);
13446 }
13447
Evan Cheng8b1190a2010-04-28 01:18:01 +000013448 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000013449 if (ShAmt1.getOpcode() == ISD::SUB) {
13450 SDValue Sum = ShAmt1.getOperand(0);
13451 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000013452 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13453 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13454 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13455 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000013456 return DAG.getNode(Opc, DL, VT,
13457 Op0, Op1,
13458 DAG.getNode(ISD::TRUNCATE, DL,
13459 MVT::i8, ShAmt0));
13460 }
13461 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13462 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13463 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000013464 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000013465 return DAG.getNode(Opc, DL, VT,
13466 N0.getOperand(0), N1.getOperand(0),
13467 DAG.getNode(ISD::TRUNCATE, DL,
13468 MVT::i8, ShAmt0));
13469 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013470
Evan Cheng760d1942010-01-04 21:22:48 +000013471 return SDValue();
13472}
13473
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013474/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
13475static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
13476 const X86Subtarget *Subtarget) {
13477 LoadSDNode *Ld = cast<LoadSDNode>(N);
13478 EVT RegVT = Ld->getValueType(0);
13479 EVT MemVT = Ld->getMemoryVT();
13480 DebugLoc dl = Ld->getDebugLoc();
13481 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13482
13483 ISD::LoadExtType Ext = Ld->getExtensionType();
13484
Nadav Rotemca6f2962011-09-18 19:00:23 +000013485 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013486 // shuffle. We need SSE4 for the shuffles.
13487 // TODO: It is possible to support ZExt by zeroing the undef values
13488 // during the shuffle phase or after the shuffle.
13489 if (RegVT.isVector() && Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
13490 assert(MemVT != RegVT && "Cannot extend to the same type");
13491 assert(MemVT.isVector() && "Must load a vector from memory");
13492
13493 unsigned NumElems = RegVT.getVectorNumElements();
13494 unsigned RegSz = RegVT.getSizeInBits();
13495 unsigned MemSz = MemVT.getSizeInBits();
13496 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000013497 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013498 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
13499
13500 // Attempt to load the original value using a single load op.
13501 // Find a scalar type which is equal to the loaded word size.
13502 MVT SclrLoadTy = MVT::i8;
13503 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13504 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13505 MVT Tp = (MVT::SimpleValueType)tp;
13506 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() == MemSz) {
13507 SclrLoadTy = Tp;
13508 break;
13509 }
13510 }
13511
13512 // Proceed if a load word is found.
13513 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
13514
13515 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
13516 RegSz/SclrLoadTy.getSizeInBits());
13517
13518 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
13519 RegSz/MemVT.getScalarType().getSizeInBits());
13520 // Can't shuffle using an illegal type.
13521 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13522
13523 // Perform a single load.
13524 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
13525 Ld->getBasePtr(),
13526 Ld->getPointerInfo(), Ld->isVolatile(),
13527 Ld->isNonTemporal(), Ld->getAlignment());
13528
13529 // Insert the word loaded into a vector.
13530 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
13531 LoadUnitVecVT, ScalarLoad);
13532
13533 // Bitcast the loaded value to a vector of the original element type, in
13534 // the size of the target vector type.
13535 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, ScalarInVector);
13536 unsigned SizeRatio = RegSz/MemSz;
13537
13538 // Redistribute the loaded elements into the different locations.
13539 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13540 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
13541
13542 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
13543 DAG.getUNDEF(SlicedVec.getValueType()),
13544 ShuffleVec.data());
13545
13546 // Bitcast to the requested type.
13547 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
13548 // Replace the original load with the new sequence
13549 // and return the new chain.
13550 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
13551 return SDValue(ScalarLoad.getNode(), 1);
13552 }
13553
13554 return SDValue();
13555}
13556
Chris Lattner149a4e52008-02-22 02:09:43 +000013557/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013558static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000013559 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000013560 StoreSDNode *St = cast<StoreSDNode>(N);
13561 EVT VT = St->getValue().getValueType();
13562 EVT StVT = St->getMemoryVT();
13563 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000013564 SDValue StoredVal = St->getOperand(1);
13565 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13566
13567 // If we are saving a concatination of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000013568 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
13569 // 128-bit ones. If in the future the cost becomes only one memory access the
13570 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000013571 if (VT.getSizeInBits() == 256 &&
13572 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
13573 StoredVal.getNumOperands() == 2) {
13574
13575 SDValue Value0 = StoredVal.getOperand(0);
13576 SDValue Value1 = StoredVal.getOperand(1);
13577
13578 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
13579 SDValue Ptr0 = St->getBasePtr();
13580 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
13581
13582 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
13583 St->getPointerInfo(), St->isVolatile(),
13584 St->isNonTemporal(), St->getAlignment());
13585 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
13586 St->getPointerInfo(), St->isVolatile(),
13587 St->isNonTemporal(), St->getAlignment());
13588 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
13589 }
Nadav Rotem614061b2011-08-10 19:30:14 +000013590
13591 // Optimize trunc store (of multiple scalars) to shuffle and store.
13592 // First, pack all of the elements in one place. Next, store to memory
13593 // in fewer chunks.
13594 if (St->isTruncatingStore() && VT.isVector()) {
13595 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13596 unsigned NumElems = VT.getVectorNumElements();
13597 assert(StVT != VT && "Cannot truncate to the same type");
13598 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
13599 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
13600
13601 // From, To sizes and ElemCount must be pow of two
13602 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013603 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000013604 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000013605 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000013606
Nadav Rotem614061b2011-08-10 19:30:14 +000013607 unsigned SizeRatio = FromSz / ToSz;
13608
13609 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
13610
13611 // Create a type on which we perform the shuffle
13612 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
13613 StVT.getScalarType(), NumElems*SizeRatio);
13614
13615 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
13616
13617 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
13618 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
13619 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
13620
13621 // Can't shuffle using an illegal type
13622 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
13623
13624 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
13625 DAG.getUNDEF(WideVec.getValueType()),
13626 ShuffleVec.data());
13627 // At this point all of the data is stored at the bottom of the
13628 // register. We now need to save it to mem.
13629
13630 // Find the largest store unit
13631 MVT StoreType = MVT::i8;
13632 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
13633 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
13634 MVT Tp = (MVT::SimpleValueType)tp;
13635 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
13636 StoreType = Tp;
13637 }
13638
13639 // Bitcast the original vector into a vector of store-size units
13640 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
13641 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
13642 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
13643 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
13644 SmallVector<SDValue, 8> Chains;
13645 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
13646 TLI.getPointerTy());
13647 SDValue Ptr = St->getBasePtr();
13648
13649 // Perform one or more big stores into memory.
13650 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
13651 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
13652 StoreType, ShuffWide,
13653 DAG.getIntPtrConstant(i));
13654 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
13655 St->getPointerInfo(), St->isVolatile(),
13656 St->isNonTemporal(), St->getAlignment());
13657 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
13658 Chains.push_back(Ch);
13659 }
13660
13661 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
13662 Chains.size());
13663 }
13664
13665
Chris Lattner149a4e52008-02-22 02:09:43 +000013666 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
13667 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000013668 // A preferable solution to the general problem is to figure out the right
13669 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000013670
13671 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000013672 if (VT.getSizeInBits() != 64)
13673 return SDValue();
13674
Devang Patel578efa92009-06-05 21:57:13 +000013675 const Function *F = DAG.getMachineFunction().getFunction();
13676 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000013677 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013678 && Subtarget->hasXMMInt();
Evan Cheng536e6672009-03-12 05:59:15 +000013679 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000013680 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000013681 isa<LoadSDNode>(St->getValue()) &&
13682 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
13683 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013684 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013685 LoadSDNode *Ld = 0;
13686 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000013687 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000013688 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013689 // Must be a store of a load. We currently handle two cases: the load
13690 // is a direct child, and it's under an intervening TokenFactor. It is
13691 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000013692 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000013693 Ld = cast<LoadSDNode>(St->getChain());
13694 else if (St->getValue().hasOneUse() &&
13695 ChainVal->getOpcode() == ISD::TokenFactor) {
13696 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000013697 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000013698 TokenFactorIndex = i;
13699 Ld = cast<LoadSDNode>(St->getValue());
13700 } else
13701 Ops.push_back(ChainVal->getOperand(i));
13702 }
13703 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000013704
Evan Cheng536e6672009-03-12 05:59:15 +000013705 if (!Ld || !ISD::isNormalLoad(Ld))
13706 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013707
Evan Cheng536e6672009-03-12 05:59:15 +000013708 // If this is not the MMX case, i.e. we are just turning i64 load/store
13709 // into f64 load/store, avoid the transformation if there are multiple
13710 // uses of the loaded value.
13711 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
13712 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000013713
Evan Cheng536e6672009-03-12 05:59:15 +000013714 DebugLoc LdDL = Ld->getDebugLoc();
13715 DebugLoc StDL = N->getDebugLoc();
13716 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
13717 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
13718 // pair instead.
13719 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013720 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000013721 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
13722 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013723 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013724 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000013725 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000013726 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000013727 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000013728 Ops.size());
13729 }
Evan Cheng536e6672009-03-12 05:59:15 +000013730 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013731 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013732 St->isVolatile(), St->isNonTemporal(),
13733 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000013734 }
Evan Cheng536e6672009-03-12 05:59:15 +000013735
13736 // Otherwise, lower to two pairs of 32-bit loads / stores.
13737 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013738 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
13739 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013740
Owen Anderson825b72b2009-08-11 20:47:22 +000013741 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013742 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013743 Ld->isVolatile(), Ld->isNonTemporal(),
13744 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000013745 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000013746 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000013747 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013748 MinAlign(Ld->getAlignment(), 4));
13749
13750 SDValue NewChain = LoLd.getValue(1);
13751 if (TokenFactorIndex != -1) {
13752 Ops.push_back(LoLd);
13753 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000013754 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000013755 Ops.size());
13756 }
13757
13758 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000013759 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
13760 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000013761
13762 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013763 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000013764 St->isVolatile(), St->isNonTemporal(),
13765 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000013766 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000013767 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000013768 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000013769 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000013770 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000013771 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000013772 }
Dan Gohman475871a2008-07-27 21:46:04 +000013773 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000013774}
13775
Duncan Sands17470be2011-09-22 20:15:48 +000013776/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
13777/// and return the operands for the horizontal operation in LHS and RHS. A
13778/// horizontal operation performs the binary operation on successive elements
13779/// of its first operand, then on successive elements of its second operand,
13780/// returning the resulting values in a vector. For example, if
13781/// A = < float a0, float a1, float a2, float a3 >
13782/// and
13783/// B = < float b0, float b1, float b2, float b3 >
13784/// then the result of doing a horizontal operation on A and B is
13785/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
13786/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
13787/// A horizontal-op B, for some already available A and B, and if so then LHS is
13788/// set to A, RHS to B, and the routine returns 'true'.
13789/// Note that the binary operation should have the property that if one of the
13790/// operands is UNDEF then the result is UNDEF.
13791static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool isCommutative) {
13792 // Look for the following pattern: if
13793 // A = < float a0, float a1, float a2, float a3 >
13794 // B = < float b0, float b1, float b2, float b3 >
13795 // and
13796 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
13797 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
13798 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
13799 // which is A horizontal-op B.
13800
13801 // At least one of the operands should be a vector shuffle.
13802 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
13803 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
13804 return false;
13805
13806 EVT VT = LHS.getValueType();
13807 unsigned N = VT.getVectorNumElements();
13808
13809 // View LHS in the form
13810 // LHS = VECTOR_SHUFFLE A, B, LMask
13811 // If LHS is not a shuffle then pretend it is the shuffle
13812 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
13813 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
13814 // type VT.
13815 SDValue A, B;
13816 SmallVector<int, 8> LMask(N);
13817 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
13818 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
13819 A = LHS.getOperand(0);
13820 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
13821 B = LHS.getOperand(1);
13822 cast<ShuffleVectorSDNode>(LHS.getNode())->getMask(LMask);
13823 } else {
13824 if (LHS.getOpcode() != ISD::UNDEF)
13825 A = LHS;
13826 for (unsigned i = 0; i != N; ++i)
13827 LMask[i] = i;
13828 }
13829
13830 // Likewise, view RHS in the form
13831 // RHS = VECTOR_SHUFFLE C, D, RMask
13832 SDValue C, D;
13833 SmallVector<int, 8> RMask(N);
13834 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
13835 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
13836 C = RHS.getOperand(0);
13837 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
13838 D = RHS.getOperand(1);
13839 cast<ShuffleVectorSDNode>(RHS.getNode())->getMask(RMask);
13840 } else {
13841 if (RHS.getOpcode() != ISD::UNDEF)
13842 C = RHS;
13843 for (unsigned i = 0; i != N; ++i)
13844 RMask[i] = i;
13845 }
13846
13847 // Check that the shuffles are both shuffling the same vectors.
13848 if (!(A == C && B == D) && !(A == D && B == C))
13849 return false;
13850
13851 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
13852 if (!A.getNode() && !B.getNode())
13853 return false;
13854
13855 // If A and B occur in reverse order in RHS, then "swap" them (which means
13856 // rewriting the mask).
13857 if (A != C)
13858 for (unsigned i = 0; i != N; ++i) {
13859 unsigned Idx = RMask[i];
13860 if (Idx < N)
13861 RMask[i] += N;
13862 else if (Idx < 2*N)
13863 RMask[i] -= N;
13864 }
13865
13866 // At this point LHS and RHS are equivalent to
13867 // LHS = VECTOR_SHUFFLE A, B, LMask
13868 // RHS = VECTOR_SHUFFLE A, B, RMask
13869 // Check that the masks correspond to performing a horizontal operation.
13870 for (unsigned i = 0; i != N; ++i) {
13871 unsigned LIdx = LMask[i], RIdx = RMask[i];
13872
13873 // Ignore any UNDEF components.
13874 if (LIdx >= 2*N || RIdx >= 2*N || (!A.getNode() && (LIdx < N || RIdx < N))
13875 || (!B.getNode() && (LIdx >= N || RIdx >= N)))
13876 continue;
13877
13878 // Check that successive elements are being operated on. If not, this is
13879 // not a horizontal operation.
13880 if (!(LIdx == 2*i && RIdx == 2*i + 1) &&
13881 !(isCommutative && LIdx == 2*i + 1 && RIdx == 2*i))
13882 return false;
13883 }
13884
13885 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
13886 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
13887 return true;
13888}
13889
13890/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
13891static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
13892 const X86Subtarget *Subtarget) {
13893 EVT VT = N->getValueType(0);
13894 SDValue LHS = N->getOperand(0);
13895 SDValue RHS = N->getOperand(1);
13896
13897 // Try to synthesize horizontal adds from adds of shuffles.
13898 if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
13899 (VT == MVT::v4f32 || VT == MVT::v2f64) &&
13900 isHorizontalBinOp(LHS, RHS, true))
13901 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
13902 return SDValue();
13903}
13904
13905/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
13906static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
13907 const X86Subtarget *Subtarget) {
13908 EVT VT = N->getValueType(0);
13909 SDValue LHS = N->getOperand(0);
13910 SDValue RHS = N->getOperand(1);
13911
13912 // Try to synthesize horizontal subs from subs of shuffles.
13913 if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
13914 (VT == MVT::v4f32 || VT == MVT::v2f64) &&
13915 isHorizontalBinOp(LHS, RHS, false))
13916 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
13917 return SDValue();
13918}
13919
Chris Lattner6cf73262008-01-25 06:14:17 +000013920/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
13921/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013922static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000013923 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
13924 // F[X]OR(0.0, x) -> x
13925 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000013926 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13927 if (C->getValueAPF().isPosZero())
13928 return N->getOperand(1);
13929 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13930 if (C->getValueAPF().isPosZero())
13931 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000013932 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013933}
13934
13935/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013936static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000013937 // FAND(0.0, x) -> 0.0
13938 // FAND(x, 0.0) -> 0.0
13939 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13940 if (C->getValueAPF().isPosZero())
13941 return N->getOperand(0);
13942 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13943 if (C->getValueAPF().isPosZero())
13944 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000013945 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000013946}
13947
Dan Gohmane5af2d32009-01-29 01:59:02 +000013948static SDValue PerformBTCombine(SDNode *N,
13949 SelectionDAG &DAG,
13950 TargetLowering::DAGCombinerInfo &DCI) {
13951 // BT ignores high bits in the bit index operand.
13952 SDValue Op1 = N->getOperand(1);
13953 if (Op1.hasOneUse()) {
13954 unsigned BitWidth = Op1.getValueSizeInBits();
13955 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
13956 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000013957 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
13958 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000013959 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000013960 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
13961 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
13962 DCI.CommitTargetLoweringOpt(TLO);
13963 }
13964 return SDValue();
13965}
Chris Lattner83e6c992006-10-04 06:57:07 +000013966
Eli Friedman7a5e5552009-06-07 06:52:44 +000013967static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
13968 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013969 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000013970 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000013971 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000013972 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000013973 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000013974 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000013975 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000013976 }
13977 return SDValue();
13978}
13979
Evan Cheng2e489c42009-12-16 00:53:11 +000013980static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
13981 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
13982 // (and (i32 x86isd::setcc_carry), 1)
13983 // This eliminates the zext. This transformation is necessary because
13984 // ISD::SETCC is always legalized to i8.
13985 DebugLoc dl = N->getDebugLoc();
13986 SDValue N0 = N->getOperand(0);
13987 EVT VT = N->getValueType(0);
13988 if (N0.getOpcode() == ISD::AND &&
13989 N0.hasOneUse() &&
13990 N0.getOperand(0).hasOneUse()) {
13991 SDValue N00 = N0.getOperand(0);
13992 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
13993 return SDValue();
13994 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
13995 if (!C || C->getZExtValue() != 1)
13996 return SDValue();
13997 return DAG.getNode(ISD::AND, dl, VT,
13998 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
13999 N00.getOperand(0), N00.getOperand(1)),
14000 DAG.getConstant(1, VT));
14001 }
14002
14003 return SDValue();
14004}
14005
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014006// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
14007static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
14008 unsigned X86CC = N->getConstantOperandVal(0);
14009 SDValue EFLAG = N->getOperand(1);
14010 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014011
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014012 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
14013 // a zext and produces an all-ones bit which is more useful than 0/1 in some
14014 // cases.
14015 if (X86CC == X86::COND_B)
14016 return DAG.getNode(ISD::AND, DL, MVT::i8,
14017 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
14018 DAG.getConstant(X86CC, MVT::i8), EFLAG),
14019 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014020
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014021 return SDValue();
14022}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014023
Benjamin Kramer1396c402011-06-18 11:09:41 +000014024static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
14025 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014026 SDValue Op0 = N->getOperand(0);
14027 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
14028 // a 32-bit target where SSE doesn't support i64->FP operations.
14029 if (Op0.getOpcode() == ISD::LOAD) {
14030 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
14031 EVT VT = Ld->getValueType(0);
14032 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
14033 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
14034 !XTLI->getSubtarget()->is64Bit() &&
14035 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000014036 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
14037 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014038 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
14039 return FILDChain;
14040 }
14041 }
14042 return SDValue();
14043}
14044
Chris Lattner23a01992010-12-20 01:37:09 +000014045// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
14046static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
14047 X86TargetLowering::DAGCombinerInfo &DCI) {
14048 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
14049 // the result is either zero or one (depending on the input carry bit).
14050 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
14051 if (X86::isZeroNode(N->getOperand(0)) &&
14052 X86::isZeroNode(N->getOperand(1)) &&
14053 // We don't have a good way to replace an EFLAGS use, so only do this when
14054 // dead right now.
14055 SDValue(N, 1).use_empty()) {
14056 DebugLoc DL = N->getDebugLoc();
14057 EVT VT = N->getValueType(0);
14058 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
14059 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
14060 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
14061 DAG.getConstant(X86::COND_B,MVT::i8),
14062 N->getOperand(2)),
14063 DAG.getConstant(1, VT));
14064 return DCI.CombineTo(N, Res1, CarryOut);
14065 }
14066
14067 return SDValue();
14068}
14069
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014070// fold (add Y, (sete X, 0)) -> adc 0, Y
14071// (add Y, (setne X, 0)) -> sbb -1, Y
14072// (sub (sete X, 0), Y) -> sbb 0, Y
14073// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014074static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014075 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014076
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014077 // Look through ZExts.
14078 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
14079 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
14080 return SDValue();
14081
14082 SDValue SetCC = Ext.getOperand(0);
14083 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
14084 return SDValue();
14085
14086 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
14087 if (CC != X86::COND_E && CC != X86::COND_NE)
14088 return SDValue();
14089
14090 SDValue Cmp = SetCC.getOperand(1);
14091 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000014092 !X86::isZeroNode(Cmp.getOperand(1)) ||
14093 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014094 return SDValue();
14095
14096 SDValue CmpOp0 = Cmp.getOperand(0);
14097 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
14098 DAG.getConstant(1, CmpOp0.getValueType()));
14099
14100 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
14101 if (CC == X86::COND_NE)
14102 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
14103 DL, OtherVal.getValueType(), OtherVal,
14104 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
14105 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
14106 DL, OtherVal.getValueType(), OtherVal,
14107 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
14108}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014109
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014110static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG) {
14111 SDValue Op0 = N->getOperand(0);
14112 SDValue Op1 = N->getOperand(1);
14113
14114 // X86 can't encode an immediate LHS of a sub. See if we can push the
14115 // negation into a preceding instruction.
14116 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014117 // If the RHS of the sub is a XOR with one use and a constant, invert the
14118 // immediate. Then add one to the LHS of the sub so we can turn
14119 // X-Y -> X+~Y+1, saving one register.
14120 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
14121 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000014122 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014123 EVT VT = Op0.getValueType();
14124 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
14125 Op1.getOperand(0),
14126 DAG.getConstant(~XorC, VT));
14127 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000014128 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014129 }
14130 }
14131
14132 return OptimizeConditionalInDecrement(N, DAG);
14133}
14134
Dan Gohman475871a2008-07-27 21:46:04 +000014135SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000014136 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014137 SelectionDAG &DAG = DCI.DAG;
14138 switch (N->getOpcode()) {
14139 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014140 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014141 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000014142 case ISD::VSELECT:
Chris Lattneraf723b92008-01-25 05:46:26 +000014143 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014144 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014145 case ISD::ADD: return OptimizeConditionalInDecrement(N, DAG);
14146 case ISD::SUB: return PerformSubCombine(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000014147 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000014148 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000014149 case ISD::SHL:
14150 case ISD::SRA:
14151 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000014152 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000014153 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014154 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000014155 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014156 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000014157 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
14158 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000014159 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000014160 case X86ISD::FOR: return PerformFORCombine(N, DAG);
14161 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000014162 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014163 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000014164 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014165 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014166 case X86ISD::SHUFPS: // Handle all target specific shuffles
14167 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000014168 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014169 case X86ISD::PUNPCKHBW:
14170 case X86ISD::PUNPCKHWD:
14171 case X86ISD::PUNPCKHDQ:
14172 case X86ISD::PUNPCKHQDQ:
14173 case X86ISD::UNPCKHPS:
14174 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +000014175 case X86ISD::VUNPCKHPSY:
14176 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014177 case X86ISD::PUNPCKLBW:
14178 case X86ISD::PUNPCKLWD:
14179 case X86ISD::PUNPCKLDQ:
14180 case X86ISD::PUNPCKLQDQ:
14181 case X86ISD::UNPCKLPS:
14182 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000014183 case X86ISD::VUNPCKLPSY:
14184 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014185 case X86ISD::MOVHLPS:
14186 case X86ISD::MOVLHPS:
14187 case X86ISD::PSHUFD:
14188 case X86ISD::PSHUFHW:
14189 case X86ISD::PSHUFLW:
14190 case X86ISD::MOVSS:
14191 case X86ISD::MOVSD:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000014192 case X86ISD::VPERMILPS:
14193 case X86ISD::VPERMILPSY:
14194 case X86ISD::VPERMILPD:
14195 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000014196 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014197 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014198 }
14199
Dan Gohman475871a2008-07-27 21:46:04 +000014200 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014201}
14202
Evan Chenge5b51ac2010-04-17 06:13:15 +000014203/// isTypeDesirableForOp - Return true if the target has native support for
14204/// the specified value type and it is 'desirable' to use the type for the
14205/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14206/// instruction encodings are longer and some i16 instructions are slow.
14207bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14208 if (!isTypeLegal(VT))
14209 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014210 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014211 return true;
14212
14213 switch (Opc) {
14214 default:
14215 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014216 case ISD::LOAD:
14217 case ISD::SIGN_EXTEND:
14218 case ISD::ZERO_EXTEND:
14219 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014220 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014221 case ISD::SRL:
14222 case ISD::SUB:
14223 case ISD::ADD:
14224 case ISD::MUL:
14225 case ISD::AND:
14226 case ISD::OR:
14227 case ISD::XOR:
14228 return false;
14229 }
14230}
14231
14232/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014233/// beneficial for dag combiner to promote the specified node. If true, it
14234/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014235bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014236 EVT VT = Op.getValueType();
14237 if (VT != MVT::i16)
14238 return false;
14239
Evan Cheng4c26e932010-04-19 19:29:22 +000014240 bool Promote = false;
14241 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014242 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014243 default: break;
14244 case ISD::LOAD: {
14245 LoadSDNode *LD = cast<LoadSDNode>(Op);
14246 // If the non-extending load has a single use and it's not live out, then it
14247 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014248 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14249 Op.hasOneUse()*/) {
14250 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14251 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14252 // The only case where we'd want to promote LOAD (rather then it being
14253 // promoted as an operand is when it's only use is liveout.
14254 if (UI->getOpcode() != ISD::CopyToReg)
14255 return false;
14256 }
14257 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014258 Promote = true;
14259 break;
14260 }
14261 case ISD::SIGN_EXTEND:
14262 case ISD::ZERO_EXTEND:
14263 case ISD::ANY_EXTEND:
14264 Promote = true;
14265 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014266 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014267 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014268 SDValue N0 = Op.getOperand(0);
14269 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014270 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014271 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014272 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014273 break;
14274 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014275 case ISD::ADD:
14276 case ISD::MUL:
14277 case ISD::AND:
14278 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014279 case ISD::XOR:
14280 Commute = true;
14281 // fallthrough
14282 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014283 SDValue N0 = Op.getOperand(0);
14284 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014285 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014286 return false;
14287 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014288 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014289 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014290 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014291 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014292 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014293 }
14294 }
14295
14296 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000014297 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014298}
14299
Evan Cheng60c07e12006-07-05 22:17:51 +000014300//===----------------------------------------------------------------------===//
14301// X86 Inline Assembly Support
14302//===----------------------------------------------------------------------===//
14303
Chris Lattnerb8105652009-07-20 17:51:36 +000014304bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14305 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000014306
14307 std::string AsmStr = IA->getAsmString();
14308
14309 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000014310 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000014311 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000014312
14313 switch (AsmPieces.size()) {
14314 default: return false;
14315 case 1:
14316 AsmStr = AsmPieces[0];
14317 AsmPieces.clear();
14318 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
14319
Chris Lattner7a2bdde2011-04-15 05:18:47 +000014320 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000014321 // we will turn this bswap into something that will be lowered to logical ops
14322 // instead of emitting the bswap asm. For now, we don't support 486 or lower
14323 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000014324 // bswap $0
14325 if (AsmPieces.size() == 2 &&
14326 (AsmPieces[0] == "bswap" ||
14327 AsmPieces[0] == "bswapq" ||
14328 AsmPieces[0] == "bswapl") &&
14329 (AsmPieces[1] == "$0" ||
14330 AsmPieces[1] == "${0:q}")) {
14331 // No need to check constraints, nothing other than the equivalent of
14332 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014333 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014334 if (!Ty || Ty->getBitWidth() % 16 != 0)
14335 return false;
14336 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000014337 }
14338 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000014339 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014340 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014341 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000014342 AsmPieces[1] == "$$8," &&
14343 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000014344 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14345 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014346 const std::string &ConstraintsStr = IA->getConstraintString();
14347 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000014348 std::sort(AsmPieces.begin(), AsmPieces.end());
14349 if (AsmPieces.size() == 4 &&
14350 AsmPieces[0] == "~{cc}" &&
14351 AsmPieces[1] == "~{dirflag}" &&
14352 AsmPieces[2] == "~{flags}" &&
14353 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014354 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014355 if (!Ty || Ty->getBitWidth() % 16 != 0)
14356 return false;
14357 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000014358 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014359 }
14360 break;
14361 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000014362 if (CI->getType()->isIntegerTy(32) &&
14363 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14364 SmallVector<StringRef, 4> Words;
14365 SplitString(AsmPieces[0], Words, " \t,");
14366 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14367 Words[2] == "${0:w}") {
14368 Words.clear();
14369 SplitString(AsmPieces[1], Words, " \t,");
14370 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
14371 Words[2] == "$0") {
14372 Words.clear();
14373 SplitString(AsmPieces[2], Words, " \t,");
14374 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
14375 Words[2] == "${0:w}") {
14376 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014377 const std::string &ConstraintsStr = IA->getConstraintString();
14378 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000014379 std::sort(AsmPieces.begin(), AsmPieces.end());
14380 if (AsmPieces.size() == 4 &&
14381 AsmPieces[0] == "~{cc}" &&
14382 AsmPieces[1] == "~{dirflag}" &&
14383 AsmPieces[2] == "~{flags}" &&
14384 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014385 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014386 if (!Ty || Ty->getBitWidth() % 16 != 0)
14387 return false;
14388 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000014389 }
14390 }
14391 }
14392 }
14393 }
Evan Cheng55d42002011-01-08 01:24:27 +000014394
14395 if (CI->getType()->isIntegerTy(64)) {
14396 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
14397 if (Constraints.size() >= 2 &&
14398 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
14399 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
14400 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
14401 SmallVector<StringRef, 4> Words;
14402 SplitString(AsmPieces[0], Words, " \t");
14403 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000014404 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000014405 SplitString(AsmPieces[1], Words, " \t");
14406 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
14407 Words.clear();
14408 SplitString(AsmPieces[2], Words, " \t,");
14409 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
14410 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014411 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000014412 if (!Ty || Ty->getBitWidth() % 16 != 0)
14413 return false;
14414 return IntrinsicLowering::LowerToByteSwap(CI);
14415 }
Chris Lattnerb8105652009-07-20 17:51:36 +000014416 }
14417 }
14418 }
14419 }
14420 break;
14421 }
14422 return false;
14423}
14424
14425
14426
Chris Lattnerf4dff842006-07-11 02:54:03 +000014427/// getConstraintType - Given a constraint letter, return the type of
14428/// constraint it is for this target.
14429X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000014430X86TargetLowering::getConstraintType(const std::string &Constraint) const {
14431 if (Constraint.size() == 1) {
14432 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000014433 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000014434 case 'q':
14435 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000014436 case 'f':
14437 case 't':
14438 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000014439 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000014440 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000014441 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000014442 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000014443 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000014444 case 'a':
14445 case 'b':
14446 case 'c':
14447 case 'd':
14448 case 'S':
14449 case 'D':
14450 case 'A':
14451 return C_Register;
14452 case 'I':
14453 case 'J':
14454 case 'K':
14455 case 'L':
14456 case 'M':
14457 case 'N':
14458 case 'G':
14459 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000014460 case 'e':
14461 case 'Z':
14462 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000014463 default:
14464 break;
14465 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000014466 }
Chris Lattner4234f572007-03-25 02:14:49 +000014467 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000014468}
14469
John Thompson44ab89e2010-10-29 17:29:13 +000014470/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000014471/// This object must already have been set up with the operand type
14472/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000014473TargetLowering::ConstraintWeight
14474 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000014475 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000014476 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014477 Value *CallOperandVal = info.CallOperandVal;
14478 // If we don't have a value, we can't do a match,
14479 // but allow it at the lowest weight.
14480 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000014481 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000014482 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000014483 // Look at the constraint type.
14484 switch (*constraint) {
14485 default:
John Thompson44ab89e2010-10-29 17:29:13 +000014486 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
14487 case 'R':
14488 case 'q':
14489 case 'Q':
14490 case 'a':
14491 case 'b':
14492 case 'c':
14493 case 'd':
14494 case 'S':
14495 case 'D':
14496 case 'A':
14497 if (CallOperandVal->getType()->isIntegerTy())
14498 weight = CW_SpecificReg;
14499 break;
14500 case 'f':
14501 case 't':
14502 case 'u':
14503 if (type->isFloatingPointTy())
14504 weight = CW_SpecificReg;
14505 break;
14506 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000014507 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000014508 weight = CW_SpecificReg;
14509 break;
14510 case 'x':
14511 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014512 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000014513 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014514 break;
14515 case 'I':
14516 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
14517 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000014518 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014519 }
14520 break;
John Thompson44ab89e2010-10-29 17:29:13 +000014521 case 'J':
14522 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14523 if (C->getZExtValue() <= 63)
14524 weight = CW_Constant;
14525 }
14526 break;
14527 case 'K':
14528 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14529 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
14530 weight = CW_Constant;
14531 }
14532 break;
14533 case 'L':
14534 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14535 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
14536 weight = CW_Constant;
14537 }
14538 break;
14539 case 'M':
14540 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14541 if (C->getZExtValue() <= 3)
14542 weight = CW_Constant;
14543 }
14544 break;
14545 case 'N':
14546 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14547 if (C->getZExtValue() <= 0xff)
14548 weight = CW_Constant;
14549 }
14550 break;
14551 case 'G':
14552 case 'C':
14553 if (dyn_cast<ConstantFP>(CallOperandVal)) {
14554 weight = CW_Constant;
14555 }
14556 break;
14557 case 'e':
14558 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14559 if ((C->getSExtValue() >= -0x80000000LL) &&
14560 (C->getSExtValue() <= 0x7fffffffLL))
14561 weight = CW_Constant;
14562 }
14563 break;
14564 case 'Z':
14565 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
14566 if (C->getZExtValue() <= 0xffffffff)
14567 weight = CW_Constant;
14568 }
14569 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000014570 }
14571 return weight;
14572}
14573
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014574/// LowerXConstraint - try to replace an X constraint, which matches anything,
14575/// with another that has more specific requirements based on the type of the
14576/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000014577const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000014578LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000014579 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
14580 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000014581 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014582 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000014583 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014584 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000014585 return "x";
14586 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014587
Chris Lattner5e764232008-04-26 23:02:14 +000014588 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000014589}
14590
Chris Lattner48884cd2007-08-25 00:47:38 +000014591/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
14592/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000014593void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000014594 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000014595 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000014596 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000014597 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000014598
Eric Christopher100c8332011-06-02 23:16:42 +000014599 // Only support length 1 constraints for now.
14600 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000014601
Eric Christopher100c8332011-06-02 23:16:42 +000014602 char ConstraintLetter = Constraint[0];
14603 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014604 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000014605 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000014606 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014607 if (C->getZExtValue() <= 31) {
14608 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014609 break;
14610 }
Devang Patel84f7fd22007-03-17 00:13:28 +000014611 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014612 return;
Evan Cheng364091e2008-09-22 23:57:37 +000014613 case 'J':
14614 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014615 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000014616 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14617 break;
14618 }
14619 }
14620 return;
14621 case 'K':
14622 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000014623 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000014624 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14625 break;
14626 }
14627 }
14628 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000014629 case 'N':
14630 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000014631 if (C->getZExtValue() <= 255) {
14632 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000014633 break;
14634 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000014635 }
Chris Lattner48884cd2007-08-25 00:47:38 +000014636 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000014637 case 'e': {
14638 // 32-bit signed value
14639 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014640 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14641 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014642 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014643 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000014644 break;
14645 }
14646 // FIXME gcc accepts some relocatable values here too, but only in certain
14647 // memory models; it's complicated.
14648 }
14649 return;
14650 }
14651 case 'Z': {
14652 // 32-bit unsigned value
14653 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000014654 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14655 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014656 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14657 break;
14658 }
14659 }
14660 // FIXME gcc accepts some relocatable values here too, but only in certain
14661 // memory models; it's complicated.
14662 return;
14663 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014664 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014665 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000014666 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000014667 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000014668 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000014669 break;
14670 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014671
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014672 // In any sort of PIC mode addresses need to be computed at runtime by
14673 // adding in a register or some sort of table lookup. These can't
14674 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000014675 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000014676 return;
14677
Chris Lattnerdc43a882007-05-03 16:52:29 +000014678 // If we are in non-pic codegen mode, we allow the address of a global (with
14679 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000014680 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014681 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000014682
Chris Lattner49921962009-05-08 18:23:14 +000014683 // Match either (GA), (GA+C), (GA+C1+C2), etc.
14684 while (1) {
14685 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
14686 Offset += GA->getOffset();
14687 break;
14688 } else if (Op.getOpcode() == ISD::ADD) {
14689 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14690 Offset += C->getZExtValue();
14691 Op = Op.getOperand(0);
14692 continue;
14693 }
14694 } else if (Op.getOpcode() == ISD::SUB) {
14695 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14696 Offset += -C->getZExtValue();
14697 Op = Op.getOperand(0);
14698 continue;
14699 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014700 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014701
Chris Lattner49921962009-05-08 18:23:14 +000014702 // Otherwise, this isn't something we can handle, reject it.
14703 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000014704 }
Eric Christopherfd179292009-08-27 18:07:15 +000014705
Dan Gohman46510a72010-04-15 01:51:59 +000014706 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014707 // If we require an extra load to get this address, as in PIC mode, we
14708 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000014709 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
14710 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000014711 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000014712
Devang Patel0d881da2010-07-06 22:08:15 +000014713 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
14714 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000014715 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014716 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000014717 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014718
Gabor Greifba36cb52008-08-28 21:40:38 +000014719 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000014720 Ops.push_back(Result);
14721 return;
14722 }
Dale Johannesen1784d162010-06-25 21:55:36 +000014723 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000014724}
14725
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014726std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000014727X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000014728 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000014729 // First, see if this is a constraint that directly corresponds to an LLVM
14730 // register class.
14731 if (Constraint.size() == 1) {
14732 // GCC Constraint Letters
14733 switch (Constraint[0]) {
14734 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000014735 // TODO: Slight differences here in allocation order and leaving
14736 // RIP in the class. Do they matter any more here than they do
14737 // in the normal allocation?
14738 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
14739 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014740 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014741 return std::make_pair(0U, X86::GR32RegisterClass);
14742 else if (VT == MVT::i16)
14743 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014744 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014745 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014746 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000014747 return std::make_pair(0U, X86::GR64RegisterClass);
14748 break;
14749 }
14750 // 32-bit fallthrough
14751 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000014752 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000014753 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
14754 else if (VT == MVT::i16)
14755 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000014756 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000014757 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
14758 else if (VT == MVT::i64)
14759 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
14760 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014761 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000014762 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014763 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000014764 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014765 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000014766 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000014767 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000014768 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000014769 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014770 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000014771 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000014772 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
14773 if (VT == MVT::i16)
14774 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
14775 if (VT == MVT::i32 || !Subtarget->is64Bit())
14776 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
14777 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014778 case 'f': // FP Stack registers.
14779 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
14780 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000014781 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014782 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014783 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000014784 return std::make_pair(0U, X86::RFP64RegisterClass);
14785 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000014786 case 'y': // MMX_REGS if MMX allowed.
14787 if (!Subtarget->hasMMX()) break;
14788 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014789 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014790 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000014791 // FALL THROUGH.
14792 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000014793 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000014794
Owen Anderson825b72b2009-08-11 20:47:22 +000014795 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000014796 default: break;
14797 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014798 case MVT::f32:
14799 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000014800 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000014801 case MVT::f64:
14802 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000014803 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000014804 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000014805 case MVT::v16i8:
14806 case MVT::v8i16:
14807 case MVT::v4i32:
14808 case MVT::v2i64:
14809 case MVT::v4f32:
14810 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000014811 return std::make_pair(0U, X86::VR128RegisterClass);
14812 }
Chris Lattnerad043e82007-04-09 05:11:28 +000014813 break;
14814 }
14815 }
Scott Michelfdc40a02009-02-17 22:15:04 +000014816
Chris Lattnerf76d1802006-07-31 23:26:50 +000014817 // Use the default implementation in TargetLowering to convert the register
14818 // constraint into a member of a register class.
14819 std::pair<unsigned, const TargetRegisterClass*> Res;
14820 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000014821
14822 // Not found as a standard register?
14823 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014824 // Map st(0) -> st(7) -> ST0
14825 if (Constraint.size() == 7 && Constraint[0] == '{' &&
14826 tolower(Constraint[1]) == 's' &&
14827 tolower(Constraint[2]) == 't' &&
14828 Constraint[3] == '(' &&
14829 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
14830 Constraint[5] == ')' &&
14831 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000014832
Chris Lattner56d77c72009-09-13 22:41:48 +000014833 Res.first = X86::ST0+Constraint[4]-'0';
14834 Res.second = X86::RFP80RegisterClass;
14835 return Res;
14836 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014837
Chris Lattner56d77c72009-09-13 22:41:48 +000014838 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014839 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000014840 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000014841 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014842 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000014843 }
Chris Lattner56d77c72009-09-13 22:41:48 +000014844
14845 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000014846 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000014847 Res.first = X86::EFLAGS;
14848 Res.second = X86::CCRRegisterClass;
14849 return Res;
14850 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000014851
Dale Johannesen330169f2008-11-13 21:52:36 +000014852 // 'A' means EAX + EDX.
14853 if (Constraint == "A") {
14854 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000014855 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000014856 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000014857 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000014858 return Res;
14859 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014860
Chris Lattnerf76d1802006-07-31 23:26:50 +000014861 // Otherwise, check to see if this is a register class of the wrong value
14862 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
14863 // turn into {ax},{dx}.
14864 if (Res.second->hasType(VT))
14865 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014866
Chris Lattnerf76d1802006-07-31 23:26:50 +000014867 // All of the single-register GCC register classes map their values onto
14868 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
14869 // really want an 8-bit or 32-bit register, map to the appropriate register
14870 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000014871 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014872 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014873 unsigned DestReg = 0;
14874 switch (Res.first) {
14875 default: break;
14876 case X86::AX: DestReg = X86::AL; break;
14877 case X86::DX: DestReg = X86::DL; break;
14878 case X86::CX: DestReg = X86::CL; break;
14879 case X86::BX: DestReg = X86::BL; break;
14880 }
14881 if (DestReg) {
14882 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014883 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014884 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014885 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014886 unsigned DestReg = 0;
14887 switch (Res.first) {
14888 default: break;
14889 case X86::AX: DestReg = X86::EAX; break;
14890 case X86::DX: DestReg = X86::EDX; break;
14891 case X86::CX: DestReg = X86::ECX; break;
14892 case X86::BX: DestReg = X86::EBX; break;
14893 case X86::SI: DestReg = X86::ESI; break;
14894 case X86::DI: DestReg = X86::EDI; break;
14895 case X86::BP: DestReg = X86::EBP; break;
14896 case X86::SP: DestReg = X86::ESP; break;
14897 }
14898 if (DestReg) {
14899 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014900 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014901 }
Owen Anderson825b72b2009-08-11 20:47:22 +000014902 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000014903 unsigned DestReg = 0;
14904 switch (Res.first) {
14905 default: break;
14906 case X86::AX: DestReg = X86::RAX; break;
14907 case X86::DX: DestReg = X86::RDX; break;
14908 case X86::CX: DestReg = X86::RCX; break;
14909 case X86::BX: DestReg = X86::RBX; break;
14910 case X86::SI: DestReg = X86::RSI; break;
14911 case X86::DI: DestReg = X86::RDI; break;
14912 case X86::BP: DestReg = X86::RBP; break;
14913 case X86::SP: DestReg = X86::RSP; break;
14914 }
14915 if (DestReg) {
14916 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000014917 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000014918 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000014919 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000014920 } else if (Res.second == X86::FR32RegisterClass ||
14921 Res.second == X86::FR64RegisterClass ||
14922 Res.second == X86::VR128RegisterClass) {
14923 // Handle references to XMM physical registers that got mapped into the
14924 // wrong class. This can happen with constraints like {xmm0} where the
14925 // target independent register mapper will just pick the first match it can
14926 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000014927 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014928 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000014929 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000014930 Res.second = X86::FR64RegisterClass;
14931 else if (X86::VR128RegisterClass->hasType(VT))
14932 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000014933 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014934
Chris Lattnerf76d1802006-07-31 23:26:50 +000014935 return Res;
14936}